Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Point class that represents points on an x, y axis. The data members sho

ID: 3551212 • Letter: W

Question

Write a Point class that represents points on an x, y axis. The data members should be doubles x and y plus an int value called count that represents the number of points currently instantiated. Your class should have 3 constructors; a default constructor, a copy constructor, and another that allows you to set the x and y values when the Point object is created. Your class should have a finalizer so that the count value always correctly reflects the number of objects currently instantiated. Your class should have get and set methods for both instance variables. Your class should have an equals method that returns true when any object passed in as a parameter has the same values for x and y as the Point object whose method was called. Finally, your class should contain a toString method that prints the Point's values in the following format: Point ( <x value>, <y value>) where x value and y value are the values of x and y stored in the object.

Explanation / Answer



//Write a Point class that represents points on an x, y axis.
class Point
{
// The data members should be doubles x and y plus an int value called count that represents the number of points currently instantiated.
private int x;
private int y;
private static int count = 0;
//Your class should have 3 constructors; a default constructor, a copy constructor,
//and another that allows you to set the x and y values when the Point object is created.
public Point()
{
x = 0;
y = 0;
count++;
}
public Point(Point P)
{
x = P.x;
y = P.y;
count++;
}
public Point(int x, int y)
{
this.x = x;
this.y = y;
count++;
}
// Your class should have a finalizer so that the count value always correctly reflects the number of objects currently instantiated.
public static int No_of_objects()
{
return count;
}
// Your class should have get and set methods for both instance variables.
public void setX(int x)
{
this.x = x;
}
public void setY(int y)
{
this.y = y;
}
public int getX()
{
return x;
}
public int getY()
{
return x;
}
// Your class should have an equals method that returns true when any object
//passed in as a parameter has the same values for x and y as the Point object whose method was called.
public boolean equals(Point P)
{
return (x == P.x && y == P.y);
}
// Finally, your class should contain a toString method that prints the Point's values in the following format: Point ( <x value>, <y value>)
//where x value and y value are the values of x and y stored in the object.
public String toString()
{
return "( "+ x + ","+y + " )";
}
}

public class tester
{
public static void main(String[] args)
{
Point P1= new Point();
Point P2= new Point(P1);
Point P3 = new Point(4,5);
System.out.println("Point P1 is " + P1);
System.out.println("Point P3 is " + P3);
System.out.println("Are P1 and P3 are equal ? "+ (P1.equals(P3)?"True":"False"));
System.out.println("Total Number of objects created is " + Point.No_of_objects());
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote