Question: TASK Add classes called Boat and Airplane . They should have the following behavior: A legal trip for a Boat must start and end at

TASK 

Add classes called Boat and Airplane. They should have the following behavior:

A legal trip for a Boat must start and end at a marina, and must not contain any hops over terrain other than water or marina.

A legal trip for an Airplane must start and end at an airport, but may continue through any kind of terrain.

Add new JUnit test classes called BoatTest and AirplaneTest that test Boat and Airplane objects (respectively) against both legal and illegal trips.

You can use the provided Car and CarTest classes as a guide.

public enum Terrain { ROAD, AIRPORT, WATER, MARINA, FIELD, FOREST, MOUNTAIN; } public class Car extends Vehicle { @Override public boolean endTrip(Terrain t) { if ( t == Terrain.AIRPORT || t == Terrain.MARINA ) { return true; } else { return false; } } @Override public boolean move(Terrain t) { if ( t == Terrain.AIRPORT || t == Terrain.MARINA || t == Terrain.ROAD ) { return true; } else { return false; } } @Override public boolean startTrip(Terrain t) { if ( t == Terrain.AIRPORT || t == Terrain.MARINA ) { return true; } else { return false; } } } public class Trip { private Terrain[] hops; /** * Constructor. * * @param numHops number of hops (through different kinds of terrain) * that the Trip requires */ public Trip(int numHops) { if (numHops < 2) { throw new IllegalArgumentException("Trips must have at least a start and finish"); } this.hops = new Terrain[numHops]; } /** * Set one of the hops of the trip * (0 is first). * * @param hop which hop of the trip to set * @param t the kind of Terrain that this hop requires */ public void setHop(int hop, Terrain t) { hops[hop] = t; } /** * Return whether or not it is possible to make * this Trip with the given Vehicle. * * @param v a Vehicle * @return true if the trip is possible, false if not */ public boolean isTripPossible(Vehicle v) { // Check the first hop if (!v.startTrip(hops[0])) { return false; } // Check all hops between the first and last for (int i = 1; i < hops.length - 1; i++) { if (!v.move(hops[i])) { return false; } } // Check the last hop if (!v.endTrip(hops[hops.length - 1])) { return false; } // success! return true; } } public abstract class Vehicle { /** * Is it possible for this Vehicle to start a trip * at the given kind of Terrain? * * @param t a kind of Terrain * @return true if this Vehicle can start at the kind of Terrain, * false otherwise */ public abstract boolean startTrip(Terrain t); /** * Is it possible for this Vehicle to end a trip * at the given kind of Terrain? * * @param t a kind of Terrain * @return true if this Vehicle can end at the kind of Terrain, * false otherwise */ public abstract boolean endTrip(Terrain t); /** * Is it possible for this Vehicle to continue a trip * at the given kind of Terrain? * * @param t a kind of Terrain * @return true if this Vehicle can continue at the kind of Terrain, * false otherwise */ public abstract boolean move(Terrain t); } 

CARTEST

import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; public class CarTest { private Trip legalTrip; private Trip illegalTrip; private Car myCar; @Before public void setUp() throws Exception { // a Trip that can be completed by Car legalTrip = new Trip(4); legalTrip.setHop(0, Terrain.AIRPORT); legalTrip.setHop(1, Terrain.ROAD); legalTrip.setHop(2, Terrain.ROAD); legalTrip.setHop(3, Terrain.MARINA); // a Trip that cannot be completed by Car // because it contains a hop through WATER illegalTrip = new Trip(5); illegalTrip.setHop(0, Terrain.WATER); illegalTrip.setHop(1, Terrain.ROAD); illegalTrip.setHop(2, Terrain.ROAD); illegalTrip.setHop(3, Terrain.WATER); // Not possible by Car! illegalTrip.setHop(4, Terrain.MARINA); myCar = new Car(); } @Test public void testLegalTrip() throws Exception { assertTrue(legalTrip.isTripPossible(myCar)); } @Test public void testIllegalTrip() throws Exception { assertFalse(illegalTrip.isTripPossible(myCar)); } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!