![]() |
|
|
|
#1 |
|
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 shannon |
|
|
|
|
#2 |
|
Posts: n/a
|
You could set up an array that contains all the interest rates then
iterate through that. mikm |
|
|
|
#3 |
|
Posts: n/a
|
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 shannon |
|
|
|
#4 |
|
Posts: n/a
|
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. shannon wrote: > 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? Rajah |
|
|
|
#5 |
|
Posts: n/a
|
Should I use double instead of int?
(double rate =5; rate > 10; rate ++) Rajah wrote: > 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. > > shannon wrote: > > 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? shannon |
|
|
|
#6 |
|
Posts: n/a
|
shannon wrote: > 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? > // 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 Steve Bosman |
|
|
|
#7 |
|
Posts: n/a
|
Rajah wrote: > 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 Steve Bosman |
|
|
|
#8 |
|
Posts: n/a
|
shannon wrote: > 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 Steve Bosman |
|
|
|
#9 |
|
Posts: n/a
|
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. Rajah |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| a+ pass rates | J.Leonard | A+ Certification | 6 | 05-09-2004 12:01 AM |
| Re: this is for everyone's interest | Tom MacIntyre | A+ Certification | 0 | 04-24-2004 01:33 PM |
| Re: this is for everyone's interest | A+ Certification | 0 | 04-23-2004 07:24 AM | |
| Speaking of Pay Rates in FLA | MF | A+ Certification | 7 | 10-13-2003 07:26 PM |