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?

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education