CS1027b Computer Science Fundamentals II

Lab 3

General lab instructions to help labs run smoothly

Overview and preparation

Upon completion of this lab, you should be able to do the following:

For preparation, Review Topic 4 of the lecture notes (the keyword super, overriding methods, superclass variables, polymorphism, dynamic binding). You might find it helpful to bring the Topic 4 notes to the Lab, to help you in answering the questions of Exercise 2. Make sure you understand the code for the BankAccount class and the CheckingAccount class.

Exercise 1: Creating another subclass of BankAccount

Download the BankAccount class and the CheckingAccount classes, and create a new project for these files.

Create a new SavingsAccount class in your project, which extends the BankAccount class. It will contain an additional attribute besides the one inherited from the BankAccount class: the interest rate (a double) for a savings account. It will also contain the following methods:

Exercise 2: Experimenting with inheritance

Download the file TestBankAccounts.java and add it to your project. It has a main method that so far only creates some bank account objects of the three types BankAccount, CheckingAccount, and SavingsAccount.

In each step of this exercise, you will be adding statements to the main method, running it after each addition. Answer the questions at each step. If there is a compilation error, make sure you remove (or comment out) the offending statement before advancing to the next step of this exercise.

Make the following changes to class TestBankAccounts (code must be added near the end of the main method, after the last comment line):