Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write the code for a class named Point to represent a point with x and y coordinates. The class contains: Two instance variables x and

Write the code for a class named Point to represent a point with x and y coordinates. The class contains:

  • Two instance variables x and y that represent the coordinates with getX() and getY() methods.
  • A constructor that constructs a point with specified coordinates, with default values 0 and 0.
  • An str method to return a string representation in the form (x, y).
  • A method named distance that returns the distance from this point to another point.
  • Appropriate code so that points can be compared using the comparison operators. Points are compared based on their distance from the origin. To simplify your code, you may add a method that returns the distance between an object and the point of origin (0, 0).
  • Add a class variable named COUNT. Initialize COUNT to 0. The purpose of this class variable is to keep track of the number of Point objects that have been created. Add code to the constructor to increment COUNT by 1 every time a Point object is created.

Add a test program that does the following:

  • Creates a point object, named p1, with coordinates at (3, 4)
  • Print the object p1. Remember that if p1 is an object of type Point, then print(p1) automatically calls the __str__ method of the calss. The output from this line is (3, 4)
  • Creates another point object, named p2, with coordinates at (3, 0)
  • Print the x and y coordinates of p2 using the getX() and getY() methods
  • Find and print the distance between p1 and p2
  • Print the results of comparing p1 and p2. Make sure to test all 6 comparison operators.
  • Your code should allow using == and != to compare a Point object with an object of a different type. Print the result of using the equality and inequality operators to compare p1 with the string "Hello".
  • Using the class variable, print out the number of Point objects that have been created by the program.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions