Override Labs
In this lab we will use polymorphism to customize class behavior, and create methods to use this functionality.
Polymorphism/com.example.polymorphism.labs.vehicles
Vehiclehas atoString()method. Use Eclipse to add atoString()method to the subclassesBoat,Automobile, andTruck.- With a class open, choose Source (right-click or ⌘⌥S).
-
Choose Generate toString()...
* Choose the fields to include in the toString()method, and any information from superclasses (via getters).
-
Add the method
public double calculateRegistrationFee(double rate)toVehicle. - Return
0.0. This method is a stub. - Use Eclipse to override
calculateRegistrationFeeinBoat. - With a class open, choose Source (right-click or ⌘⌥S).
-
Choose Override/Implement Methods...
* Choose the methods to override from superclasses.
* Functionality: if a boat is 30feet in length or more, double therateand multiply by the purchasePrice. Otherwise multiply by therateby the purchasePrice. 4. OverridecalculateRegistrationFeeinAutomobile. * Functionality: multiply therateby the purchasePrice. 5. OverridecalculateRegistrationFeeinTruck. * Functionality: if the numberOfWheels is6or more, multiplyrateby1.5and multiply by the purchasePrice. Otherwise multiply therateby the purchasePrice.
We will use JUnit tests to verify our calculateRegistrationFee methods work. Note that these classes belong in the test source directory.
1. test/com.example.polymorphism.labs.vehicles.VehicleTests
* Uncomment the assertEquals(double, double, double) method call.
* Run As->JUnit Test to verify that Vehicle functions as expected.
2. test/com.example.polymorphism.labs.vehicles.AutomobileTests
* Uncomment the test method and run the class. Fix any bugs in your Automobile code.
3. test/com.example.polymorphism.labs.vehicles.BoatTests
* Uncomment the existing test method and run the class.
* Add an appropriately named method to test if rate times two times the purchasePrice is returned for lengths greater than or equal to 30.
4. Create new JUnit Test Case TruckTests.
* Add a method to test calculateRegistrationFee returns rate times purchasePrice for under six wheels.
* Add a method to test calculateRegistrationFee returns rate times purchasePrice times 1.5 for six or more wheels.
The last task is to create a method to calculate the registration fee for any vehicle.
1. com.example.polymorphism.labs.vehicles.VehicleApp
* Add a method calculateVehicleRegistration that has a Vehicle parameter and returns a double.
* Functionality: calculate the Vehicle's registration fee with the rate 0.05.
* Calculate the registration fee for each Vehicle, and print the purchase price and registration fee.