The completed program is given below.
In completing the program,
be sure that rate is correct.
The user is supposed to enter percent interest,
for example 6 for six percent.
The program needs a decimal fraction such as 0.06 to work correctly.
import java.io.*;
class dollarsAfterForty
{
public static void main( String[] args ) throws IOException
{
double dollars = 1000.00 ;
double rate;
int year = 1 ;
// get the interest rate from the user
String rateChars;
BufferedReader stdin =
new BufferedReader( new InputStreamReader(System.in) );
System.out.println("Enter the interest rate in percent:");
rateChars = stdin.readLine();
rate = Double.parseDouble( rateChars ); // get rate in percent
rate = rate/100.0;
while ( year <= 40 )
{
// add another year's interest
dollars = dollars + dollars*rate ;
// add in this year's contribution
dollars = dollars + 1000 ;
year = year + 1 ;
}
System.out.println("After 40 years at " + rate*100
+ " percent interest you will have " + dollars + " dollars" );
}
}
Here is an example of a user dialog with the program:
C:\users\default\JavaLessons\chap19>java dollarsAfterForty Enter the interest rate in percent: 11 After 40 years at 11.0 percent interest you will have 646826.9337201559 dollars C:\users\default\JavaLessons\chap19>java dollarsAfterForty Enter the interest rate in percent: 12 After 40 years at 12.0 percent interest you will have 860142.3907860613 dollars C:\users\default\JavaLessons\chap19>java dollarsAfterForty Enter the interest rate in percent: 13 After 40 years at 13.0 percent interest you will have 1146485.7949682677 dollars