YCP Logo Lab 7: Inheriting Fields and Methods

Due: Tuesday, September 23th by 11:59 PM

Start with your solution to Lab 6. You can download your solution from the server if you don't already have it in your workspace.

Rename the CS201_Lab6 project to CS201_Lab7 by right-clicking on it in the Solution Explorer, choosing Copy, and then choosing Edit->Paste from the menu bar. Enter CS201_Lab7 as the name of the new copy. Make your changes to this new project.

In this lab, you will add the capability to the classes in the Vehicle class hierarchy to represent the vehicle's maximum speed, and use this feature to add a capability to the Trip class to determine a vehicle's average speed on a particular trip.

Your Task

Make the following modifications:

(1) Add a maxSpeed field, with type double, in the Vehicle class. Add a constructor to Vehicle that takes a single double parameter and uses its value to initialize the maxSpeed parameter.

(2) Add a getMaxSpeed accessor method to Vehicle to return the Vehicle's maximum speed.

(3) Add constructors to Car, Boat, and Airplane taking a single double parameter. Each constructor should call the superclass constructor, passing the parameter value as the argument.

(4) Add the following abstract method to the Vehicle class:

public abstract double getSpeed(Terrain t);

This method returns the speed of the Vehicle over the given kind of terrain. You will need to implement this method in each subclass of Vehicle.

Here are the rules for different kinds of vehicles:

  • A Car moves at its maximum speed over ROAD terrain, and at one-quarter of its maximum speed over AIRPORT and MARINA terrain.
  • A Boat moves at its maximum speed over WATER, and at one-quarter of its maximum speed over MARINA.
  • An Airplane moves at its maximum speed over all kinds of terrain.

If the getSpeed method is called with a terrain type that is not legal for a particular kind of Vehicle, you can throw an IllegalArgumentException:

throw new IllegalArgumentException("Illegal terrain: " + t);

(5) Your JUnit test classes will no longer compile because they assume that Car, Boat, and Airplane objects can be created without passing any arguments to the constructor. Change these tests so that:

  • The Car object has a maximum speed of 100.
  • The Boat object has a maximum speed of 50.
  • The Airplane object has a maximum speed of 500.

Re-run all of the tests to make sure they still pass.

(6) Add the following method to the Trip class:

public double findAverageSpeed(Vehicle v) {

This method determines the average speed of a given Vehicle taking a trip by calling the vehicle object's getSpeed method for each terrain type in the trip, and computing the average speed.

(7). Add JUnit tests to CarTest, BoatTest, and AirplaneTest to test the getSpeed and findAverageSpeed methods.

For example:

// in CarTest

private static final double DELTA = 0.1;

private Trip exampleTrip;
private Car myCar;

protected void setUp() {
  exampleTrip = new Trip(3);
  exampleTrip.setHop(0, Terrain.AIRPORT);
  exampleTrip.setHop(1, Terrain.ROAD);
  exampleTrip.setHop(2, Terrain.MARINA);

  myCar = new Car(100.0); // the Car's maximum speed is 100

public void testGetSpeed() {
  // full speed over road
  assertEquals( 100.0, myCar.getSpeed(Terrain.ROAD), DELTA );

  // one-quarter speed through an airport
  assertEquals( 0.25 * 100.0, myCar.getSpeed(Terrain.AIRPORT), DELTA );

  // one-quarter speed through a marina
  assertEquals( 0.25 * 100.0, myCar.getSpeed(Terrain.MARINA), DELTA );

public void testFindAverageSpeed() {
  // exampleTrip is AIRPORT, ROAD, MARINA
  assertEquals( ((0.25 * 100) + 100 + (0.25 * 100)) / 3,
                DELTA );

Note that the JUnit assertEquals method takes a third parameter, which is a "slop factor" to account for the fact that floating-point arithmetic may introduce small errors. In the code above, we define a constant double value called DELTA to allow the expected and actual values compared to differ by a small amount, but stil be considered "equal".


When you are done, save the project (CS201_Lab7) to a zip file by right-clicking it and choosing

Export...->Archive File

Upload the saved zip file to the Marmoset server as lab7. The server URL is