//CIS 790 L.A.T.T.C PROF. AUCIELLO // Programmed by Joseph Auciello, CIS Professor, LA Trade Technical College. // a38 /* w TOPICS: rounding / formatting. [To round at the decimal point, multiply by 100, use Math.round, then divide by 100] making variables global (front of program). system input (kb) & output (display) casting. conversion: int to char (char) string to integer to float (talk about shortcut). convert float to string (for drawString). convert string to double (wow!). achieve floating point (decimal) output. input floating point variables using textfields. gettext() -- important. x, y printing. error-trapping {try.. catch..} = [IO Exception] */ import corejava.*; 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.lang.Math; import java.lang.String; import java.util.Date; import java.util.Random; import java.util.*; public class a38 extends Applet { // all definitions and initializations can go here. StringBuffer saveinfo = new StringBuffer(); Font font2 = new Font("TimesRoman", Font.ITALIC, 20); Font font3 = new Font("Helvetica", Font.BOLD, 20); Font font5 = new Font("Symbol", Font.BOLD, 20); // add new initialization statements: TextField inputField0,inputField1,inputField2,inputField3; Button button1; // ...... button. int i = 1, n1, n2, ver = 140; double dintrate, monthlyInterest, payment, principal, y,y2; // defines variable textFields // 'awt' (abstract windows toolkit) contains // components, such as: Buttons, Text Fields, etc. // end of new statements. public void start() { // add new init() statements. // Label namelabel0 = new Label(""",Label.RIGHT); // add (namelabel1); // def and add label. inputField0 = new TextField(80);// create, add, and add (inputField0); // initialize input field1. inputField0.setText(" LOAN ANALYZER JAVA APPLET PROGRAMMING (CIS 790) PROF. J. AUCIELLO"); Label namelabel4 = new Label("Mortgage Amount",Label.RIGHT); add (namelabel4); // def and add label. inputField1 = new TextField(7); // create, add, and add (inputField1); // initialize input field1. inputField1.setText("250000"); Label namelabel5 = new Label("Number of Years",Label.RIGHT); add (namelabel5); inputField2 = new TextField(2); add (inputField2); inputField2.setText("30"); // same as above. Label namelabel6 = new Label("Interest Rate(n.nn)",Label.RIGHT); add (namelabel6); inputField3 = new TextField(5); add (inputField3); inputField3.setText("7.50"); // same as above. System.out.println("done entering .. about to click start"); button1 = new Button("Start"); // def button. add(button1); // end new statements. } // end init() public void paint(Graphics g) { Font fonts = new Font("Helvetica",Font.BOLD,12); g.setFont(fonts); g.setColor(new Color(22,22,225)); // base color // add new paint() statements. g.setFont(fonts); g.drawString("(no commas)",39,65); g.setColor(new Color(25,25,245)); // base color try { String s1 = inputField1.getText(); String s2 = inputField2.getText(); String s3 = inputField3.getText(); principal = Integer.parseInt(s1); int years = Integer.parseInt(s2); // g.drawString("Converted int " + years + " from String " + s2, 20,105); dintrate = Double.valueOf(inputField3.getText()).doubleValue(); // g.drawString("Converted double " + principal + " from String " + s1, 20,90); // g.drawString("Converted int " + years + " from String " + s2, 20,105); // g.drawString("Converted double " + dintrate +" from String "+ s3, 20,120); dintrate = dintrate/100; // g.drawString(" "+saveinfo[0], 240, 160); // g.drawString(" "+saveinfo[1], 240, 180); y = dintrate; i = 0; { monthlyInterest = y / 12; // g.drawString("Calculating ... y = "+y, 240, ver); System.out.println("p= " + principal + " mi=" + monthlyInterest); payment = principal * monthlyInterest / (1-(Math.pow((1/(1 + monthlyInterest)),years*12))); System.out.println ("y= "+y+" payment = "+ payment +" in paint() ... about to calculate"); double y1 = (Math.round(y*1000000)); y2 = y1/10000; double y3 = (Math.round(y*1000000)) / 10000; g.drawString("At rate " + y2+ " %, your payment is $"+Math.round(payment),20,ver+20); // saveinfo[i] = "At" + y2+ " %, your payment is $"+Math.round(payment); // i ++; // y += 0.00100; } // g.drawString("y="+Math.round(y)+" dintrate="+Math.round(dintrate)+" mInt="+Math.round(monthlyInterest)+" Payment="+Math.round(payment),250,120); System.out.println ("y= "+y+" resetting y .. done calculating."); y = dintrate - 0.01; i = 1; System.out.println ("was y reset? y= "+y+"i= "+i); } catch (Exception e) { String err = e.toString(); g.drawString("In Error-Trapping Routine. ",320,160); g.drawString(" Check Divide by 0 Error ",320,180); g.drawString(" " + err,320,200); } // catch // end new paint() statements. } // end paint() public boolean action(Event event, Object arg) { repaint(); // insert 3 gettext fields here. String s1 = inputField1.getText(); String s2 = inputField2.getText(); String s3 = inputField3.getText(); principal = Integer.parseInt(s1); int years = Integer.parseInt(s2); dintrate = Double.valueOf(inputField3.getText()).doubleValue(); // end insert gettext fields. i = 1; // repaint(); return true; } } // end class // ===================== END OF PROGRAM =========================== /* Note to students: you must interact with these programs to fully understand them. Download the code, make changes, and track the results. By planning-doing-testing-verifying, you will gain critically important programming skills that will speed your ability to program. JA. 06.08.99 */