Question: Python Create a new class called Point that represents a point in the Cartesian plane with two coordinates. Each instance of Point should have to
Python
- Create a new class called Point that represents a point in the Cartesian plane with two coordinates.
- Each instance of Point should have to coordinates called x and y.
- The constructor for Point should take two arguments x and y that represent the two coordinates of the newly created point.
- Your class Point should override the __str__(self) method so that it returns a string showing the x- and y-coordinates of the Point enclosed in parentheses and separated by a comma.
- Your class Point should override the __repr__(self) method so that it returns a string that contains Python code that would produce an instance of the same point.
- Your class Point should override the __eq__(self, other) method so that it returns True if and only if other is also a Point that has the same x- and y- coordinate as self.
- Write a function distance(p, q) that takes two Points p and q and returns the Euclidean distance between p and q.
- Write a function double(p) that modifies p so that the values of its x- and y-coordinates are doubled.
- Write a function doubled(p) that returns a new Point whose x- and y-coordinates are twice those of p
Sample Output
>>> p = Point(3,4)
>>> p Point(3,4)
>>> >>> str(p) '(3,4)'
>>> >>> print("p = {}".format(p)) p = (3,4)
>>> >>> >>> q = Point(3,4)
>>> >>> >>> p Point(3,4)
>>> q Point(3,4)
>>> >>> >>> p == q True
>>> >>> >>> p is q False
>>> >>> >>> >>> >>> r = Point(3,7)
>>> p Point(3,4)
>>> r Point(3,7)
>>> >>> p == r False
>>> >>> >>> p Point(3,4)
>>> q Point(3,4)
>>> r Point(3,7)
>>> >>> distance (p,q) 0.0
>>> >>> distance(p,r) 3.0
>>> >>> >>> p Point(3,4)
>>> double(p)
>>> p Point(6,8)
>>> >>> >>> q Point(3,4)
>>> doubled(q) Point(6,8)
>>> q Point(3,4)
>>> s = doubled(q)
>>> >>> s Point(6,8)
>>> q Point(3,4)
>>> >>> quit()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
