/** * Title: Chapter 3, "Control Statements" * Description: Chapter 3 Examples * Copyright: Copyright (c) 2000 * Company: Armstrong Atlantic State University * @author Y. Daniel Liang * @version 1.0 */ // TestIfElse.java: Test if-else statements public class TestIfElse { /**Main method*/ public static void main(String[] args) { double annualInterestRate = 0; int numOfYears; double loanAmount; // Enter number of years System.out.print( "Enter number of years (7, 15 and 30 only): "); numOfYears = MyInput.readInt(); // Find interest rate based on year if (numOfYears == 7) annualInterestRate = 7.25; else if (numOfYears == 15) annualInterestRate = 8.50; else if (numOfYears == 30) annualInterestRate = 9.0; else { System.out.println("Wrong number of years"); System.exit(0); } // Obtain monthly interest rate double monthlyInterestRate = annualInterestRate/1200; // Enter loan amount System.out.print("Enter loan amount, for example 120000.95: "); loanAmount = MyInput.readDouble(); // Compute mortgage double monthlyPayment = loanAmount*monthlyInterestRate/ (1-(Math.pow(1/(1+monthlyInterestRate), numOfYears*12))); double totalPayment = monthlyPayment*numOfYears*12; // Display results System.out.println("The monthly payment is " + (int)(monthlyPayment*100)/100.0); System.out.println("The total payment is " + (int)(totalPayment*100)/100.0); } } // a class for reading various types from the keyboard (System.in) class MyInput { public static String readString() { String string = ""; java.io.BufferedReader bufferedReader = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); try { string = bufferedReader.readLine(); } catch (java.io.IOException ex) { throw new RuntimeException(ex); } return string; } public static int readInt() { return Integer.parseInt(readString()); } public static double readDouble() { return Double.parseDouble(readString()); } // test all the methods of this class public static void main(String[] args) { System.out.println("Testing 'readString()'"); System.out.print("Input your string : "); System.out.println("Your string was '" + readString() + "'"); System.out.println("\nTesting 'readInt()'"); System.out.print("Input your int : "); System.out.println("Your int was '" + readInt() + "'"); System.out.println("\nTesting 'readDouble()'"); System.out.print("Input your double : "); System.out.println("Your double was '" + readDouble() + "'"); } }