Innerclass Practice Exercise For this example we will be cre

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: Restrictions You can store no more than 10 points in your grid. You must check for the case where you add more than 10 points and throw an ArrayIndexOutOfBoundsException. You must check for the case where the user gives you a bad index for your printPoint() method and throw an ArrayIndexOutOfBoundsException.

Solution

Please find my implementation.

Please let me know in case ofa ny 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)

*/

 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 a
 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 a
 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 a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site