Write a Java program for a class called MyPoint, which models a 2D point with x and y coordinates, is designed as shown in the class diagram. It contains: Two instance variables x (int) and y (int). A default (or "no-argument" or "no-arg") constructor that construct a point at the default location of (0, 0). A overloaded constructor that constructs a point with the given x and y coordinates. Getter and setter for the instance variables x and y. A method setXY() to set both x and y. A method getXY() which returns the x and y in a 2-element int array. A toString() method that returns a string description of the instance in the format "(x, y)". A method called distance(int x, int y) that returns the distance from this point to another point at the given (x, y) coordinates, e.g.,MyPoint p1 = new MyPoint(3, 4); System.out.println(p1.distance(5, 6)); An overloaded distance(MyPoint another) that returns the distance from this point to the given MyPoint instance (called another), e.g.,MyPoint p1 = new MyPoint(3, 4); MyPoint p2 = new MyPoint(5, 6); System.out.println(p1.distance(p2)); Another overloaded distance() method that returns the distance from this point to the origin (0,0), e.g.,MyPoint p1 = new MyPoint(3, 4); System.out.println(p1.distance()); You are required to: Write the code for the class MyPoint. Write unit-tests in Java class MyPointTest. // Test program to test all constructors and public methods MyPoint p1 = new MyPoint(); // Test constructor System.out.println(p1); // Test toString() p1.setX(8); // Test setters p1.setY(6); System.out.println("x is: " + p1.getX()); // Test getters System.out.println("y is: " + p1.getY()); p1.setXY(3, 0); // Test setXY() System.out.println(p1.getXY()[0]); // Test getXY() System.out.println(p1.getXY()[1]); System.out.println(p1); MyPoint p2 = new MyPoint(0, 4); // Test another constructor System.out.println(p2); // Testing the overloaded methods distance() System.out.println(p1.distance(p2)); // which version? System.out.println(p2.distance(p1)); // which version? System.out.println(p1.distance(5, 6)); // which version? System.out.println(p1.distance()); // which version?

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter9: Advanced Array Concepts
Section: Chapter Questions
Problem 14RQ
icon
Related questions
Question

Write a Java program for a class called MyPoint, which models a 2D point with x and y coordinates, is designed as shown in the class diagram.

It contains:

  • Two instance variables x (int) and y (int).
  • A default (or "no-argument" or "no-arg") constructor that construct a point at the default location of (0, 0).
  • A overloaded constructor that constructs a point with the given x and y coordinates.
  • Getter and setter for the instance variables x and y.
  • A method setXY() to set both x and y.
  • A method getXY() which returns the x and y in a 2-element int array.
  • A toString() method that returns a string description of the instance in the format "(xy)".
  • A method called distance(int x, int y) that returns the distance from this point to another point at the given (x, y) coordinates, e.g.,MyPoint p1 = new MyPoint(3, 4); System.out.println(p1.distance(5, 6));
  • An overloaded distance(MyPoint another) that returns the distance from this point to the given MyPoint instance (called another), e.g.,MyPoint p1 = new MyPoint(3, 4); MyPoint p2 = new MyPoint(5, 6); System.out.println(p1.distance(p2));
  • Another overloaded distance() method that returns the distance from this point to the origin (0,0), e.g.,MyPoint p1 = new MyPoint(3, 4); System.out.println(p1.distance());

You are required to:

  1. Write the code for the class MyPoint.
  2. Write unit-tests in Java class MyPointTest.

// Test program to test all constructors and public methods

MyPoint p1 = new MyPoint(); // Test constructor

System.out.println(p1); // Test toString()

p1.setX(8); // Test setters

p1.setY(6);

System.out.println("x is: " + p1.getX()); // Test getters

System.out.println("y is: " + p1.getY());

p1.setXY(3, 0); // Test setXY()

System.out.println(p1.getXY()[0]); // Test getXY() System.out.println(p1.getXY()[1]);

System.out.println(p1);

MyPoint p2 = new MyPoint(0, 4); // Test another constructor System.out.println(p2); // Testing the overloaded methods distance() System.out.println(p1.distance(p2)); // which version? System.out.println(p2.distance(p1)); // which version? System.out.println(p1.distance(5, 6)); // which version? System.out.println(p1.distance()); // which version?

 

МyPoint
Return a 2-element int array of {x,y}.
-x:int = 0
-у:int
+МуРoint ()
|+MyPoint (x:int,y:int)
+getX():int
+setX(x:int): void
+getY():int
+setY(y:int): void
|+getXY():int[2]
+setXY(x:int,y:int):void
+toString():String
+distance(x:int,y:int):double
+distance(another:MyPoint):double
+distance(): double
"(х,у)"
Distance from this point to the given
point at (x,y).
Distance from this point to the given
instance of MyPoint.
Distance from this point to (0,0).
Transcribed Image Text:МyPoint Return a 2-element int array of {x,y}. -x:int = 0 -у:int +МуРoint () |+MyPoint (x:int,y:int) +getX():int +setX(x:int): void +getY():int +setY(y:int): void |+getXY():int[2] +setXY(x:int,y:int):void +toString():String +distance(x:int,y:int):double +distance(another:MyPoint):double +distance(): double "(х,у)" Distance from this point to the given point at (x,y). Distance from this point to the given instance of MyPoint. Distance from this point to (0,0).
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
ADT and Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,