← Unit 3 · all exercises
Saved in this browser only

A bank account

Write a BankAccount class holding a balance in cents. The constructor takes a starting balance. deposit adds to it, withdraw takes away, and getBalance returns it. The balance may go negative.

class BankAccount { BankAccount(int start); void deposit(int amount); void withdraw(int amount); int getBalance(); }

BankAccount.java
Loading editor…
Results

Examples

  • BankAccount a = new BankAccount(100); a.deposit(50); a.getBalance() → 150
  • BankAccount a = new BankAccount(100); a.withdraw(30); a.getBalance() → 70

There are also 3 hidden tests you can't see. Solve the problem, not the examples.