Assignment 4: Abstracting Over Vehicles
Goals: Learn to design abstract classes and class hierarchies.
Instructions
This is a standalone variant of Assignment 4.
Be very, very careful with naming. The solution files expect your submissions to use the class, interface, field, method, constructor, argument, and file names specified here.
The submission will be organized as follows:
Homework 4 Problem 1: The Vehicle.java file
1 Abstracting Over Data Definitions
A mode of transportation uses a particular type of infrastructure or vehicle. Modes include air, ground, and water, while vehicles include airplanes, trains, and buses. In this problem, we will work with different vehicles.
Each vehicle has a name, passenger capacity, per passenger fare, and fuel capacity. Different kinds of vehicles can also contain additional information as shown in the supplied Vehicle.java file.
Note: only the totalRevenue() method is properly implemented. The others are stubs that currently return dummy values, so the code will compile but not yet work.
Warmup: Download the file and work out the following problems:
Make one more example of data for each of the three classes and add more tests for the totalRevenue() method (that is already defined) using them.
Design the method fuelCost which produces a double with the cost to fully fuel the vehicle. We assume airplanes consume kerosene, which costs 1.94 dollars per gallon, while trains and buses run on diesel, which costs 2.55 dollars per gallon.
Design the method perPassengerProfit, which produces a double with the profit per passenger. For this problem, the profit will be the total revenue minus the fuel cost.
Design the method format which produces a String showing the name and passenger capacity of this vehicle. The name and capacity should be separated by a comma and a space and end with a period.
Design the method sameVehicle that determines whether this vehicle is the same as the given one.
Once you have finished these methods and are confident that they work properly, save the work you have done to a separate file. Do not submit the code as written so far. The problems below are the main point of this exercise, and it will be helpful for you to preserve the code written so far as a reference against which to compare your revised code below. Again, submit only the work below.
Look at the code and identify all places where the code repeats: the opportunity for abstraction. Lift the common fields to an abstract class AVehicle. Make sure you include a constructor in the abstract class, and change the constructors in the derived classes accordingly. Run the program and make sure all test cases work as before.
- For each method that is defined in all three classes decide to which category it belongs:
The method bodies in the different classes are all different, and so the method has to be declared as abstract in the abstract class.
The method bodies are the same in all classes and it can be implemented concretely in the abstract class.
The method bodies are the same for two of the classes, but are different in one class, and therefore we can define the common body in the abstract class and override it in only one derived class.
Now, lift the methods that can be lifted and run all tests again.