Labs
We will be adding to the vehicles.Vehicle hierarchy in this lab. If you did not finish the Vehicle, Automobile, Truck, or Boat classes, use the classes in ...solutions.vehicles.
- Create a subclass of
VehiclecalledFlyingVehiclethat contains fields for - Air speed
- Range (which is how far it can fly before running out of fuel).
Generate a constructor to set all fields, and getters for the fields.
(Solution: FlyingVehicle.java)
- Create a subclass of
FlyingVehiclecalledPlanethat has fields for - Name
- Passenger capacity
Generate one constructor to set all Plane, FlyingVehicle, and Vehicle fields; a getter for each new field; and a toString() method.
(Solution: Plane.java)
- Create a class called
TravelingTestwith a staticmainmethod. - Add a field for an array of
Vehicleobjects toTravelingTest. - In the
TravelingTestconstructor, initialize the array to hold8vehicles.- Create one
Automobileobject, and add it to the array. - Create three
Planeobjects, with passenger capacities of5,75, and144. Add them to the array.
- Create one
- Add a
runmethod. Create an instance of the class inmainand callrunto start the program.
TravelingTest functionality is below. Note that it is better to have more methods than fewer.
1. Prompt a user for the number of passengers who will be flying.
2. If the number of passengers is 4 or fewer, the user will travel in the Automobile. Print its information to the screen using toString().
3. If there are 5 or more passengers, choose the appropriate Plane to accommodate all passengers. (Note that you will have to use casting).
4. If the number of passengers is larger than any available plane, print a message to the screen.
5. Test your program using small and large numbers.
(Solution: TravelingTest.java)