This website is preserved for historical and scholarly reference and is no longer actively maintained.

Homework 11

Due Wednesday, October 1, 1997

Recall Exercise R 1.16:

Write an algorithm to settle the following question: A bank account starts out with $10,000. Interest is compounded monthly at 6 percent per year (0.5 percent per month). Every month $500 is withdrawn to meet college expenses. After how many years is the account depleted?
We prepared a table for this calculation. Now we wish to take the more general approach with a Java program. The following are to be input by the user:

Step 1.

Step 1 in the development of this program is to read these from the user and print them out (to be sure that you read what you think you read).

Step 2.

Write a function, modeled on the program and function at the top of page 181, which computes the monthly interest to be added, adds the month's interest, and returns the new balance to the main program. Your function is different fromt he function in the text because your function computes only the balance after one month.

Run your program (including function) with test data to be sure the program works.

Step 3.

Add a loop to the main program to repeatedly add monthly interest and subtract the withdrawal amount until the account runs out of money. Your program should print out the month number and the new balance each iteration of the while loop. Run this program with the data used for R 1.16.

In our hand-calculated problem, we allowed the balance to become negative. In this case, since real banks don't like overdrafts, do not allow the balance to drop below zero. Instead, loop on the condition that the balance is greater than zero. If the monthly withdrawal is greater than the balance, write a statement to this effect, withdraw the balance, and state the amount of the withdrawal.

Run your program with the test data from R 1.16 to be sure your program works correctly for some data. Then change each of the input values and test to see if your program still works correctly.

Note:

This program ties together the concepts of chapters 1, 4, 5, and 6. Step 1 makes use of your understanding of the problem from Chapter 1. Step 2 asks you to construct a function from Chapter 5. Step 3 asks you to use a while loop from Chapter 6 and an if-statement from Chapter 4. Do not put this program off until the last minute. This program will require considerable time and effort.

As always, comments should give your name, the date program completed, and a statement of the problem. Use style standards endorsed by the text and in class. Turn in a printout at the beginning of class.

Return to Eleanor Hare's home page.

Return to Department of Computer Science Home page.