/* This program computes the value of an investment earning simple interest for two years. Inputs: (1) Initial investment; (2)current interest rate. The computation will be based on simple interest compounded annually. The output will include the initial investment, the value after one year and the value after two years.computes compound interest for an investment nd is a refinement of CompoundInterest.java. Changes: Minimal error checking--if user enters an invalid compounding interest period choice, the program displays an error message and ends. */ import java.text.DecimalFormat; import javax.swing.*; import javax.swing.JTextArea; import javax.swing.JOptionPane; import javax.swing.JScrollPane; public class Invest1 { public static void main( String args[] ) { final int YEARS = 2; double principal ; float rate; float interest = 0.0f; //interest each period DecimalFormat precisionTwo = new DecimalFormat( "0.00" ); /*JTextArea is used for multiple lines of output. Arguments below request 17 rows and 20 columms */ JTextArea outputTextArea = new JTextArea( 17, 20 ); //Input principal and interest rate String input; input = JOptionPane.showInputDialog("Enter principal"); principal = Double.parseDouble(input); input = JOptionPane.showInputDialog("Enter rate"); rate = Float.parseFloat(input); //Set up columns for output outputTextArea.append( "INITIAL INVESTMENT: " + "\t" +precisionTwo.format( principal )+ "\n" ); outputTextArea.append("YEAR\t" + "INTEREST\t" + "VALUE\n" ); //compute interest per period and increment principal for ( int year = 1; year <= YEARS; year++ ) { interest = (float)(principal*rate); principal += interest; outputTextArea.append( year + "\t" + precisionTwo.format(interest) + "\t" + precisionTwo.format( principal ) + "\n" ); }//for loop JOptionPane.showMessageDialog( null, outputTextArea, "Compound Interest", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); // terminate the application } }