Answer to Question #4784 in Java | JSP | JSF for Allaa

Question #4784
I have to implement a class which represents a 2-d geometrical point and for my main program it is: public static void main(String[] args){
Point p=new Point();
System.out.println(p);
Point q=new Point(3,4);
System.out.println(p);
System.out.println(distance(p,q));
p.translate(2,3);
q.SetLocation(7,15);
System.out.println(p.distance(q));
i am having trouble figuring out what other codes to enter to make this work, the output should be:
Point [0.0,0.0]
Point[3.0,4.0]
5.0
13.0
HELP?
1
Expert's answer
2012-05-24T08:45:14-0400

public class Point {

private double x, y;

public Point(){
& this.x = 0.0;
& this.y = 0.0;
}

public Point(double x, double y){
& this.x = x;
& this.y = y;
}

@Override
public String toString(){
& return "Point[" + x + "," + y + "]";
}


public double getX() {
& return x;
}

public double getY() {
& return y;
}

public void translate(double x, double y){
& this.x+=x;
& this.y+=y;
}

public void SetLocation(double x, double y){
& this.x = x;
& this.y = y;
}

public double distance(Point q){
& return Math.sqrt((getX() - q.getX())*(getX() - q.getX()) + (getY() - q.getY())*(getY() - q.getY()));
}
}




public class PointTest {

public static double distance(Point p, Point q){
& return Math.sqrt((p.getX() - q.getX())*(p.getX() - q.getX()) + (p.getY() - q.getY())*(p.getY() - q.getY()));
}


public static void main(String[] args) {
&
& Point p=new Point();
& System.out.println(p);
& Point q=new Point(3,4);
& System.out.println(q);
& System.out.println(distance(p,q));
& p.translate(2,3);
& q.SetLocation(7,15);
& System.out.println(p.distance(q));

}

}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS