BankAccount.java
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount
{
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)
{
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;
// your work here
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
}
Use the following file:
BankAccountTester.java
public class BankAccountTester
{
public static void main(String[] args)
{
BankAccount account1 = new BankAccount(1000);
account1.withdraw(100);
System.out.println(account1.getBalance());
System.out.println("Expected: 899");
BankAccount account2 = new BankAccount();
account2.deposit(1000);
account2.withdraw(100);
account2.withdraw(100);
System.out.println("Balance: " + account2.getBalance());
System.out.println("Expected: 798");
}
}