Question
using python 3: implement the scale method A) class Point: def __init__(self, initX, initY): Create a new point at the given coordinates self.__x
using python 3: implement the scale method
A)
class Point:
def __init__(self, initX, initY): """ Create a new point at the given coordinates """ self.__x = initX self.__y = initY
def getX(self): """ Get its x coordinate """ return self.__x
def getY(self): """ Get its y coordinate """ return self.__y
def __str__(self): """ Return a string representation of the point """ return "({}, {})".format(self.__x, self.__y)
def scale(self, val): """ Return a new point that is self multiplied by val """
if __name__ == "__main__": import test import unittest class TestPoint(unittest.TestCase): def setUp(self): pass def test_scale_1(self): p = Point(6,2) self.assertEqual(p.scale(Point(12, 4)), 2)
def test_scale_2(self): q = Point(1,1) self.assertEqual(q.scale(Point(11,11)),11) if __name__ == "__main__": unittest.main()
B)
We can represent a rectangle by knowing three things: the location of its lower left corner (its starting point), its width, and its height. Write a class definition and constructor method for a Rectangle class (after the definition for the Point class) using this idea. To instantiate a Rectangle object at location (4,5) with width 6 and height 5, we would do the following:
r = Rectangle(Point(4, 5), 6, 5)
class Point: def __init__(self, initX, initY): """ Create a new point at the given coordinates """ self.__x = initX self.__y = initY
def __str__(self): """ Return a string representation of the point """ return "({}, {})".format(self.__x, self.__y)
# your code goes here
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started