Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 3: 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 3: 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 inthis folder, and we encourage you to review the provided files. They include:

a class calledVehiclethat defines thestate(fields) andbehavior(methods) that are common to all vehicles; it does not explicitly extend another class

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

a class calledAutomobilethat 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 inheritedtoString()method with its own version

a class calledTruckthat 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 inheritedtoString()method with its own version

a class calledTaxithat 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 inheritedtoString()method with its own version

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

The hierarchy diagram above includes aLimousineclass and aMovingVanclass, but we have not defined those classes yet.

In the appropriate section of yourps3_partIdocument (see above), answer the following questions:

1. (1 point) Which of the classes that are shown in the diagram above are asuperclassof theTractorTrailerclass?

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

t.getMileage()

Explain briefly why or why not.

3. Write a definition for theLimousineclass that takes full advantage of inheritance.

ALimousineobject should have all of the same state and behavior as anAutomobileobject. In addition, it should maintain additional state that keeps track of:

  • whether the limousine has a sun roof (true or false)
  • how many bottles of champagne it can chill (a non-negative integer)

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

B. (2 points) whatever new fields are needed to capture the state of aLimousineobject. 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, and total number of seats, as well as a boolean value indicating whether the limo has a sun roof and an integer indicating how many bottles of champagne it can chill. 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 anAutomobileobject. However, you should assume that the portion of the state specifying whether there is a sun roof and how many champagne bottles can be chilled will never change, and thus mutator methods are not needed for those fields. You also don't need to define anequalsmethod for this class.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Basic Marketing Research

Authors: Tom J. Brown, Tracy A. Suter, Gilbert A. Churchill

8th edition

1133188540, 978-1111525293, 1111525293, 978-1305178571, 1305178572, 978-1133188544

More Books

Students also viewed these Programming questions