Second Summer 2002


Homework Assignment Three: Interests

In this assignment you will be practicing with while and for loops.

First, write an algorithm to settle the following question:

A bank account starts out with $10,000. Interest is compounded at the end of every month at 6 percent per year (0.5 percent per month). At the beginning of every month, $500 is withdrawn to meet college expenses after the interest has been credited. After how many years is the account depleted?
This problem is listed as Exercise R1.16 (page 44) in your text.

Now suppose the numbers ($10,000, 6 percent, $500) were user-selectable. Are there values for which the algorithm you developed would not terminate? If so, make sure it always terminates.
This extension is listed as Exercise R1.17 (page 44). This will be your assignment.

Therefore you are to write a program that asks the user for three values:

Then if the program can determine that the account will eventually be depleted, it will report in how many years this will happen, otherwise, if the program determines that for the values provided by the user the account will never get depleted, the program will report that and end. Here's a sample session with your program:
frilled.cs.indiana.edu%java Interests
Welcome to the financial calculator.
What's your initial balance? 10000
What's the yearly interest? 6
How much do you plan to withdraw monthly? 500
The account will last 1 year(s) and 9 month(s).
Ending balance will be: 62.2 dollars.
frilled.cs.indiana.edu%java Interests
Welcome to the financial calculator.
What's your initial balance? 10000
What's the yearly interest? 6
How much do you plan to withdraw monthly? 100
The account will last 11 year(s) and 6 month(s).
Ending balance will be: 97.09 dollars.
frilled.cs.indiana.edu%java Interests
Welcome to the financial calculator.
What's your initial balance? 10000
What's the yearly interest? 6
How much do you plan to withdraw monthly? 10
This will last forever.
frilled.cs.indiana.edu%java Interests
Welcome to the financial calculator.
What's your initial balance? 10000
What's the yearly interest? 6
How much do you plan to withdraw monthly? 50
The account will last 525 year(s) and 1 month(s).
Ending balance will be: 48.1 dollars.
frilled.cs.indiana.edu%

Please call your program (the class) Interests for faster grading.

Do you see anything fishy in the examples given? (I hope you do.) What is it?

frilled.cs.indiana.edu%webster fishy
fishy \'fish-e^-\  fish-i-er; -est
(1547)
1: of or resembling fish esp. in taste or odor
2: creating doubt or suspicion: QUESTIONABLE 


frilled.cs.indiana.edu%
What do you plan to do about it in your program?


Here's the code used to produce the output above:

class Interests {
    public static void main(String[] args) {

	ConsoleReader c = new ConsoleReader(System.in); 

	System.out.println("Welcome to the financial calculator.");

	System.out.print("What's your initial balance? "); 
	double initialBalance = c.readDouble(); 

	System.out.print("What's the yearly interest? ");
	double yearlyInterest = c.readDouble() / 100; 

	System.out.print("How much do you plan to withdraw monthly? "); 
	double monthlyStipend = c.readDouble(); 

	double calculation = 
	    initialBalance * (1 + yearlyInterest / 12) - monthlyStipend; 

	if (calculation >= initialBalance) {
	    System.out.println("This will last forever."); 

	} else {

	    double balance = initialBalance; 

	    int months = 0; 

	    while (balance * ( 1 + yearlyInterest / 12) > monthlyStipend) {
		months += 1; 
		balance = balance * (1 + yearlyInterest / 12) - monthlyStipend;
	    }

	    int years = months / 12; 
	    months = months % 12; 

	    System.out.println("The account will last " + years + 
			       " year(s) and " + months + " month(s)."); 

	    System.out.println("Ending balance will be: " + 
			       Math.round(balance * 100) / 100.00
			       + " dollars."
			       ); 

	}

    }
} 
Why is this program not calculating the last case correctly?

Also, is this program printing the right (correct) output? Why or why not?

frilled.cs.indiana.edu%cat Why.java
class Why {
  public static void main(String[] args) {
    double sum = 0; 
    for (int i = 0; i < 10; i++) 
      sum = sum + 0.1; 
    System.out.println(sum); 
  }
}
frilled.cs.indiana.edu%javac Why.java
frilled.cs.indiana.edu%java Why
0.9999999999999999
frilled.cs.indiana.edu%


Last updated: Jun 16, 2002 by Adrian German for A201