Java Innerclass Practice Exercise For this example we will b

Java Inner-class Practice Exercise


For this example we will be creating a class to manage a Cartesian coordinate plane, also called Euclidean space. We want to be able to store and manage ordered pairs of the form: (x, y).
To solve this problem we will create two classes: EuclideanSpace and Point. Point will be a public – static inner class within EuclideanSpace. Your two classes should be designed according to the following class diagrams:

EuclideanSpace
- points : Point[]

//adds a point to the points array
+ addPoint(x : int, y : int) : boolean
//prints a point at the given index in the points array
+ printPoint(index : int) : void


Point
- x : int
- y : int
+ Point(x : int, y : int)
+ toString() : String


Restrictions
1. You can store no more than 10 points in your grid.
2. You must check for the case where you add more than 10 points and throw an ArrayIndexOutOfBoundsException.
3. You must check for the case where the user gives you a bad index for your printPoint() method and throw an ArrayIndexOutOfBoundsException.
Driver Class
Write a test class that uses your two classes and shows them working correctly.

Solution

Hi, Please find my implementation.

Please let me know in case of any issue.

public class EuclideanSpace {

   // inner class

   public static class Point{

       private int x;

       private int y;

       public Point(int x, int y){

           this.x = x;

           this.y = y;

       }

       public String toString(){

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

       }

   }

  

   // instance variable of EuclideanSpace class

   private Point[] points;

   private int N;

  

   public EuclideanSpace() {

       points = new Point[10];

       N = 0;

   }

   boolean addPoint(int x ,int y ){

      

       if(N == points.length)

           throw new ArrayIndexOutOfBoundsException();

       points[N++] = new Point(x,y);

      

       return true;

   }

  

   public void printPoint(int index) {

      

       if( index < 0 || index >= N){

           throw new ArrayIndexOutOfBoundsException();

       }

      

       System.out.println(points[index]);

   }

}

public class EuclideanSpaceTest {

  

   public static void main(String[] args) {

      

       EuclideanSpace space = new EuclideanSpace();

      

       space.addPoint(3, 4);

       space.addPoint(1, 2);

       space.printPoint(0);

       space.addPoint(5, 6);

       space.addPoint(3, 3);

       space.printPoint(2);

       space.printPoint(3);

       space.printPoint(4);

   }

}

/*

Sample run:

(3,4)

Exception in thread \"main\" (5,6)

(3,3)

java.lang.ArrayIndexOutOfBoundsException

   at year_2017.faburary.fourthweek.EuclideanSpace.printPoint(EuclideanSpace.java:38)

   at year_2017.faburary.fourthweek.EuclideanSpaceTest.main(EuclideanSpaceTest.java:16)

*/

Java Inner-class Practice Exercise For this example we will be creating a class to manage a Cartesian coordinate plane, also called Euclidean space. We want to
Java Inner-class Practice Exercise For this example we will be creating a class to manage a Cartesian coordinate plane, also called Euclidean space. We want to
Java Inner-class Practice Exercise For this example we will be creating a class to manage a Cartesian coordinate plane, also called Euclidean space. We want to

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site