/* * File: CDInterest.java * Author: Java, Java, Java * Description: This application program illustrates the use of Java library * classes to perform certain useful calculations. * It uses the java.lang.Math class to calculate the principal of a * Certificate of Deposit (CD) invested at a certain interest rate for a certain * time period. It uses the java.text.NumberFormat class to format the numerical results * which represent dollar amounts and percentages. */ import java.io.*; // Import the Java I/O Classes import java.text.NumberFormat; // For formatting as $nn.dd or n% public class CDInterest { private BufferedReader input = new BufferedReader // Handles console input (new InputStreamReader(System.in)); private String inputString; // Stores the input private double principal; // The CD's initial principal private double rate; // CD's interest rate private double years; // Number of years to maturity private double cdAnnual; // Accumulated principal with annual compounding private double cdDaily; // Accumulated principal with daily compounding /** * convertStringTodouble() converts a String of the form "54.87" into * its corresponding double value (54.87). * @param s -- stores the string to be converted * @return A double is returned */ private double convertStringTodouble(String s) { Double doubleObject = Double.valueOf(s) ; return doubleObject.doubleValue(); } /** * getInput() handles all the input required by the program. It inputs * three Strings and converts each to a (double) number, storing them * in instance variables (principal, rate, and years). */ private void getInput() throws IOException { // Prompt the user and get the input System.out.println("This program compares daily and annual compounding for a CD."); System.out.print(" Input the CD's initial principal, e.g. 1000.55 > "); inputString = input.readLine(); principal = convertStringTodouble(inputString); System.out.print(" Input the CD's interest rate, e.g. 6.5 > "); inputString = input.readLine(); rate = (convertStringTodouble(inputString)) / 100.0; System.out.print(" Input the number of years to maturity, e.g., 10.5 > "); inputString = input.readLine(); years = convertStringTodouble(inputString); } //getInput() /** * calcAndReportResults() handles all the interest calculations and reports * the program's results. Note the use of Math.pow() to calculate the CD's * value, and the use of the NumberFormat.format() methods for making the output * look pretty. */ private void calcAndReportResult() { // Calculate and output the result NumberFormat dollars = NumberFormat.getCurrencyInstance(); // Set up formats NumberFormat percent = NumberFormat.getPercentInstance(); percent.setMaximumFractionDigits(2); cdAnnual = principal * Math.pow(1 + rate, years); // Calculate interest cdDaily = principal * Math.pow(1 + rate/365, years*365); // Print the results System.out.println("The original principal is " + dollars.format(principal)); System.out.println("The resulting principal compounded daily at " + percent.format(rate) + " is " + dollars.format(cdDaily)); System.out.println("The resulting principal compounded yearly at " + percent.format(rate) + " is " + dollars.format(cdAnnual)); } // calcAndReportResult() /** * main() creates an instance of the CDInterest object and then invokes * its methods to calculate the interest for a CD using values input by * the user. */ public static void main( String args[] ) throws IOException { CDInterest cd = new CDInterest(); cd.getInput(); cd.calcAndReportResult(); } // main() } // Interest