THINK FIRST·CODE LATER

← All labs

How long until the savings goal?

Problem

You deposit the same amount at the end of every month in an account that pays interest monthly. Write SavingsGoal: read the goal, the monthly deposit and the annual interest rate in percent. Each month: add the interest on the current balance (balance × rate / 100 / 12), then the deposit. Stop when the balance reaches the goal.

Print the balance after each full year, then the number of months, the final balance, the total deposited and the interest earned. A deposit ≤ 0 prints The monthly deposit must be positive.

For the input

10000 800 3

the program prints

After year 1: 9733.11
Goal reached after 13 months (1 years and 1 months)
Final balance: 10557.44
Deposited: 10400.00, interest earned: 157.44

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l7-6-data-1.txt

10000 800 3
What your program must print for l7-6-data-1.txt
After year 1: 9733.11
Goal reached after 13 months (1 years and 1 months)
Final balance: 10557.44
Deposited: 10400.00, interest earned: 157.44

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l7-6-data-1.txt

Windows PowerShell does not understand <:

Get-Content l7-6-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.