Lab Assignment 9
(April 19/April 20)
Note: You can do this
lab on your own or with a partner.
The main purpose of this lab is to understand more
thoroughly some of the inheritance concepts we covered in class. For today's
lab we will work with following four classes.
- Inventory
- Vehicle
- Car
- Bicycle
The main functionalities of the classes are explained below.
Based on this, you will design and implement these classes. I will also let you
think more about the method signatures.
Vehicle
This class should abstract the main similarities between a
car and a bicycle. You should decide some of the fields and methods of this
class. All the fields of this class should be declared
private. Though there could be a lot of fields, just limit yourself
to maximum 3 or 4.
- Your
constructor should initialize all these fields.
- Amongst
other methods, this class must have a print()
method that should print information about all the fields. This method
should be public.
Car
This class should be a subtype of Vehicle class. It should
extend functionality of Vehicle class by adding fields and methods that are
more relevant to a car. Again limit yourself to just 2-3 more fields and
methods.
- Note
that you will have to explicitly call superclass
constructor to initialize fields. (Refer to lecture notes for how to do
this).
- There
should be a print method that prints complete car information, i.e.
information for both Car fields and Vehicle fields. Thus, the print method
in car should call print method from its superclass.
(Refer to lecture notes for how to do this).
Bicycle
This class should also subtype Vehicle class. It should
extend functionality of Vehicle class by adding fields and methods that are
more relevant to a bicycle. Limit yourself to just 2-3 more fields and methods.
- Note
that you will have to explicitly call superclass
constructor to initialize fields.
- There
should be a print method that prints complete bicycle information.
Inventory
The inventory class will keep track of all the vehicles.
This class should use an ArrayList to store information
about vehicles.
- There
should be just one add() method that should accept objects of both Car
and Bicycle Type.
- There
should be a list() method that would print
information about all the vehicles.Implement an if statement in the list()
method that should be able to differentiate between the two type of objects. If
the objects are of type car the method will print "This information is about a
car" and then the other information else if the objects are of type bicycle it
should print information "This information is about a bicycle" Note that information printed should
be complete i.e. it should print information about all the fields of a
car or a bicycle and their superclass vehicle.
(Note the purpose of this method is to make sure you know how to access the
methods in a super object from the sub class, and to be able to distinguish
between the objects of different subclasses).
Your class diagram should be as shown below:
