Question: Problem 1: Understanding and using inheritance 10 points total; individual-only Imagine that you wanted to capture information about a collection of different types of vehicles

Problem 1: Understanding and using inheritance

10 points total; individual-only

Imagine that you wanted to capture information about a collection of different types of vehicles (cars, trucks, motorcycles, etc.). To do so, you could use inheritance to create a collection of related classes whose inheritance hierarchy looks like this:

We have provided an implementation of most of these classes in this folder, and we encourage you to review the provided files. They include:

a class called Vehicle that defines the state (fields) and behaviour (methods) that are common to all vehicles; it does not explicitly extend another class

a class called Motorcycle that can be used to create objects that represent motorcycles; it inherits all of its fields and methods from Vehicle, with the exception of its own constructor

a class called Automobile that can be used to create objects that represent cars; it also inherits fields and methods fromVehicle, but it adds some new fields and methods that are only needed by cars, and it overrides the inherited toString()method with its own version

a class called Truck that can be used to create objects that represent trucks; it also inherits fields and methods fromVehicle, but it adds some new fields and methods that are only needed by trucks, and it overrides the inherited toString()method with its own version

a class called Taxi that can be used to create objects that represent cars; it inherits its fields and methods fromAutomobile, but it adds some new fields and methods that are only needed by taxis, and it overrides the inherited toString()method with its own version

a class called TractorTrailer that can be used to create objects that represent tractor trailors; it inherits its fields and methods from Truck, but it adds some new fields and methods that are only needed by tractor trailers, and it overrides the inherited getNumAxles() method with its own version.

The hierarchy diagram above includes a Limousine class and aMovingVan class, but we have not defined those classes yet.

In the Problem 1 section of your ps4_partI document (see above), answer the following questions:

1. (1 point) Which of the classes that are shown in the diagram above are a subclass of the Automobile class?

2. (1 point) If we have a Motorcycle object that has been assigned to a properly declared variable m, is it possible to make the following call?

m.getNumSeats() 

Explain briefly why or why not.

3. Write a definition for the MovingVan class that takes full advantage of inheritance.

A MovingVan object should have all of the same state and behavior as a Truck object. In addition, it should maintain additional state that keeps track of:

- the distance from the ground to the floor of the cargo area (a positive integer)

- whether it has a ramp (true or false)

When a MovingVan object is printed, we want to see its capacity, its distance to the cargo area, and whether it has a ramp. For example:

capacity = 10000, distance to cargo = 5, has a ramp 

In your ps4_partI file, add a definition of this class that includes the following:

a. (1 point) a class header that sets up the appropriate inheritance

b. (2 points) appropriate fields i.e., whatever fields are needed to capture the state of a MovingVan object. Make sure that you take inheritance into account when deciding which fields to include.

c. (2 points) a constructor that takes as parameters the make, model, year, number of wheels, and distance to the cargo area, and a boolean value indicating whether the moving van has a ramp. The constructor should ensure that the object is not put in an invalid state (throwing an exception as needed), and it should take whatever steps are needed to initialize the object.

d. (3 points) any necessary methods. It should be possible for a client to obtain the value of any of the fields, and to change the value of any field that can be changed in a Truck object. However, you should assume that the portion of the state specifying the distance to the cargo area and the presence or absence of a ramp will never change, and thus mutator methods are not needed for those fields. You also dont need to define an equals method for this class.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!