Now you will put all of your new Java skills to use to create a model of a Train. Once you complete this code, you can submit it to me for validation.
After you've finished this part, this is how you'll be able to use your code:
//start the first train int numFreightCars = 50; Train train = new Train(); //add two locomotives to the train train.addCar(new Locomotive(1400000)); train.addCar(new Locomotive(1400000)); //add all the freight cars for (int i = 0; i < numFreightCars; i++) { train.addCar(new FreightCar(30000)); } System.out.println(train.readyToGo()); // should print: false //add the caboose train.addCar(new Caboose()); System.out.println(train.readyToGo()); // should print: true //make a second train numFreightCars = 200; train = new Train(); train.addCar(new Locomotive(1400000)); train.addCar(new Locomotive(1400000)); for (int i = 0; i < numFreightCars; i++) { train.addCar(new FreightCar(30000)); } train.addCar(new Caboose()); System.out.println(train.readyToGo()); // should print: false
TrainCar
TrainCar.java
, with a public abstract class of the same name.mass
. This represents the mass of the train car.isLocomotive
, which takes no parameters and always returns false. This may not seem helpful, but we'll override this in a subclass.isCaboose
, which does the same thing: it takes no parameters and always returns false.getMass
, which returns the mass of this train car.getTowingCapacity
, which takes no parameters and always returns 0.Caboose
Caboose.java
, with a public concrete class that inherits from TrainCar
.isCaboose
method so that this always returns true instead of false.Locomotive
Locomotive.java
, with a public concrete class that also inherits from TrainCar
.towingCapacity
.isLocomotive
method so that this always returns true instead of false.getTowingCapacity
method to return the towing capacity.FreightCar
FreightCar.java
, with a public concrete class that inherits from TrainCar
.Train
Train.java
, with a public class of the same name.ArrayList<TrainCar>
field: cars
. This will keep track of all the cars in this train. (You'll need to remember to import java.util.*
.)TrainCar
objects with no elements.addCar
that takes a single parameter, a TrainCar
, and adds it to the end of the train.hasCabooseOnEnd
that returns true if (and only if) the last car on the train is a caboose. (The zeroeth car on the train is at the front of the train.)allLocomotivesInFront
that returns true if (and only if) all the locomotives are at the front of the train. (This is done to reduce noise to the later train cars.) Hint: I did this by first calculating the number of locomotives on the entire train.getMass
. This should return the total mass of all the train cars.getTowingCapacity
. This should return the total towing capacity of the entire train. (For our benefits, just sum up the towing capacity of all the cars (locomotives).readyToGo
. This should return true exactly when all three of these things are true:
Please test your code to make sure it works! Use the code I wrote above, but also write some additional tests.