I need help on this assignment please!! I am a beginner and my code looks really
ID: 3562356 • Letter: I
Question
I need help on this assignment please!! I am a beginner and my code looks really funny to myself (so I will not post it here). I do not want the answer, I need to learn!!! I want someone to write this program here, so I can compare it with mine. Here is the requirements: Your Point class will represent a point on the Cartesian plane it will have both an x and a y coordinate. You need to implement at least the following required data variables/constructors/methods: Data Variables You should have two integers that hold the x-value and the y-value of the Point. These must be private. Constructors You should have at least three constructors: A default constructor that will set the values to (0, 0) A parameterized constructor that will receive 2 ints (first the x coordinate and then the y) and set the data variables to the values received. A copy constructor that will receive a Point and then initializes the data (of the new instance being created) to be the same as the Point that was received (Note - this should be a DEEP copy!). Methods public double distanceTo( Point other ) The distanceTo method should return the distance to the other Point. public Point halfwayTo( Point other ) The halfwayTo method should return a new Point that is halfway between itself (the original Point) and the other Point that is received. If the halfway point does not have integer x and y values, you should round to the nearest integer values. public void translate( int xmove, int ymove ) The translate method returns nothing and should translate, or shift, itself (the Point) by the distances passed in. So if a Point p has data x=3 and y=5, p.translate(5,2) will cause p to change its data to x=8 and y=7. public boolean equals( Object otherObj ) The equals method should return true if the given object is equal to the Object passed in. Note that this method receives an Object; there is a particular way that this method should be implemented (see notes from class). public String toString() A toString() method that returns a String representing the Point. It should display the point in the form "(x, y)".
Explanation / Answer
public class Point {
private int x;
private int y;
public Point() {
x = 0;
y = 0;
}
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point(Point other) {
this.x = other.x;
this.y = other.y;
}
public void translate(int xDistance, int yDistance) {
x = x + xDistance;
y = y + yDistance;
}
public double distanceTo(Point other) {
return Math.sqrt(((this.x - other.x) * (this.x - other.x))
+ (this.y - other.y) * (this.y - other.y));
}
public Point halfWayTo(Point other) {
return new Point((int) (this.x + other.x) / 2,
(int) (this.y + other.y) / 2);
}
public boolean equals(Object obj) {
if (obj instanceof Point) {
Point other = (Point) obj;
return this.x == other.x && this.y == other.y;
}
return super.equals(obj);
}
public String toString() {
return "(" + x + "," + y + ")";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.