Homework 4DetailsSmallBank
READ THESE DIRECTIONS CAREFULLY
-
Create a class named BankAccount.
This class should include two instance variables,
one that records the balance and another that records the name of the account holder.
You should provide code for each of the following:
- Constructor that takes a name as its only input parameter and sets the initial balance to zero.
- Constructor that takes a name and an initial balance as its two input parameters.
- An accessor method that returns the current balance of the account.
- An accessor method that returns the current name of the account.
- A mutator method that sets the name of the account to the new name in its input parameter.
- A mutator method that makes a deposit to the account by adding its input parameter
to the balance.
- A method that makes a withdrawal from the account by subtracting its input parameter
from the balance.
- Write your class so that it prohibits negative values for deposit and withdrawal
and so that it prohibits a withdrawal of an amount larger than the balance.
- Test your class thoroughly.
-
Create a second class named SmallBank.
This class should have two instance variables named account1 and account2,
each of type BankAccount. The reason this is a small bank is because it can only
have two bank accounts - we'll see how to create larger collections in chapter 4.
You should provide code for each of the following:
-
SmallBank Constructor that takes an account name and an initial balance as its two input parameters.
Since we don't want to have a bank without having an account,
this constructor should create a BankAccount object using the account name and initial balance
parameters, and store it in the account1 instance variable. It should leave the account2
instance variable uninitialized (i.e., null).
-
A method in SmallBank named addSecondAccount that takes an account name and an initial
balance as its two input parameters. This method should create a BankAccount object using
the account name and initial balance parameters, and store it in the account2 instance variable.
-
A method in SmallBank named printBankDetails that prints out the details of the stored
BankAccounts. This method should work in the case that there is only one BankAccount as
well as in the case that there are two BankAccounts. You can check whether there is a
value in the account2 instance variable as follows:
if (account2 != null)
-
Write a class named SmallBankTester that has a method named testSmallBank.
This method should create a SmallBank object, invoke the printBankDetails method a
first time to print the first account's details, invoke the addSecondAccount method
to add the second account, and invoke the printBankDetails method a second time to
print both account details. This method doesn't need to return anything, but you
should look at the Terminal window to make sure the account details printed properly both times.
When you have completed this assignment, select "Create Jar File ..." from the File menu in BlueJ.
Check "include source" and "include Bluej project files".
Save the file with the name smith-smallbank.jar (except use your name instead of smith).
Then upload the "jar" file into Moodle no less than ten minutes before the start of class.
|