Write a java code for below instructions: (Grid.java) Grid ? public Grid(int xSi
ID: 3919120 • Letter: W
Question
Write a java code for below instructions: (Grid.java)
Grid
? public Grid(int xSize, int ySize, int [] water, int [][] road, int [] airport1, int [] airport2)
? A constructor for grid with parameters for the size of the grid, the location of the left, right, up, and down boundaries of the body of water (as a four element array), the location of the road as an array of (x,y) pairs, and the location of the two airports as two element arrays.
? public int getXSize()
? A getter method that returns the X axis size of the grid.
? public int getYSize()
? A getter method that returns the Y axis size of the grid
? public int getWaterLeft()
? A getter method that returns the left boundary of the body of water. getWaterLeft() would return 5 in our example.
? public int getWaterRight()
? A getter method that returns the right boundary of the body of water. getWaterRight() would return 5 in our example.
? public int getWaterUp()
? A getter method that returns the up boundary of the body of water. getWaterUp() would return 34 in our example.
? public int getWaterDown)
? A getter method that returns the down boundary of the body of water. getWaterDown() would return 30 in our example.
? public int [] getRoadLeft()
? A getter method that returns the (x,y) coordinates for the leftmost element of the road ((20,15) in the example).
? public int [] getRoadRight()
? A getter method that returns the (x,y) coordinates for the rightmost element of the road ((25, 23) in the example).
? public int [] getRoad(int i)
? A getter method that returns the (x, y) coordinates for the ith element of the road, zero-based. getRoad(4) would return (22, 17).
? public int getRoadIndex(int [] coordinates)
? A getter method that returns the index (zero-based) for the road element with the (x,y) coordinates or -1 if the coordinates are not part of the road.
? public int getRoadLength()
? Returns the number of elements in the road.
? public int [] getAirport1()
? A getter method that returns the (x,y) coordinates for airport 1 in the grid.
? public int [] getAirport2()
? A getter method that returns the (x,y) coordinates for airport 2 in the grid.
[You should implement exceptions for the constructors and the setXloc and setYLoc methods to call. You may call them anything you like, but match the standard conventions for exception names. They should be checked exceptions. ]
Explanation / Answer
Please find the code below.
CODE
==============
class LocationException extends Exception {
public LocationException(String message) {
System.out.println("Exception caught : " + message);
}
}
public class Grid {
private int xSize, ySize;
private int[] water, airport1, airport2;
private int[][] road;
public Grid(int xSize, int ySize, int [] water, int [][] road, int [] airport1, int [] airport2) throws LocationException {
if(xSize < 0)
throw new LocationException("Invalid input for X axis size!!");
else {
this.xSize = xSize;
}
if(ySize < 0)
throw new LocationException("Invalid input for Y axis size!!");
else {
this.ySize = ySize;
}
this.water = water;
this.road = road;
this.airport1 = airport1;
this.airport2 = airport2;
}
public int getXSize() {
return xSize;
}
public int getYSize() {
return ySize;
}
public int getWaterLeft() {
return water[0];
}
public int getWaterRight() {
return water[1];
}
public int getWaterUp() {
return water[2];
}
public int getWaterDown() {
return water[3];
}
public int [] getRoadLeft() {
return road[0];
}
public int [] getRoadRight() {
return road[road.length-1];
}
public int [] getRoad(int i) {
return road[i];
}
public int getRoadIndex(int [] coordinates) {
boolean found = true;
for(int i=0; i<road.length; i++) {
if(road[i].length != coordinates.length) {
found = false;
continue;
}
for(int j=0; j<road[i].length; j++) {
if(road[i][j] != coordinates[j]) {
found = false;
}
}
if(found) {
return i;
}
}
return -1;
}
public int getRoadLength() {
return road.length;
}
public int [] getAirport1() {
return airport1;
}
public int [] getAirport2() {
return airport2;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.