Fields and Methods Labs

This is an extended drill that we will use for the rest of the chapter.

We will create a class hierarchy for Vehicles.

  1. Create the package com.example.inheritance.labs.vehicles.
  2. Create a Vehicle class
  3. Fields
    • protected double purchasePrice
  4. Add a no-arg constructor.
  5. Add a one-arg constructor for the field.
  6. Add a method public String toString() to return a String containing the class name, and field names and values.
  7. Create a Automobile class which extends Vehicle
  8. Fields
    • protected String make
    • protected String model
    • protected int year
    • protected int numberOfWheels
    • protected double speedInMph
  9. Add a no-arg constructor.
  10. Add a six-arg constructor for the fields of this class and Vehicle. Set each field in the constructor.
  11. Add a method public String toString() to return a String containing the class name, and all six fields and values, starting with the field in Vehicle.
  12. Create a Boat class which extends Vehicle
  13. Fields
    • protected String name
    • protected double speedInKnots
    • protected int lengthInFeet
  14. Add a no-arg constructor.
  15. Add a four-arg constructor.
  16. Add a method public String toString() to return a String containing the class name, and all field names and values.
  17. Create a Truck class which extends Automobile.
  18. Fields
    • protected int bedSizeInCubicFeet
  19. Add a no-arg constructor.
  20. Add an seven-arg constructor for fields of this class, Automobile, and Vehicle.
  21. Create a VehicleTestApp class that you can run to test Vehicles.
  22. Create one Automobile, Boat, and Truck using the multi-arg constructors.
  23. Call each object's toString() method, and print the String to the screen. Verify the classes work as expected.

Check your class structure against this UML diagram.

Vehicles UML

(Solution: Vehicle.java, Automobile.java, Truck.java, Boat.java, VehicleTestApp.java)


Prev -- Up