Lab 4: Bank Account
- Review the dice simulation example files Die.java and DieTest.java. and other examples.
- In a file named
BankAccount.java
, create a
class named BankAccount
to simulate a bank account.
- The bank balance should be stored in a private floating point
variable named balance
.
- The
BankAccount
class should have the following methods:
-
withdraw()
and deposit()
should accept a single
parameter which will decrease or increase the balance.
For this assignment they should not have a return value, so their type
should be void
.
- A constructor method
BankAccount()
should have an initial deposit parameter to initialise the account.
- In a file named
BankAccountTest.java
, create
a class named BankAccountTest
with just one
method named main()
.
- Ask the user to enter an initial deposit.
- Create a new
account
object of type BankAccount
with this initial deposit.
- Loop through the following until the user enters 0:
- Ask the user to enter a positive number for deposit,
negative number for withdrawal, or 0 to exit.
- After each deposit or withdrawal, print the current balance.
- Check: Make sure all your variables, methods and classes are
either
public
or private
as
appropriate.
- Compile the programs by typing
javac
BankAccount.java BankAccountTest.java
- If there are no other Java files in the current directory you
can simply type
javac *.java
- Run, test, debug and document the programs.
- Note: it is possible that the account balance will become
negative. You may want to consider ways to prevent this.
- Send the source code to the instructor as two attachments in a
message with subject java lab4.