The following is a file of BankAccount.java
/*****************************************************
A bank account has a balance that can be changed by
deposits and withdrawals.
*****************************************************/
public class BankAccount
{
/***********************************************
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)
{
double newBalance = balance + amount;
balance = newBalance;
}
/***********************************************
Withdraws money from the bank account.
@param amount the amount to withdraw
***********************************************/
public void withdraw(double amount)
{
double newBalance = balance - amount;
balance = newBalance;
}
/************************************************
Gets the current balance of the bank account.
@return the current balance
************************************************/
public double getBalance()
{
return balance;
}
private double balance;
}
The following is a file of BankAccountTester.java
/**************************************************
A class to test the BankAccount class.
**************************************************/
public class BankAccountTester
{
/**********************************************
Tests the methods of the BankAccount class.
@param args not used
**********************************************/
public static void main(String[] args)
{
BankAccount harrysChecking = new BankAccount();
harrysChecking.deposit(2000);
harrysChecking.withdraw(500);
System.out.println(harrysChecking.getBalance());
}
}