Implement a java class named Point that represents a two-dimensional point, with
ID: 3811004 • Letter: I
Question
Implement a java class named Point that represents a two-dimensional point, with an x and y value. The Point class should have the following instance variables (a.k.a. fields or data):
x (the x-value of the point, as a whole number)
y (the y-value of the point, as a whole number)
The Point class will have methods to:
Create a new Point (given an x-value and y-value) [constructor]
Create a new Point (from another Point) [copy constructor]
getX
getY
setX
setY
distanceTo(Point otherPoint)
equals [method to check if one Point is the same as another]
toString [method to turn a Point into a string for display, e.g. display as "Point[x=0,y=5]" if the x-value is 0 and y-value is 5]
The distanceTo method can be implemented with the following equation for distance:
Now, implement a class named Line that represents a two-dimensional line, consisting of two points. The Line class should have the following instance variables (a.k.a. fields or data):
p1 (the first Point of the line)
p2 (the last Point of the line)
The Line class will have methods to:
Create a new Line (from two x-values and two y-values) [constructor]
Create a new Line (from two Points) [constructor]
Create a new Line (from another Line) [copy constructor]
getP1
getP2
getX1
getY1
getX2
getY2
setP1
setP2
setX1
setY1
setX2
setY2
length() [method that calculates the length of the Line]
equals [method to check if one Line is the same as another]
toString [method to turn a Line into a string for display, e.g. display as "Line[(x1=0,y1=5), (x2=0,y2=10)]"
Explanation / Answer
PROGRAM CODE:
Point.java
package array;
public class Point {
private int x;
private int y;
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 int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public boolean equals(Object obj) {
Point p = (Point)obj;
if(this.x == p.x && this.y == p.y)
return true;
else return false;
}
public double distanceTo(Point otherPoint)
{
return Math.sqrt(Math.pow((otherPoint.x - this.x), 2) + Math.pow((otherPoint.y - this.y), 2));
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Point[x=" + x + ",y=" + y + "]";
}
}
LIne.java
package array;
public class Line {
private Point p1;
private Point p2;
Line(int x1, int x2, int y1, int y2)
{
p1 = new Point(x1, y1);
p2 = new Point(x2, y2);
}
Line(Point p1, Point p2)
{
this.p1 = p1;
this.p2 = p2;
}
Line(Line otherLine)
{
this.p1 = otherLine.p1;
this.p2 = otherLine.p2;
}
public Point getP1() {
return p1;
}
public void setP1(Point p1) {
this.p1 = p1;
}
public Point getP2() {
return p2;
}
public void setP2(Point p2) {
this.p2 = p2;
}
public int getX1() {
return p1.getX();
}
public void setX1(int x) {
p1.setX(x);
}
public int getY1() {
return p1.getY();
}
public void setY1(int y) {
p1.setY(y);
}
public int getX2() {
return p1.getX();
}
public void setX2(int x) {
p1.setX(x);
}
public int getY2() {
return p1.getY();
}
public void setY2(int y) {
p1.setY(y);
}
public double length()
{
return p1.distanceTo(p2);
}
@Override
public boolean equals(Object obj) {
Line line = (Line)obj;
if(this.p1 == line.p1 && this.p2 == line.p2)
return true;
else return false;
}
@Override
public String toString() {
return "Line[(x1=" + p1.getX() + ",y1=" + p1.getY() + "), (x2=" + p2.getX() + ",y2=1" + p2.getY() + ")]";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.