HomepageTeaching PageResearch PageResources Page

Java Tutorial for Python Programmers

Final Exam

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.

How you'll be able to use your code

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
        

First class: TrainCar

  1. Create a new file, TrainCar.java, with a public abstract class of the same name.
  2. Add a single protected integer field: mass. This represents the mass of the train car.
  3. Add a new boolean method, isLocomotive, which takes no parameters and always returns false. This may not seem helpful, but we'll override this in a subclass.
  4. Add another boolean method, isCaboose, which does the same thing: it takes no parameters and always returns false.
  5. Add an integer method, getMass, which returns the mass of this train car.
  6. Add another integer method, getTowingCapacity, which takes no parameters and always returns 0.

Next class: Caboose

  1. Create a new file, Caboose.java, with a public concrete class that inherits from TrainCar.
  2. Add a no-argument constructor that sets the mass to be 23000 (kilograms).
  3. Override the isCaboose method so that this always returns true instead of false.

Next class: Locomotive

  1. Create a new file, Locomotive.java, with a public concrete class that also inherits from TrainCar.
  2. Add a private integer field: towingCapacity.
  3. Add a constructor that takes a single integer argument specifying the towing capacity of the locomotive. The constructor should use this to set the towing capacity, and always set the mass to be 40000 (also kilograms).
  4. Override the isLocomotive method so that this always returns true instead of false.
  5. Override the getTowingCapacity method to return the towing capacity.

Next class: FreightCar

  1. Create a new file, FreightCar.java, with a public concrete class that inherits from TrainCar.
  2. Add a constructor that takes a single integer argument specifying the mass of this car. The constructor should use this to set the mass field.

Next class: Train

  1. Create a new file, Train.java, with a public class of the same name.
  2. Add a private ArrayList<TrainCar> field: cars. This will keep track of all the cars in this train. (You'll need to remember to import java.util.* .)
  3. Add a no-argument constructor. This constructor should set the cars field to be a new array-list of TrainCar objects with no elements.
  4. Add a void public method, addCar that takes a single parameter, a TrainCar, and adds it to the end of the train.
  5. Add a public boolean method, 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.)
  6. Add another public boolean method, 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.
  7. Add a public method that returns an integer, getMass. This should return the total mass of all the train cars.
  8. Add another public method that returns an integer, 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).
  9. Add a final public boolean method, readyToGo. This should return true exactly when all three of these things are true:

Test it!

Please test your code to make sure it works! Use the code I wrote above, but also write some additional tests.


Tutorial Table of Contents

  1. Intro
  2. Java Files and Classes
  3. Running Java Code
  4. Statements and Printing
  5. Comments
  6. Types and Variables
  7. Conditionals
  8. Static Methods
  9. Strings
  10. Loops
  11. Arrays
  12. ArrayLists
  13. Classes
  14. Subclasses
  15. Generic Types
  16. Javadoc
  17. Final Exam
  18. What's Next?