Lab 3

Subclassing

In Chapter 10, we start to study an advanced OO mechanism, inheritance, which can be achieved by writing a class that extends an existing class. In addition to inherit public behaviors from its super class, a subclass can also define variables and members of its own. A subclass often needs to override some of the methods defined in its superclass. Instances of a subclass can be used when a value of its super class is expected. In such cases, they need to be cast back to its own type to invoke any methods that are available in the subclass only.

In this lab, we will have some hands-on in

-  subclassing

-  overriding methods

-  class type testing and class casting

We will also have an exercise on exception handling related to Date data parsing.

1.  Write a blank BankAccount class.

public class BankAccount {}

Will it compile?

2.  Write a BankAccount Test class that has a main method. In the main method, add the following statements:

BankAccount myAccount = ; //(a)

System.out.println(myAccount); //(b)

boolean woops = myAccount.equals("account"); //(c)

System.out.println(woops); //(d)

2.1 Finish the first line to instantiate a BankAccount object.

2.2 Will this program compile? [I said it’s going to compile in class. But what is the constructor that you can use and why you can use it?]

2.3 If it does compile, briefly explain what happens when each of the first three statements is executed.

Hint / Explanation
(a) / Constructor
(b) / Print object as string
(c) / Test equality

2.4 What will be displayed when the println statements are executed?

Expected / Actual
(b)
(d)

3.  Copy the class definition in BankAccount.java into your BankAccount class.

3.1 Will the BankAccount Test still compile? Why?


3.2 If not, how to fix it?

4.  Create a SavingsAccount class with the content in SavingsAccount.java. Add the following lines into the main method of the SavingsAccountTest class:

public class SavingsAccountTest {

public static void main(String[] args) {

// # 1

BankAccount a1 = new BankAccount(new Money(123.45));

// # 2

BankAccount a2 = new SavingsAccount (0.03);

// # 3

SavingsAccount s1 = new BankAccount ();

// # 4

BankAccount myAccount = s1;

// # 5

SavingsAccount yourAccount = a2;

// # 6

System.out.println(myAccount);

// # 7

System.out.println(a2.getBalance());

// # 8

System.out.println(yourAccount.equals(myAccount));

}

}

Which line(s) will not compile? Briefly explain.

Compile or not? / Explanation
1
2
3
4
5
6
7

Fix all errors to get the program to work. (For statement #3, instantiate a savings account that is the same as the one in #2.) After the program is executed, what will be the output from statement #6? Which version of the toString() method is invoked? Why?

5.  What will be the output for #8 in SavingsAccountTest? Why? Assume true should be displayed when two computers/laptops have the same value in each attribute. How to fix this error? [Hint: override the method equals in SavingsAccount and BankAccount classes.]

6.  Lastly, an exercise about reading data from a file and creating objects with the data (as may be used in your CIS project). Add the DataFileTest.java file to your project and answer the following questions.

6.1 Will this program compile as given? Why or why not?

6.2 How can you fix it?

6.3 Under which folder should you put your account.data file? [Hint: it should be the base directory for your project. You can look it up on the properties sheet for your project.]

6.4 (optional) You may try to use a FileChooser to let the user to select a file on the fly.

Deliverables:

Submit your completed worksheet by Tuesday, Sept 21. Attach your SavingsAccountTest class and the equals methods that you implemented.

Also, maintain your course portfolio and be prepared for a code review towards the end of the semester.