In this lab, you will gain experience working with the concepts below by implementing a program for storing data about different kinds of vehicles. Concepts are:
You are to implement the program given by the UML class diagram shown below. The program creates a Vehicle database containing 3 different kinds of vehicles: Gas, Electric and Bicycles. The circled green numbers indicate the suggested order of implementation. You will need to look up, in your text, the syntax for creating an interface, abstract, super or subclass.
Super/sub classes: Each of these vehicles share a number of different properties. The common properties are contained in the Vehicle (blue) super class.
Each subclass (Gas, Electric, Bicycle) (green) contain the properties and methods that are unique to that type of vehicle. For example, only gas powered vehicles contain a gas tank and mpg.
MaxDistance Interface: We want to be able to compare all of these vehicles based on how far they can travel without any servicing (i.e. without filling a gas tank, charging a battery, or resting a human body). This distance is computed in the distance() method. Since one must know what type of vehicle it is before calculating this distance, the distance() method is not implemented in the Vehicle class. However, we want to force all of the subclasses to implement the distance() method; We do this by having the Vehicle class implement the MaxDistance interface (yellow). The Vehicle class is said to be abstract because the distance() method is required but not implemented in the Vehicle class. It is only implemented in the subclasses.
VehicleList and polymorphism: The VehicleList class (red) contains an ArrayList of Vehicle objects. Even though Gas, Electric and Bicycle objects are all different object types, we can place them in the same ArrayList if we set the type of the ArrayList to be Vehicle, because Gas, Electric, and Bicycle objects are all subtypes of the Vehicle class. This is the concept behind polymorphism (an object can take on mutliple forms).
Sorting and the Comparable Interface: We want to be able to sort the ArrayList based on the distance(). The standard Java sort method compares objects using a compareTo() method. This is why we require that the Vehicle class implement the Comparable interface (yellow), i.e. the Comparable interface forces the Vehicle class to implement a compareTo() method. In the Javadoc, look up the Comparable interface and look to see what the compareTo method does. You must implement the comparTo() method using the distance() method.
Unit Testing: Finally, in main (not shown in the above picture), you are to create a VehicleListobject and add different types of Vehicle objects to it. For example, the code in main, could be:
public static void main(String[] args) {
VehicleList vehicles = new VehicleList();
vehicles.addVehicle(new Electric("Nissan" , "Leaf", 4, 107 ));
vehicles.addVehicle(new Electric("Chevrolet" , "Bolt", 4, 238));
vehicles.addVehicle(new Gas("Harley-Davison" , "SuperLow", 2, 51, 4.5));
vehicles.addVehicle(new Gas("Mercedez-Benz", "C 300 Sedan", 4, 33, 17.4 ));
vehicles.addVehicle(new Bicycle("Trek","Madone 9.9", 2, 11, 27 ));
vehicles.addVehicle(new Bicycle("Unicycle.com","Purple Monster", 1, 1, 27 ));
System.out.println(vehicles);
vehicles.sortList();
System.out.println("\nSorted by distance: ");
System.out.println(vehicles);
}
The output of running the above instructions should look something like output.txt
To receive full credit, you need to:
Make sure that your Java code:
Demonstrate: Your program to the instructor or lab assistant by the due date.
Submit your .java files to Wise as attachments to Lab 9.