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

  1. Create a new class called Point that represents a point in the Cartesian plane with two coordinates.
    1. Each instance of Point should have to coordinates called x and y.
    2. The constructor for Point should take two arguments x and y that represent the two coordinates of the newly created point.
    3. 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.
    4. 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.
    5. 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.
  2. Write a function distance(p, q) that takes two Points p and q and returns the Euclidean distance between p and q.
  3. Write a function double(p) that modifies p so that the values of its x- and y-coordinates are doubled.
  4. 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!