Lab Assignment #9: Classes
Due by 11:59PM on Friday, April 16.
DRAFT - to be finalized by 4/9/99
Reading Assignment
Read about classes in Chapter 8 of the text.
In-Lab Work
A Class design for a Bank
In the lab this week, we are going to design a class hierarchy for
a simple bank account structure. You will extend the same program you
write for the in-lab exercise for the actual lab assignment.
The basic problem has to do with bank accounts. Think of a bank
account as an individual object. What are the most important data
properties? What types of operations would you have on these data? In
the lab, you will design a simple bank account class that can store
just the basic information on that account - the account
balance. Write a class BankAccountLab with the following
features:
- The class should have a data variable called balance
which cannot be accessed from any other classes. (So what type
of protection should you use?)
- The class should have the following constructors:
- A default constructor, that sets the account balance to
zero.
- A special constructor which takes in a parameter called
initialbalance, and creates an account with that value as
the balance.
- The class should also have the following methods:
- Deposit - this method should take in the amount
for the deposit, and add that to the current balance.
- PrintBalance - this method should print the
current balance of the account in some useful
fashion.
Save this program as BankAccountLab.java
Lab Assignment
NOTE!
To test your classes, you will need a main program to drive all
your classes. I have created a main program that tests out all your
methods. You should make sure your classes work with this main
program. Do not change this main program. We will be
using a main program like this one to test your code. If your
parameters are not in proper order, make sure you order them according
to how the main program expects it. The account number is treated as a
string in this main program.
Download BankMain.java
For the lab, we will start with the basic lab assignment that you have
created, and will make a number of changes. There are two sets of
implementations that you will need to do - the first set you should be
able to do by 4/13, since everything you need will be
covered in the lab and lecture. The second set requires inheritence,
which would be covered in class on (4/13). Of course, nothing is due
until 4/16, but you should still work on the program this week, since
it is significantly easier than lab 8.
The following sets of enhancements are to be made to the program:
- First set:
- Change the filename and classname to BankAccount
- In addition to the account balance, you should have an
accountNumber and patronName field to store
the account number and the name of the person with that account
number.
- Change the constructor so that it also accepts the account
number and name of client in addition to the balance
- Change the printBalance method so that it prints the
account number and name on the account in addition to the
current balance.
- Add the method Withdraw that lets someone withdraw
money from the accounts, and generates an error message if the
user over-withdraws from the account (i.e., a withdrawal causes
the balance to fall below 0. In that case, deny the withdrawal
(i.e., do not change balance, but generate an appropriate error
message.)
- Second set:
- Create two subclasses of BankAccount -
SavingsAccount and
CheckingAccount. CheckingAccount should have a
data variable called checks (an integer, that shows the
number of checks left). This should be set to a default
value of 10 by the CheckingAccount constructor.
- Override printBalance so that it also prints something
that shows what type of account it is.
- In SavingsAccount, create a method called
addInterest that takes in an interest rate,
updates the account balance using a simple interest
formula (balance = balance + balance * rate / 100.0).
What change do you need to make to the BankAccount
class in order to facilitate this? (Hint: balance
in bankaccount was originally defined so that it cannot be
accessed directly by any other class!
- In CheckingAccount, create a method called
buyChecks that takes in a number (corresponding to the
number of checks being bought) and updates the remaining
number of checkts.
- In CheckingAccount, create a method called
WriteCheck that takes in an amount for the check
and updates the balance and the remaining number
of checks. Notice that you will also need to update the
balance and check for errors there, but you can do this by
simply calling the corresponding method from the parent
class (BankAccount). If the number of checks is exhausted,
then it should display an error message and refuse the
transaction.
Submitting Your Lab Assignment
For this lab, you should submit three files: BankAccount.java,
SavingsAccount.java, and CheckingAccount.java
Attach these files to an e-mail sent to
a201@cs.indiana.edu with
lab9 as the subject.