Week 02: Homework 1

Growing money with loops

The problem

An example of a simple question that you might ask of a computer is “How many years does it take to accumulate $20,000 in a bank account if you start with $10,000 and interest is fixed at 5% compounding annually?”

Code up and run a program to solve this problem. Remember that a Java program must contain at least one class declaration (and the name of this class must match the program name case-sensitively), which inside must include the main-method with prescribed modifiers and signature.

HINT: Draw up a table of your bank balance after 1 year, 2 years and so on:

Year   Balance ($)
0      10,000
1      10,000 + 0.05*(10,000)
2      Year 1 balance + 0.05*(Year 1 balance)	
3      Year 2 balance + 0.05*(Year 2 balance)	
... ... ...

You could write a program to print this output and then run it for, say, 10 years. You could then decide how you might write a program which will find out how many years you would need to accumulate $20,000.

Note that you will need to use a loop to write this program. Because we may not have yet covered this in lectures, you may wish to wait a little, or learn it on your own (in the text book, or in the lecture slides, or in Java Tutorial, The while and do-while Statements).

Assessment

You will get up to two marks, if you present a solution to the Homework exercise during the next week lab.

Updated:  16 Feb 2017/ Responsible Officer:  Head of School/ Page Contact:  Alexei Khorev