CIS 790 JAVA Distance Education QUIZ -05

Prof. Auciello's Virtual Classroom Quiz

(c) 2001 by Joseph Auciello, California Technical Training

9 - 11 class = Section 0175

Last.First Name:

 


Q001: CIS 790 LOAN CALCULATOR PROF. AUCIELLO Topics: 1. variable interest rate (0.00125) 2. Format.print (controlled formatting) 3. KB Input 4. for loop ... 5. changing parameters in a for-loop. 6. math.pow method (powerful procedure) 7. what does 'amortization' mean 8. formula for calculating payments -------------------------------------------------------------- Write the java instructions to create the following output: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Enter Principal Amount (no commas): 157000 Interest rate in % (use 7.5 for 7.5%): 7.00 The number of years: 30 With rate 6.000 your payment is $ 941.29 With rate 6.100 your payment is $ 951.41 With rate 6.200 your payment is $ 961.58 With rate 6.300 your payment is $ 971.79 With rate 6.400 your payment is $ 982.04 With rate 6.500 your payment is $ 992.35 With rate 6.600 your payment is $ 1002.69 With rate 6.700 your payment is $ 1013.09 With rate 6.800 your payment is $ 1023.52 With rate 6.900 your payment is $ 1034.00 With rate 7.000 your payment is $ 1044.52 With rate 7.100 your payment is $ 1055.09 With rate 7.200 your payment is $ 1065.70 With rate 7.300 your payment is $ 1076.35 With rate 7.400 your payment is $ 1087.04 With rate 7.500 your payment is $ 1097.77 With rate 7.600 your payment is $ 1108.54 With rate 7.700 your payment is $ 1119.35 With rate 7.800 your payment is $ 1130.20 With rate 7.900 your payment is $ 1141.08 With rate 8.000 your payment is $ 1152.01 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Note that printout starts with 1 point below and ends with 1 point above current interest rate. ........................................................... Explain fully the (12) statements that are numbered below: 1. A storage area containing 64 bytes is set up. 2. ........................................................... 1. Start program with an optional comment that describes it. 2. Link with libraries. 3. Name program as "loancalc"; the name of the class object. 4. Open bracket. followd by a statement that means this class will be available to other classes, and objects in same workspace; that means this method initializes and does not return a result. That has a main method, always declared public, and has as its only parameter an array of Strings type used for command line parameters. 5. Begin bracket that defines 'main' class; define principal and yearlyInterest as Doubleword, floating point variables. 6. define years as an integer. 7. Prompt the user to enter the loan amount, read it as a doubleword, and stuff it into "principal". 8. Read in yearlyinterest from keyboard and divide by 100 before assigning it to a variable. 9. Accept value for years from console. 10. Define y as a doubleword. 11. Begin a loop that starts 1 point below yearlyinterest, ends with 1 point above, and autoincrements by 1/10 of a point. 12. Inside brackets, calc monthlyinterest = to yearlyinterest/12. 13. Calculate payment (2 line formula). 14. Print out rate in a formatted field, without a linefeed. 15. Print out payment in a formatted field, without linefeed. 16. Print a blank line [line feed]: (advances line). 17. Close innermost bracket (loop) 18. Close next set of brackets. 19. Close outermost set of brackets. .................................................................. // CIS 790 JAVA PROGRAMMING PROF. AUCIELLO import java.awt.*; import java.awt.event.*; import java.applet.*; import java.awt.Graphics; import java.awt.Font; import java.awt.Color; import java.awt.Image.*; import java.lang.Double; import java.lang.Integer; import java.io.*; import java.lang.Math; import java.lang.String; import java.text.*; import java.util.Date; import java.util.Random; import java.util.*; class loancalc { public static void main(String args[]) { byte buff1[] = new byte[64]; 1 byte buff2[] = new byte[64]; byte buff3[] = new byte[64]; double principal; double yearlyInterest; 2 DecimalFormat df1 = new DecimalFormat("###,##0.00"); DecimalFormat df2 = new DecimalFormat("###,##0"); 3 try { System.out.print ("Enter Loan Amount (no decimals): "); 4 System.in.read(buff1, 0, 64); String str1 = new String(buff1,0); // System.out.println("Loan Amount ...................... : " + str1); // int l1 = str1.length(); // System.out.println("Length of str1 = " + l1); 5 String str2 = str1.trim(); // System.out.println("Len of str2 = " + str2.length()); 6 principal = Integer.parseInt(str2); // int i6 = i5 + 1; 7 System.out.println(" Loan Amount ....................." + (int)principal); // System.out.println(i2+1); System.out.print ("Enter Term in Months: "); System.in.read(buff2, 0, 64); String str3 = new String(buff2,0); // System.out.println("Term in Months ................ : " + str3); String str4 = str3.trim(); int months = Integer.parseInt(str4); System.out.println("Term in Months ......................" + months); System.out.print ("Enter APR [eg: 6.00] ............. : " ); System.in.read(buff3, 0, 64); String str5 = new String(buff3,0); String str6 = str5.trim(); 8 double d4 = Double.parseDouble(str6); // System.out.println("d4 = " + d4); yearlyInterest = d4 / 100.; System.out.println("Yearly Interest Rate ............... " + df1.format(100 * yearlyInterest)+"%"); double y; 9 for (y= yearlyInterest - 0.01; y<=yearlyInterest + 0.011; y += 0.00100) { double monthlyInterest = y / 12; // System.out.println("monthlyInterest = " + monthlyInterest); double payment = principal * monthlyInterest / (1-(Math.pow((1/(1 + monthlyInterest)),months))); 10 System.out.print(" With rate " + df1.format(100*y)+"%"); System.out.print(" your payment is $" + df2.format(payment)); System.out.println(" "); } // end for } // end try. catch (Exception e) { String err = e.toString(); System.out.println(err); } // end catch. } // end main procedure. } // end class 11 EXPLAIN try ... catch. 12 WHAT DOES (int)principal DO (LINE 7)? // end of program ...................................................................