2D POINT XY AXIS

 A class called MyPoint, which models a 2D point with x and y coordinates, is designed as follows:

● Two instance variables x (int) and y (int).

● A default (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.

● 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

● An overloaded distance(MyPoint another) that returns the distance from this point to the given MyPoint

instance (called another)

● Another overloaded distance() method that returns the distance from this point to the origin (0,0)

Develop the code for the class MyPoint. Also develop a JAVA program (called TestMyPoint) to test all the

methods defined in the class.

 

Note:



PROGRAM:

package joda;

 

       class MyPoint {

              private int x;

              private int y;

               

              public MyPoint() {

              this.x = 0;

              this.y = 0;

              }

               

              public MyPoint(int x, int y) {

              this.x = x;

              this.y = y;

              }

               

              public void setXY(int x, int y) {

              this.x = x;

              this.y = y;

              }

               

              public int[] getXY() {

              int[] coordinates = {x, y};

              return coordinates;

              }

               

              public double distance(int x, int y) {

              int xDistance = this.x - x;

              int yDistance = this.y - y;

              return Math.sqrt(xDistance * xDistance + yDistance * yDistance);

              }

               

              public double distance(MyPoint another) {

              int xDistance = this.x - another.x;

              int yDistance = this.y - another.y;

              return Math.sqrt(xDistance * xDistance + yDistance * yDistance);

              }

               

              public double distance() {

              return Math.sqrt(x * x + y * y);

              }

              @Override

              public String toString() {

              return "(" + x + ", " + y + ")";

              }

              }

               

              class TestMyPoint {

              public static void main(String[] args) {

              MyPoint point1 = new MyPoint(); // Default constructor

              MyPoint point2 = new MyPoint(3, 4); // Overloaded constructor

               

              System.out.println("point1: " + point1);

              System.out.println("point2: " + point2);

               

              point1.setXY(5, 6);

              System.out.println("point1 after setXY: " + point1);

               

              int[] coordinates = point2.getXY();

              System.out.println("Coordinates of point2: (" + coordinates[0] + ", "

              + coordinates[1] + ")");

               

              double distance1 = point1.distance(7, 8);

              System.out.println("Distance from point1 to (7, 8): " + distance1);

               

              double distance2 = point1.distance(point2);

              System.out.println("Distance from point1 to point2: " + distance2);

               

              double distance3 = point1.distance();

              System.out.println("Distance from point1 to the origin: " +

              distance3);

              }

              }

 

OUTPUT:











No comments:

Post a Comment