Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Programming Assignment: In this programming assignment, you'll write a small simulation that involves three types of entities: boats, cars, and airplanes. These three entities

Java Programming Assignment:

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).:

image text in transcribed

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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions