Various Interest Rates

S

shannon

hi all,

Working on this compound Interest Program and want to repeat the
program for 10 different interest rates, I think i should be using a
loop but not quite sure how to go about it. The following is what i
have so far.

Cheers
Shannon

____________________________________________________________________

//Java Packages

import java.text.NumberFormat; // class for numeric formatting
import java.util.Locale; // class for country-specific information

import javax.swing.JOptionPane;
import javax.swing.JTextArea;


public class CompoundInterestProgram {

public static void main( String args[] )
{

double amount; //amount on deposit at end of each year
double principal = 20000.0; // initial amount before interest



//create NumberFormat for currency in US dollar format
NumberFormat moneyFormat = NumberFormat.getCurrencyInstance( Locale.US
);

// create JTextArea to display output
JTextArea outputTextArea = new JTextArea();

// set first line of text in outputTextArea
outputTextArea.setText( "Year\tAmount on deposit\n" );

// calculate amount of deposit for each of ten years
for ( int year = 1; year <= 10; year ++ ) {

//
for ( int rate = 1; rate <= 10; rate ++)

// calculate new amount for specified year
amount = principal * Math.pow( 1.0 + rate, year );

// append one line of text to outputTextAea
outputTextArea.append( year + "\t" + moneyFormat.format( amount
) + "\n" );

}// end for

// display results

JOptionPane.showMessageDialog( null, outputTextArea, "Compound
Interest", JOptionPane.INFORMATION_MESSAGE );

System.exit ( 0 ); // terminates the application

} // end main
} // end class Interest
 
M

mikm

You could set up an array that contains all the interest rates then
iterate through that.
 
S

shannon

The for loop below seems to be working for me, at the moment. I have
the interest rates at full numbers eg 5, 6 etc. but i need to change
them to 0.05, 0.06 but not sure how to go about this. Think it might be
something to do with precision?


// calculate amount of deposit for each of ten years
for ( int year = 1; year <= 10; year ++ ) {

//calculate interest rates
for ( int rate = 5; rate <=10; rate ++) {

// calculate new amount for specified year
amount = principal * Math.pow( 1.0 + rate, year );

// append one line of text to outputTextAea
outputTextArea.append( year + "\t" + moneyFormat.format( amount
) + "\t" + moneyFormat.format( amount )
+ "\t" + moneyFormat.format( amount ) + "\t" +
moneyFormat.format( amount ) + "\t" + moneyFormat.format( amount ) +
"\t" + moneyFormat.format( amount ) + "\n" );

}// end for
} //end for
 
R

Rajah

Shannon, it's not so much the precision, but rather the declaration
that you have for rate.

Since you declared it as (int rate = 5; rate <= 10; rate++) rate can
only be an integer.

Without giving you the answer, could you think of another way to
declare rate so that it might hold fractional parts?

By the way, the "rate <= 10" will end the loop with rate = 11. I don't
know if you want to write this as "rate < 10" instead.
 
S

Steve Bosman

shannon said:
The for loop below seems to be working for me, at the moment. I have
the interest rates at full numbers eg 5, 6 etc. but i need to change
them to 0.05, 0.06 but not sure how to go about this. Think it might be
something to do with precision?


// calculate amount of deposit for each of ten years
for ( int year = 1; year <= 10; year ++ ) {

//calculate interest rates
for ( int rate = 5; rate <=10; rate ++) {

// calculate new amount for specified year
amount = principal * Math.pow( 1.0 + rate, year );

This line is going to give you problems, ask yourself the question what
value does 1.0 + rate give me for each value of rate and what value do
I want it to give me?
 
S

Steve Bosman

Rajah said:
By the way, the "rate <= 10" will end the loop with rate = 11. I don't
know if you want to write this as "rate < 10" instead.

No it won't - try it

Steve
 
S

Steve Bosman

shannon said:
Should I use double instead of int?
(double rate =5; rate > 10; rate ++)

I'd strongly recommend never using doubles in loops, for this problem
it isn't too much of an issue, but eventually the practice will lead
you into trouble.

Steve
 
R

Rajah

You're right, Steve. I stand corrected.

Shannon, if you wrote the for loop as
for ( double rate = 5; rate <=10; rate ++)
then it would usually go through the loop the last time with rate =
10.0.

Steve's warning probably refers to the notion that if it looped a large
number of times or using a very small increment (like .01) then you
might end at 10.0, or you might end at something lower.

I tried this code:
for (double rate = 5; rate <= 10; rate+= .01)
System.out.println("Value: " + rate );
and the last time through, rate was 9.999999999999893.

If you're going from .05 to .10 by .01, you probably won't face this
kind of roundoff error. Your program results would look correct,
especially with the moneyFormat function rounding things off.

(Why is there roundoff error? In part, it is because numbers that look
even in our decimal system, like 0.1, cannot be represented exactly in
binary, because they have repeating digits.)

In contrast with doubles, ints are represented exactly. If you and your
prof don't mind being off by a tiny fraction, or if your moneyFormat
function is going to round off correctly, then your for loop with the
double will work just fine.

Best wishes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top