/* CIS 790 PROGRAMMING PROJECT (36) PROF. AUCIELLO ASSIGNMENT: SHOW OFF YOUR PROGRAMMING SKILLS BY CONVERTING AN APPLICATION TO AN APPLET. ======================================================================== APPLET CREATES THIS OUTPUT: (srr) Index Pointer = 0 Contents = Hello World! (srr) Index Pointer = 1 Contents = Demos loops, arrays, printing from arrays. (srr) Index Pointer = 2 Contents = I hope this is worth it ... (srr) Index Pointer = 3 Contents = End of String Array. Starting Numeric Array. 1 2 3 5 8 13 21 -- stored internally numbers printed out from array. (arr) Index Pointer = 0 Contents = 1 (arr) Index Pointer = 1 Contents = 2 (arr) Index Pointer = 2 Contents = 3 (arr) Index Pointer = 3 Contents = 5 (arr) Index Pointer = 4 Contents = 8 (arr) Index Pointer = 5 Contents = 13 (arr) Index Pointer = 6 Contents = 21 Control goes here only when an error is 'caught'! Index = 7 is Out of Range ================================================================= // -- THIS IS THE APPLICATION -- // THIS RUNS AS A JAVA APPLICATION. import java.io.*; class x21 { public static void main (String args[]) { int i; int arr[] = {1,2,3,5,8,13,21}; // 5: String srr[] = {"Hello World!", "Demos loops, arrays, printing from arrays.", "I hope this is worth it ...", "END OF STRING ARRAY. START NUMERIC ARRAY." } for (i = 0; i < 4; i++) // 11: { System.out.println("Index Pointer = " + i + " Contents = " + srr[i]); } System.out.println("1 2 3 5 8 13 21 -- stored internally"); System.out.println("numbers printed out from array."); try { for (i = 0; i < 8; i++) { System.out.println("Index Pointer = " + i + " Contents = " + arr[i]); } System.out.println("Very, very cool! "); } catch (Exception e) { System.out.println("Control goes here only when an error is 'caught'!"); System.out.println("Index = " + i + " is Out of Range"); } // end catch } // end main } // end class // -- END OF JAVA APPLICATION -- // ============================================================= // -- START OF JAVA APPLET -- 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.*; import java.io.*; 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() { 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"); System.out.println("done entering .. about to click start"); button1 = new Button("Start"); // def button. add(button1); } // end start() public void paint(Graphics g) { Font fonts = new Font("Helvetica",Font.BOLD,12); g.setFont(fonts); g.drawString("(no commas)",39,65); g.setColor(new Color(25,25,245)); // base color try { String s1 = inputField1.getText(); principal = Integer.parseInt(s1); int years = Integer.parseInt(s2); dintrate = Double.valueOf(inputField3.getText()).doubleValue(); dintrate = dintrate/100; y = dintrate; i = 0; monthlyInterest = y / 12; 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); 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); } // end try 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); } // end catch } // end paint() } // end class // ===================== END OF APPLET =========================== /* CLUES FOR SUCCESS: 1. NOTE THE STRUCTURE OF THE APPLET: import public class name { definitions / initializations public void start() method (this can be eliminated -- not needed.) public void paint (Graphics g) { set up fonts and color (g.setColor) try { print two "for" loops. (take from APPLICATION) } // end try catch { statements to capture and print error. (take from APPLICATION) } // end catch } // end paint method } // end class // ========================================================= THE COMPLETED EXECUTABLE APPLET IS BELOW USE IT TO CHECK YOUR PROGRESS. // APPLICATION CONVERTED TO APPLET! 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.*; import java.io.*; public class c2 extends Applet { Font fonts = new Font("Helvetica",Font.BOLD,12); int i; int arr[] = {1,2,3,5,8,13,21}; // 5: String[] srr = {"Hello World!", "Demos loops, arrays, printing from arrays.", "I hope this is worth it ...", "End of String Array. Starting Numeric Array." }; public void paint(Graphics g) { // begin paint method try { g.setFont(fonts); g.setColor(new Color(25,25,245)); // base color g.drawString("APPLET RUNNING!",39,65); g.drawString("Minimize this screen to see Output!",39,95); for ( i = 0; i < 4; i++) // 11: { System.out.println("(srr) Index Pointer = " + i + " Contents = " + srr[i]); } System.out.println("1 2 3 5 8 13 21 -- stored internally"); System.out.println("numbers printed out from array."); for (i = 0; i < 8; i++) { System.out.println("(arr) Index Pointer = " + i + " Contents = " + arr[i]); } System.out.println("Very, very cool! "); // 17: } // end try catch (Exception e) { System.out.println("Control goes here only when an error is 'caught'!"); System.out.println("Index = " + i + " is Out of Range"); } // end catch } // end paint } // end class // ===================== END OF APPLET =========================== 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 */