← Unit 3 · all exercises
Saved in this browser only

Dollars and cents

Write a Money class holding an amount in whole cents. dollars returns the whole dollars, cents returns the leftover cents, and format returns the amount as text like "$3.25" — always with two digits after the point.

class Money { Money(int totalCents); int dollars(); int cents(); String format(); }

Money.java
Loading editor…
Results

Examples

  • new Money(325).format() → $3.25
  • new Money(325).dollars() → 3

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