CS 5 Fall 2009
Short Assignment #2
Due Monday, September 28

How rich am I?

In the year 0 A.D., my ancestor Brutus Cormen invested $1.00 in the Bank of Rome. (OK, it wasn't a real dollar; it was the Roman equivalent of a dollar. Probably some kind of big coin with a hole in the middle.) The bank promised to pay 5% interest compounded once a year. Each year the bank added to his account 5% of the previous year's balance. In other words, they multiplied his balance by 1.05 once each year. Since old Brutus was not around to withdraw it (Julie Caesar's descendants took care of that), the balance has accumulated nicely up to the present day. Because I am his rightful heir, the bank account is now mine. Bwa-ha-ha!!

Write a Java program that computes the size of my bank balance in a year that the user types into the console. That is, it should ask for a single integer as its input (the year) and should print out a single value (the bank balance in that year). For example, if you give it the input 0, it should print out that the balance is $1.00. If you give it the input 1, it should print out the balance $1.05. If you give it the input 2, it should print out the balance $1.1025 (which is 1 * 1.05 * 1.05). And if you give it the input 3, it should print out the balance 1.157625 (1 * 1.05 * 1.05 * 1.05). Your program should ask the user to enter a year (we call this a prompt), and the output should be descriptive.

It cost Dartmouth $50,000,000 to build Berry Library. Have your program print out not only the balance, but also how many Berry Libraries we could build with that much money. (To make life easier for you, assume that we can build a fractional part of a Berry Library.)

Test your program by inputting years such as 1800, or 1492, or 1956. Use your program to find out what the bank balance (and number of libraries that could be built) was in the year 800. How about in the year that Dartmouth was founded? This year? Submit your Java program, and submit the output for runs that show the balance and number of libraries in 800, the year Dartmouth was founded, and this year.

In your program, make nice use of defined constants (i.e., final variables), well chosen variable names, informative headers and line comments (if necessary), and gorgeous output (including a prompt). The variable you use to maintain the balance should be of type double. Note that your program is basically very simple—mainly a for-loop to perform repeated multiplications. Recall that we saw several for-loops in the class examples. Refer to them if you need to. Section 5.8 of the Lewis & Loftus text discusses for-loops, but the examples from class should suffice. If the mathematics of this problem puzzles you, get help with the math from me or any of the course staff.

Note: You can use fancier mathematics to solve this problem without a for-loop. Please don't try to impress me with how much math you know. You'll have plenty of opportunities later on to impress the heck out me. In particular, don't try to perform exponentiation. Just use a for-loop that recomputes the balance once in each iteration, where each iteration represents one year.

Note also that although the examples of Fact4.java, Fact5.java, and Fact6.java all used the loop variable i within the body of the for-loop as part of the calculation, you don't have to use the loop variable in the body; you can use it just to count out how many iterations to perform.

Don't worry about the formatting of the balance. Let Java and Eclipse print it out in whatever way they want to.

We expect to make up the section assignments before Monday's class. You will place your output in the CS 5 HW HANDIN box with your Section Leader's name by the start of class on Monday. Make sure to put it in an envelope with your name clearly marked on the outside.


Back to Short Assignments
Thomas H. Cormen <thc@cs.dartmouth.edu>
Last modified: Thu Sep 24 23:07:18 2009