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
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