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

(The MyPoint.java class) Design a class named MyPoint to represent a point with

ID: 3684813 • Letter: #

Question

(The MyPoint.java class) Design a class named MyPoint to represent a point with x and y coordinates. The class contains: - Two data fields x and y that represent the coordinates. - A no-arg constructor that creates a point (0, 0). - A constructor that constructs a point with specified coordinates. - Two get methods for data fields x and y, respectively. - A method named distance that returns the distance from this point to another point of the MyPoint type. - A method named distance that returns the distance from this point to another point with specified x and y-coordinates. Draw the UML diagram for the class. Implement the class. Write a test program that creates two points (0, 0) and (10, 30.5) and displays the distance between them.

Explanation / Answer

********** MyPoint.java ******************

public class MyPoint {

   private double x;
   private double y;
  
   public MyPoint() {
       x = 0;
       y = 0;
   }
  
   public MyPoint(double x, double y) {
       this.x = x;
       this.y = y;
   }
  
   public double getX(){
       return x;
   }
  
   public double getY(){
       return y;
   }
  
   public double distance(MyPoint podouble){
      
       return Math.sqrt((podouble.getX() -x)*(podouble.getX() -x) + (podouble.getY() -y)*(podouble.getY() -y));
   }
  
   public double distance(double x1, double y1){
      
       return Math.sqrt((x1 -x)*(x1 -x) + (y1 -y)*(y1 -y));
   }
}

************** Test Class ****************

public class TestPoint {

   public static void main(String[] args) {
      
       MyPoint p1 = new MyPoint();
      
       MyPoint p2 = new MyPoint(10, 30.5);
      
       System.out.println("Distance: "+ String.format("%.2f",p2.distance(p1)));
   }
}