Write a java code for below instruction: In this programming assignment, you\'ll
ID: 3914905 • Letter: W
Question
Write a java code for below instruction:
In this programming assignment, you'll write a small simulation that involves three types of entities: boats, cars, and airplanes. These three entities are, of course, types of vehicles, which can move at specific speeds, but only in left-right, up-down directions on a rectangular grid. Boats are special vehicles that can travel only on water. Cars are special vehicles that can travel only on roads. Airplanes are special vehicles that can only travel between two airports.
The vehicles should be implemented as a class hierarchy, with an abstract base class Vehicle, which keeps track of the vehicle's speed and (x, y) location on the grid. The Vehicle class has a constructor that initializes the vehicle's speed and location on the grid. The vehicle class has getter methods for its internal data items, but no setter methods. The Vehicle class also has a move() method, which is abstract.
Boat, Car, and Airplane are sub-classes of Vehicle. The Boat class has an internal data item which defines the boats' direction of travel (either left, right, up or down). The Car has an internal data item which also defines the car's direction of travel (either left-up or right-down). The Airplane has two internal data items which are the coordinates of the origin and destination airports.
The rectangular grid is also represented as a class, called Grid. It's internal data are the size of the grid as an (x, y) pair, the location of a body of water (as a pair of x values representing the left and right boundaries and a pair of y values representing the up and down boundaries. the location of a road (an array of contiguous (x,y) values), and the locations of two airports as (x, y) values. The Grid class has a constructor to initialize the grid and its other component. See the figure below for an example (Note: you do not have to implement the grid graphically; you need only use Java arrays, although the ArrayList class could be used to keep track of the roads).:
The figure shows a 55 x 40 grid. The boundaries of the blue body of water are: left 5, right 14, up 34, down 30. The sequence of locations that form the red road are (20, 15), (21, 15), (22, 15), (22, 16), (22, 17), (22, 18), (23, 18), (24, 18), (24, 19), (24, 20), (24, 21), (24, 22), (24, 23), and (25, 23). The green airports are located at (5, 5) and (40, 30). The size of the grid, the location of the body of water, the road, and the airports are initialized using parameters given to the Grid class constructor. You to not need to verify that the locations for the road are contiguous; your Grid class can assume this. You may also assume that the airports, the road and the body of water do not overlap. Also assume that road will always move right and up from their initial left-most position.
Below are the required interfaces for the four classes described above:
Vehicle: abstract base class
public Vehicle(Grid grid, int speed, int xLoc, int yLoc)
An implemented constructor that initializes the speed and location components of the vehicle, along with the grid to be used.
public Grid getGrid()
A getter method for the grid.
public int getSpeed()
A getter method for the vehicle's speed.
public int [] getLoc()
A getter method for the vehicle's location, with the X coordinate at index 0 of the returned array and the Y coordinate at index 1
public void setXLoc(int x)
A setter method for the X coordinate. Should throw an exception if the value is outside the grid boundaries.
public void setYLoc(int y)
A setter method for the Y coordinate. Should throw an exception if the value is outside the grid boundaries.
public abstract void move() throws Exception
An abstract method to be implemented in the base classes. The implementations will probably use functions (setXLoc() and setYLoc() of Vehicle) that throw exceptions; if the abstract method does not declare that it throws exceptions, the implementation in the sub-classes can't either.
public String toString()
An override of the toString method that returns a String version of the Vehicle data.
Boat sub-class of Vehicle
public Boat(Grid grid, int speed, int xLoc, int yLoc, int direction)
A constructor for boat. The direction parameter sets the direction of the boat: 0 for left, 1 for up, 2 for right, and 3 for down. Boat checks that the speed is in the range 1 to 3, inclusive and throws an exception otherwise. You may assume that the location given in the parameters is in the body of water.
public String getDirectionString()
A getter method for the boat's direction, returned as a String for convenience.
public int getDirection()
A getter metod for the boat's direction as the stored integer.
public void move()
Boat's implementation of move (see below).
public String toString()
An override of the toString method that returns a String version of the Boat data (including the Vehicle instance).
Car sub-class of Vehicle
public Car(Grid grid, int speed, int xLoc, int yLoc, int direction)
A constructor for Car, The direction parameter sets the direction of the car: 0 for right-up and 1 for left-down. Car checks that the speed is between 3 and 8, inclusive and throws an exception otherwise. You may assume that the location given is one of the road elements.
public String getDirectionString()
A getter method for the boat's direction, returned as a String for convenience.
public int getDirection()
A getter method for the boat's direction as the stored integer.
public void move()
Car's implementation of move (see below).
public String toString()
An override of the toString method that returns a String version of the Car data (including the Vehicle instance).
Airplane sub-class of Vehicle
public Airplane(Grid grid, int speed, int xLoc, int yLoc, int [] origin, int [] destination)
A constructor for Airplane. The last two parameters are for the (x,y) coordinates of the origin and destination airports. Airplane checks that the speed is between 10 and 15, inclusive, and throws an exception otherwise. You may assume that the origin and destination parameters contain the coordinates of the airports in the grid and that they are different.
public int [] getOrigin()
A getter method for the coordinates of the origin airport.
public int [] getDestination()
A get method for the coordinates of the destination airport.
public void move()
Airplane's implementation of move (see below).
public String toString()
An override of the toString method that returns a String version of the Airplane data (including the Vehicle instance).
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 your own 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.
Implementations of the move() method
The move() method is implemented differently in each of the Vehicle sub-classes. This section describes the how each of the vehicle's move() method should work.
Boat: Suppose that speed is s. In each call to move(), the boat method moves s blocks in what ever direction it is traveling, unless that is past the boundary of the body of water for the direction that it is traveling. If so, the boat reverses direction, remains at the boundary. In our example, if the boat is traveling at speed 3 at (6, 7) heading down, a call to move() changes the boat's location to (6, 4), which is past the down boundary. Therefore, the location is set to (6, 5) and the direction switches to up. If the speed is 2 with the boat at (6,7), heading down, move() changes the location to (6, 5).
Car: Let the car's speed be s. In each call to move(), the car moves s road elements in the direction its traveling, unless it moves past the end of the road for the direction it is traveling. It this case, the car reverses direction at the end of the road. For example, if the is traveling left-down and the car is current at road element 3(with coordinates (22, 16) with speed 5, move() notes that this will move past the end of the road and sets the location to (20, 15) and reverses the direction to right-up. If the car is at road element 3 traveling left-down at speed 3, move() changes the location to (21, 15).
Airplane: Let the airplane's speed be s. The airplane moves s blocks on the x axis, toward the destination airport until it meets or passes the destination airport's x coordinates. Subsequent calls to move() move sblocks along the y axis until the destination airport's y coordinate is met or past. The airplane then switches the origin and destination airports. For example, suppose the speed of the airplane is 15 and the destination airport is at (40, 30) and it is starting at the airport at (5, 5). The first call to move() changes the airplane location to (20, 5). The next call to move() changes the location to (35, 5). The next call to move() changes the location to (40, 5), since destination airports x coordinate is passed. The next call to move() changes the location to (40, 15). The next call to move changes the location to (40, 30) and since the y coordinate of the destination airport is met, (5, 5) becomes the destination airport.
[ Can anyone help me to write java program for above instruction. It is console based ]
15 10Explanation / Answer
Its a very complex program. I just can;t finish everything in the given time. I have finished the boat class. Its working fine. NOT Done move() for Car(NOT DONE) I have tried to add as much comments possible in given tme. Have a look and if you have any doubt in boat class let me know.
If you have some test code put it..
//////////////////////////////////////////////////Vehicle.java////////////////////////////////////////////////////////
public abstract class Vehicle {
private int xLoc;
private int yLoc;
private int speed;
private Grid grid;
public Vehicle(Grid grid, int speed, int xLoc, int yLoc) {
this.speed = speed;
this.xLoc = xLoc;
this.yLoc = yLoc;
this.grid = grid;
}
public int[] getLoc() {
int[] loc = { xLoc, yLoc };
return loc;
}
public Grid getGrid() {
return grid;
}
public int getXLoc() {
return xLoc;
}
public int getYLoc() {
return yLoc;
}
public int getSpeed() {
return speed;
}
public void setXLoc(int x) throws Exception {
if (x > this.grid.getXSize()) {
throw new Exception("X is outside boundry of grid");
}
this.xLoc = x;
}
public void setYLoc(int y) throws Exception {
if (y > this.grid.getYSize()) {
throw new Exception("Y is outside boundry of grid");
}
this.yLoc = y;
}
@Override
public String toString() {
return "speed[" + speed + "]" + " X[" + xLoc + "] Y[" + yLoc + "]";
}
public abstract void move() throws Exception;
}
////////////////////////////////////////End Vehicle.java//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////Boat.java//////////////////////////////////////////////////////////////////////////////////
package com.java.VehicleSimulation;
public class Boat extends Vehicle {
private int direction;
private String directionString;
// Direction 0 for left, 1 for up, 2 for right, and 3 for down. Boat checks that
// the speed
// is in the range 1 to 3
public Boat(Grid grid, int speed, int xLoc, int yLoc, int direction) throws Exception {
super(grid, checkSpeed(speed), xLoc, yLoc);
this.direction = direction;
if (direction == 0) {
directionString = "left";
} else if (direction == 1) {
directionString = "up";
} else if (direction == 2) {
directionString = "right";
} else if (direction == 3) {
directionString = "down";
} else {
throw new Exception("Wrong direction of boat");
}
}
private static int checkSpeed(int speed) throws Exception {
if (speed < 1 || speed > 3)
throw new Exception("Wrong speed");
return speed;
}
@Override
// n each call to move(), the boat method moves s blocks in what ever direction
// it is traveling, unless that is past the boundary of the body of water for
// the direction that it is traveling.
public void move() throws Exception {
// if the boat is traveling at speed 3 at (6, 7) heading down, a call to move()
// changes the boat's location to (6, 4), which is past the down boundary
String dir = getDirectionString();
if (dir.equalsIgnoreCase("left")) {
moveLeft();
} else if (dir.equalsIgnoreCase("right")) {
moveRight();
} else if (dir.equalsIgnoreCase("up")) {
moveUp();
} else if (dir.equalsIgnoreCase("down")) {
moveDown();
}
}
// Move in left direction and if reach the boundary change direction to right .
// As you r going in left the speed will be subtracted from the x to move left
private void moveLeft() throws Exception {
int nextPosition = getXLoc() - getSpeed();
if (nextPosition > this.getGrid().getWaterLeft()) {
System.out.println("Move Left to =" + nextPosition);
setXLoc(nextPosition);
} else if (nextPosition == this.getGrid().getWaterLeft()) {
// Change the direction if it is on the boundary
nextPosition = getXLoc() - getSpeed();
setXLoc(nextPosition);
System.out.println("Direction change to right");
// change the direction to right now
setDirection(2);
setDirectionString("right");
} else {
nextPosition = getXLoc() + (this.getGrid().getWaterLeft() - getXLoc());
setXLoc(nextPosition);
System.out.println("Direction change to right");
// change the direction to right now
setDirection(2);
setDirectionString("right");
}
}
// Move in Right direction and if reach the boundary change direction to Left .
// As you r going in right the speed will be added to the x to move right
private void moveRight() throws Exception {
int nextPosition = getXLoc() + getSpeed();
if (nextPosition < this.getGrid().getWaterRight()) {
System.out.println("Move right to =" + nextPosition);
setXLoc(nextPosition);
} else if (nextPosition == this.getGrid().getWaterRight()) {
System.out.println("Move right nextPosition=" + nextPosition);
nextPosition = getXLoc() + getSpeed();
setXLoc(nextPosition);
System.out.println("Direction change to left");
// change the direction to left now
setDirection(0);
setDirectionString("left");
} else {
nextPosition = getXLoc() + (this.getGrid().getWaterRight() - getXLoc());
setXLoc(nextPosition);
System.out.println("Direction change to left");
// chnage the direction to left now
setDirection(0);
setDirectionString("left");
}
}
// Move in Down direction and if reach the boundary change direction to Up.
// As you r going in down the speed will be subtracted to the y to move down
private void moveDown() throws Exception {
int nextPosition = getYLoc() - getSpeed();
if (nextPosition < this.getGrid().getWaterDown()) {
System.out.println("Move down to =" + nextPosition);
setYLoc(nextPosition);
} else if (nextPosition == this.getGrid().getWaterDown()) {
System.out.println("Move down nextPosition=" + nextPosition);
nextPosition = getYLoc() - getSpeed();
setYLoc(nextPosition);
System.out.println("Direction change to up");
// chnage the direction to up now
setDirection(1);
setDirectionString("up");
} else {
System.out.println("Move Down nextPosition=" + nextPosition);
nextPosition = getYLoc() + (this.getGrid().getWaterDown() - getYLoc());
setYLoc(nextPosition);
System.out.println("Direction change to Up");
// chnage the direction to right now
setDirection(1);
setDirectionString("up");
}
}
// Move in up direction and if reach the boundary change direction to Down.
// As you r going in up the speed will be added to the y to move up
private void moveUp() throws Exception {
int nextPosition = getYLoc() + getSpeed();
if (nextPosition < this.getGrid().getWaterUp()) {
System.out.println("Move up to =" + nextPosition);
setYLoc(nextPosition);
} else if (nextPosition == this.getGrid().getWaterDown()) {
System.out.println("Move up nextPosition=" + nextPosition);
nextPosition = getYLoc() + getSpeed();
setYLoc(nextPosition);
System.out.println("Direction change to down");
// chnage the direction to down now
setDirection(3);
setDirectionString("down");
} else {
nextPosition = getYLoc() + (this.getGrid().getWaterUp() - getYLoc());
setYLoc(nextPosition);
System.out.println("Direction change to Down");
// chnage the direction to down now
setDirection(3);
setDirectionString("down");
}
}
public String getDirectionString() {
return directionString;
}
public void setDirectionString(String direction) {
this.directionString = direction;
}
public void setDirection(int direction) {
this.direction = direction;
}
public int getDirection() {
return direction;
}
@Override
public String toString() {
return super.toString() + " direction[" + directionString + "]";
}
}
/////////////////////////////////////////////ENd Boat.java/////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////Car.java////////////////////////////////////////////////////////////////////////////////
package com.java.VehicleSimulation;
public class Car extends Vehicle {
private int direction;
private String directionString;
public Car(Grid grid, int speed, int xLoc, int yLoc, int direction) throws Exception {
super(grid, checkSpeed(speed), xLoc, yLoc);
if (direction == 0) {
directionString = "right-up ";
this.direction = direction;
} else if (direction == 1) {
directionString = " left-down";
this.direction = direction;
} else {
throw new Exception("Wrong direction of Car");
}
}
private static int checkSpeed(int speed) throws Exception {
if (speed < 3 || speed > 8)
throw new Exception("Wrong speed");
return speed;
}
public String getDirectionString() {
return directionString;
}
public int getDirection() {
return direction;
}
@Override
public void move() throws Exception {
// TODO Auto-generated method stub
}
@Override
public String toString() {
return super.toString() + "direction[" + directionString + "]";
}
}
//////////////////////////////////////////////End Car.java////////////////////////////////////////////////////////
//////////////////////////////////////////////AirPlane.java//////////////////////////////////////////////////////
package com.java.VehicleSimulation;
import java.util.Arrays;
public class Airplane extends Vehicle {
private int[] origin;
private int[] destination;
public Airplane(Grid grid, int speed, int xLoc, int yLoc, int[] origin, int[] destination) throws Exception {
super(grid, checkSpeed(speed), xLoc, yLoc);
this.destination = destination;
this.origin = origin;
}
// checks if the speed is in the limit otherwise throws exception
private static int checkSpeed(int speed) throws Exception {
if (speed < 10 || speed > 15)
throw new Exception("Wrong speed");
return speed;
}
public int[] getOrigin() {
return origin;
}
public void setOrigin(int[] origin) {
this.origin = origin;
}
public void setDestination(int[] destination) {
this.destination = destination;
}
public int[] getDestination() {
return destination;
}
@Override
// airplane moves s blocks on the x axis, toward the destination airport until
// it meets or passes the destination airport's x coordinates. Subsequent calls
// to move() move s blocks along the y axis until the destination airport's y
// coordinate is met or past
public void move() throws Exception {
int originX = getOrigin()[0];
int originY = getOrigin()[1];
int destinationX = getDestination()[0];
int destinationY = getDestination()[1];
if ((destinationX - originX) > 0 && (destinationY - originY) > 0) {
// The case when we are going from less to more
moveForward(originX, originY, destinationX, destinationY);
} else {
// The case when we are going from more to less
moveBackwards(originX, originY, destinationX, destinationY);
}
}
// When we are moving towards left(from more to less)
private void moveBackwards(int originX, int originY, int destinationX, int destinationY) throws Exception {
if (getXLoc() > destinationX) {
int nextPosition = getXLoc() - getSpeed();
// if it is between the destination boundary change x location
if (nextPosition > destinationX) {
setXLoc(nextPosition);
System.out.println("X=" + nextPosition);
} else if (nextPosition < destinationX) {
// if it is outside the destination boundary add the positions till the boundary
// and then change the direction
int temp = getXLoc() - getSpeed();
int temp1 = destinationX - temp;
nextPosition = temp1 + temp;
setXLoc(nextPosition);
}
} else if (getYLoc() > destinationY) {
// Similar to X same for Y coordinate
int nextYPosition = getYLoc() - getSpeed();
// if it is between the destination boundary change only y location till we
// reach or pass the boundary
if (nextYPosition > destinationY) {
setYLoc(nextYPosition);
} else if (nextYPosition < destinationY) {
// if it is outside the destination boundary add the positions till the boundary
// and then change the direction
int temp = destinationY - getYLoc();
nextYPosition = getYLoc() + temp;
if (nextYPosition == destinationY) {
setYLoc(nextYPosition);
} else {
int nextX = getXLoc() + (getSpeed() - temp);
setYLoc(nextYPosition);
setXLoc(nextX);
}
}
// The airplane then switches the origin and destination airports
if (getXLoc() == destinationX && getYLoc() == destinationY) {
int[] newOrigin = { destinationX, destinationY };
int[] newDestination = { originX, originY };
setOrigin(newOrigin);
setDestination(newDestination);
}
}
}
// Just like previous method But the direction here is forward so speed is added
// to coordinates instead of subtract
private void moveForward(int originX, int originY, int destinationX, int destinationY) throws Exception {
if (getXLoc() < destinationX) {
int nextPosition = getXLoc() + getSpeed();
if (nextPosition < destinationX) {
setXLoc(nextPosition);
} else if (nextPosition > destinationX) {
nextPosition = getXLoc() + (destinationX - getXLoc());
setXLoc(nextPosition);
if (nextPosition == destinationX && originY == destinationY) {
setXLoc(nextPosition);
}
}
} else if (getYLoc() < destinationY) {
int nextYPosition = getYLoc() + getSpeed();
if (nextYPosition < destinationY) {
setYLoc(nextYPosition);
} else if (nextYPosition > destinationY) {
// move in y direction
int temp = destinationY - getYLoc();
nextYPosition = getYLoc() + temp;
if (nextYPosition == destinationY) {
setYLoc(nextYPosition);
} else {
int nextX = getXLoc() + (getSpeed() - temp);
setYLoc(nextYPosition);
setXLoc(nextX);
}
}
// The airplane then switches the origin and destination airports
if (getXLoc() == destinationX && getYLoc() == destinationY) {
int[] newOrigin = { destinationX, destinationY };
int[] newDestination = { originX, originY };
setOrigin(newOrigin);
setDestination(newDestination);
}
}
}
@Override
public String toString() {
return super.toString() + " origin[" + Arrays.toString(origin) + "] destination[" + Arrays.toString(destination)
+ "]";
}
}
//////////////////////////////////////ENd Airplane.java//////////////////////////////////////
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.