pennyChallengeDays 🌶️🌶️🌶️
In a savings challenge you put start into a jar on day 1. After that, each day you put in as much as the jar already holds, so the amount in the jar doubles every day.
Write a method pennyChallengeDays that takes start (more than $0.00) and goal, and returns the first day on which the jar holds at least goal. Use add. Money has no way to compare two amounts, so compare them in cents: getDollars() * 100 + getCents().
For example, starting with $1.00, the jar holds $1, $2, $4, $8 on days 1 to 4, so pennyChallengeDays with a goal of $5.00 is 4. If the goal is no more than start, the answer is 1.
add?jar = jar.add(jar); doubles it. Compare jar and goal in cents, and count the day number as the jar grows.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
| Method | What it does |
|---|---|
Money(int dollars, int cents) | Makes an amount. Cents of 100 or more carry into dollars, so
|
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.
Returns the sum |
boolean equals(Money other) | Whether another amount is exactly the same.
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 |