/* * ============================================================================= * CompoundInterest.java : This java program demonstrates use of a "for looping" * construct to compute compound interest .... * * Written By: Mark Austin April, 2004 * ============================================================================= */ import java.lang.Math; import java.util.*; import java.io.*; import java.text.*; class CompoundInterest { public static void main( String args[] ) { float fPrincipal, fInterestRate, fNoYears; float fs, farea, fMax; String sLine; // Print welcome message. System.out.println("Welcome to the Compound Interes Program"); System.out.println("---------------------------------------"); // Prompt user for coefficients in quadratic equation. System.out.print("Input Principal: "); sLine = keyboardInput(); fPrincipal = Float.valueOf(sLine).floatValue(); System.out.print("Input Interest Rate: "); sLine = keyboardInput(); fInterestRate = Float.valueOf(sLine).floatValue(); System.out.print("Input No Of Years: "); sLine = keyboardInput(); fNoYears = Float.valueOf(sLine).floatValue(); // Print details of the triangle edges ..... System.out.println("The parameter values you have entered are: "); System.out.println("Principal = " + fPrincipal ); System.out.println("Interest Rate = " + fInterestRate ); System.out.println("No Years = " + fNoYears ); // Check that problem parameters are greater than zero ... if ( fPrincipal < 0.0 || fInterestRate < 0.0 || fNoYears < 0.0 ) { System.out.println("ERROR: Input parameters must be positive!"); return; } // Check that no of years is an integer ... if ( fNoYears - Math.round( fNoYears) > 0.0 ) { System.out.println("ERROR: No of years must be an integer!"); return; } // Use for loop to compute economic gain .... float fFactor = 1.0F; for ( int i = 0; i < (int) fNoYears; i = i + 1 ) fFactor = fFactor * ( 1.0F + fInterestRate ); float fF = fPrincipal * fFactor; // Format output to two decimal places of accuracy ... DecimalFormat formatSum = new DecimalFormat( "##.##" ); String output1 = formatSum.format( fF ); System.out.println("========================"); System.out.println("Future Sum = " + output1 ); } /* * =========================================================== * Method keyboardInput() : Get line of input from keyboard * * Input : None. * Output : String sLine -- character string of keyboard input * =========================================================== */ static String keyboardInput() { String sLine; DataInputStream in = new DataInputStream(System.in); try{ sLine = in.readLine(); return sLine; } catch (Exception e){ return "error"; } } }