Spring 2005
CSUDH Computer Science Department
CSC123A -01
Lab Assignment 3: Due by the end of lab session
Goals:
1. The relationship between classes: inheritance
2. Abstract classes and abstract methods
3. Static and final fields and methods
4. Testing classes
Requirements: (Exercises P11.10 on Page 472)
Reorganize the bank account classes as follows. In the BankAccount class, introduce an abstract method endOfMonth with no implementation. Rename the addInterest and deductFees methods into endOfMonth in the subclasses. Which classes are now abstract and which are concrete? Write a static method void test(BankAccount account) that makes five random transactions, prints out the balance after each of them, and then calls endOfMonth and prints the balance once again. Test it with instances of all concrete account classes.
1. Name a new project as lab03
2. Edit the source code for all classes
3. Compile all source code and run the main method without parameter.
4. Create a stand-alone application by exporting your source code to a folder (directory), and go to the corresponding folder to compile the source and run the byte code with the command:
java BankAccountTest
5. Create all documentation files and use the web browser to display your documentation.
6. Your source code documentation must follow the instructions of Chapter 2.8, pages 54-56. Be advised to briefly comment all classes, constructors, and methods.
Design:
1. In this new project, we design four classes: BankAccount, SavingsAccount, CheckingAccount, and BankAccountTest.
2. The BankAccount class contains all methods of bank transactions such as getting balance, deposit, and withdraw, which are similar to what we developed in our classes. Additionally, this class provides an abstract method endOfMonth, with no implementation. Because of this, the BankAccount class should be an abstract class.
3. Both SavingAccount and CheckingAccount are the subclasses of BankAccount. In SavingAccount, the endOfMethod method calculates the interest and updates the balance; while in CheckingAccount, the endOfMonth method calculates the transaction fees and updates the balance.
4. The static method test should be in the BankAccountTest class. In order to generate random transactions, import the class java.util.Random.
Implementation: The source codes for BankAccount and BankAccountTest are given below. You are required to implement SavingAccount and CheckingAccount.
1. The source code for BankAccount
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
@author Jack Han
@date Feb. 4, 2005
*/
public abstract class BankAccount
{
// The instance field for account balance
private double balance;
/**
Constructs a bank account with a zero balance
*/
public BankAccount()
{
balance = 0;
}
/**
Constructs a bank account with a given balance
@param initialBalance the initial balance
*/
public BankAccount(double initialBalance)
{
balance = initialBalance;
}
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(double amount)
{
balance = balance + amount;
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
balance = balance - amount;
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
/**
Transfers money from the bank account to another account
@param other the other account
@param amount the amount to transfer
*/
public void transfer(BankAccount other, double amount)
{
withdraw(amount);
other.deposit(amount);
}
/**
Abstract method that calculate various operations
at the end of the month
*/
public abstract void endOfMonth();
}
2. The source code for BankAccountTest
/**
This program tests the BankAccount class and their subclasses.
@author Jack Han
@date Feb. 4, 2005
*/
public class BankAccountTest
{
public static void main(String[] args)
{
SavingsAccount mySavings
= new SavingsAccount(0.5);
CheckingAccount myChecking
= new CheckingAccount(0);
test(mySavings);
test(myChecking);
}
public static void test(BankAccount account)
{
Random generator = new Random();
for (int i = 1; i <= 5; i++)
{
double amount = generator.nextInt(1000);
if (generator.nextBoolean())
account.deposit(amount);
else
account.withdraw(amount);
}
System.out.println("Before endOfMonth, the balance is $"
+ account.getBalance());
account.endOfMonth();
System.out.println("After endOfMonth, the balance is $"
+ account.getBalance());
}
}
3. The source code for SavingAccount
4. The source code for CheckingAccount