Labsheet - 5 - Inheritance
Labsheet - 5 - Inheritance
Labsheet 5: Inheritance
Question 1
Create a class named Computer that contains two integer data fields for processor model (for
example, 486) and clock speed in megahertz (for example, 166). Include a get method for
each field and a constructor that requires a parameter for each field. Create a subclass named
MultimediaComputer that contains an additional integer field for the CDROM speed. The
MultimediaComputer class also contains a get method for the new data field and a constructor
that requires arguments for each of the three data fields. Write a program to demonstrate
creating and using an object of each class.
Question 2
Create a class named HotelRoom that includes an integer field for the room number and a
double field for the nightly rental rate. Include get methods for these fields, and a constructor
that requires an integer argument representing the room number. The constructor sets the
room rate based on the room number; rooms numbered 299 and below are $69.95 per night,
others are $89.95 per night. Create an extended class named Suite whose constructor requires
a room number and adds a $40.00 surcharge to the regular hotel room rate based on the room
number. Write a program to create an array of HotelRoom with 3 HotelRoom objects and 2
Suite objects. The program calculates and displays the total room rate of all the 5 rooms.
Question 3
Create a class named Package with data fields for weight in ounces, shipping method, and
shipping cost. The shipping method is a character: A for Air, T for Truck, or M for Mail. The
Package class contains a constructor that requires arguments for weight and shipping method.
The constructor calls a calculateCost() method that determines the shipping cost based on the
following:
Shipping Methods ($)
Weight (lb) Air Truck Mail
1 to 8 2.00 1.50 0.50
9 to 16 3.00 2.35 1.50
17 and over 4.50 3.25 2.15
The Package class also contains a display() method that displays the values in all four fields.
Create a subclass named InsuredPackage that adds an insurance cost to the shipping cost
based on the following:
1
CSE 2020Y(3) – Object-Oriented Techniques Labsheet 5
Write a program that instantiates at least three objects of each type (Package and
InsuredPackage) using a variety of weights and shipping method codes. Display the results
for each Package and InsuredPackage.
Question 4
a) Create a class called Account1 that stores information about a bank account. It should
have a data member to store the account balance named balance, and the account number,
named accountNumber. The class should also have a static data member called
nextAccountNumber initialized to zero. Create a constructor for the class that takes a
balance amount as an argument, and sets the accountNumber variable to the
nextAccountNumber plus 1. Save this class in a file named Account1.java.
b) Add a method called display that will display the account number and balance. Create
another class called OpenAccount that will create three Account1 objects with numbers.
Save this class in a file called OpenAccount.java.
c) Add another constructor that will allow Account1 objects to be instantiated without a
balance argument, so that the balance is initialized to zero, but the account number
receives the next available number. Use the this reference in the constructor to call the
constructor that takes the double argument.
d) Add a static variable called interestRate and initialize it to .08 (8% interest). Add a method
called calcInterest that will calculate the interest, display the interest amount, add the
interest to the balance and display the new balance.
e) Add a method that will report what the last account number assigned was, and also what
the current interest rate is. Call this method reportStatus.
Question 5
a) Add a method called withdraw( long amount) to the BankAcc (found in this folder) class
to make a withdrawal. It should be something like:
Rerun the program BankAcc.java. Does the correct balance come out?
2
CSE 2020Y(3) – Object-Oriented Techniques Labsheet 5
b) Compile and run the program DepositAcc.java (found in this folder). Override the
display() method in class DepositAcc it only displays a message like: Account type is
Deposit with the statement:
Rerun the program. Which display() method is invoked on acc, the BankAcc one or the
DepositAcc one?
c) The display() method from DepositAcc is not as useful as the one of its superclass or
parent class. We can get it the first call display() from BankAcc by adding the following
line at its beginning:
super.display();
d) Override the withdraw() method in class DepositAcc so that it first checks to see if the
amount to be withdrawn is less than or equal to the balance. If it is, then it calls the
withdraw() method of it’s parent class, otherwise it generates an error message. Modify
main() to try this out.
e) Create a subclass of BankAcc called CurrentAcc which will represent a current account. It
will need 3 extra attributes: withdrawal limit, interest rate charged on overdrafts, number
of transactions. Each time a transaction is made, the number of transactions should be
increased by 1.
f) Write a program called BankApplic.java which uses the above mentioned classes. Get it to
create 3 variables of type BankAcc. Assign a BankAcc object, a DepositAcc object and a
CurrentAcc object to these variables.
3
CSE 2020Y(3) – Object-Oriented Techniques Labsheet 5
g) Create a subclass of DepositAcc called DepositAcc2. Add an extra attribute to count the
number of transactions so that on each transaction the count is incremented by 1. You will
need to override the deposit() and withdraw() methods to do this. Modify them again so
that on the fifth transaction, the account fee is calculated at 25p per transaction if the
balance is less than £100 or at 0p otherwise. The fee should be subtracted from the
balance during the transaction.