← all problems

sameOrBothMissing 🌶️🌶️

Write a method sameOrBothMissing that takes two Money objects, either of which may be null (no amount), and returns true if both are null, or if neither is null and they are the same amount. In every other case it returns false. Calling equals on null throws a NullPointerException, so test for null first.

Reference: Money — the methods you can call

class Money

An amount of money in dollars and cents, kept exactly (as whole cents, never a double). A Money never changes once it is made. You do not write this class; make an amount with new Money(dollars, cents).

Money price = new Money(3, 75);
price.toString()                          // "$3.75"
price.add(new Money(1, 50)).toString()    // "$5.25"
new Money(0, 250).getDollars()            // 2
new Money(0, 250).getCents()              // 50
price.equals(new Money(3, 75))            // true
MethodWhat it does
Money(int dollars, int cents)

Makes an amount. Cents of 100 or more carry into dollars, so new Money(0, 250) is $2.50.

  • dollars – the whole dollars
  • cents – the cents
int getDollars()

The whole dollars of the amount.

Returns the dollars

int getCents()

The cents left over after the whole dollars.

Returns the cents, 0 to 99

Money add(Money other)

The sum of this amount and another, as a NEW Money. Neither original changes.

  • other – the amount to add

Returns the sum

boolean equals(Money other)

Whether another amount is exactly the same.

  • other – the amount to compare with

Returns true if equal; false otherwise, including for null

String toString()

The amount as text with a dollar sign and two digits of cents, such as "$3.75" or "$0.05".

Returns the amount as text