/*
CIS 790 JAVA PROGRAMMING PROF. AUCIELLO
a30d6.java a30d6.html a30d6txt.html
TOPICS: input fields.
start button.
program tracing / debugging.
casting (conversion) = string to int to float.
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.Math;
import java.util.Date;
import java.util.Random;
import java.util.*;
public class a30d6 extends Applet
{ // all definitions and initializations can go here.
TextField inputField1,inputField2; // define input fields.
Button button1; // ...... button.
//
int n1, n2; // ...... integers.
// defines variable textFields
// 'awt' (abstract windows toolkit) contains
// components, such as: Buttons, Text Fields, etc.
Font font2 = new Font("TimesRoman", Font.ITALIC, 20); // def. fonts.
Font font5 = new Font("Symbol", Font.BOLD, 20); // .... ......
public void init() // start at init() time.
{ Label namelabel1 = new Label("Enter Number 1: ",Label.RIGHT);
add (namelabel1); // def and add label.
inputField1 = new TextField(6); // create, add, and
add (inputField1); // initialize input field1.
inputField1.setText("100");
Label namelabel2 = new Label("Number 2:",Label.RIGHT);
add (namelabel2);
inputField2 = new TextField(6);
add (inputField2);
inputField2.setText("3"); // same as above.
System.out.println("done entering .. about to click start");
button1 = new Button("Start"); // def button.
add(button1);
} // end init()
public void paint(Graphics g)
{ Font fonts = new Font("Times New Roman",Font.BOLD,10);
g.setFont(fonts);
g.setColor(new Color(25,25,245)); // base color
g.drawString("Line 61: linked to libraries, textfields setup and data entered.",20,45);
g.drawString("Line 62: fonts (58,59) and color (60) set up.",20,60);
g.drawString("Line 63: Press or click 'Start' when done.",20,80);
try
{ String s1 = inputField1.getText();
String s2 = inputField2.getText();
g.drawString("67: Got strings (" + s1 + ", " + s2 + ") from input fields.",20,100);
n1 = Integer.parseInt(s1);
n2 = Integer.parseInt(s2);
g.drawString("70: Converted integers (" + n1 +", " + n2 + " from
Strings (" + s1 +", "+s2+")", 20,120);
System.out.println ("in paint() ... about to calculate");
g.drawString("72: About to divide Number1 by Number2 and test for div by 0.", 20, 140);
double n3 = ((100*n1)/n2);
g.drawString(" "+n1+" / "+n2+ " = " +(n3/100), 20,160);
g.drawString("73-74: Note manipulation of decimal point to give correct answer.",20,180);
g.drawString("This Applet accepts (2) string inputs, waits for an
or button-click ...", 20,200);
g.drawString("converts them to integers, and attempts division.",20,220);
g.drawString("If successful, result is displayed ...",20,240);
g.drawString("If unsuccessful, control passes to 'catch' routine where error
messages are displayed.",20,260);
}
catch (Exception e)
{ String err = e.toString();
g.drawString("84: In Error-Trapping Routine. ",20,160);
g.drawString("85: Now you know, you can't divide by 0! ",20,180);
g.drawString("86: " + err,20,200);
} // catch
} // end paint.
public boolean action(Event event, Object arg)
{ repaint();
System.out.println("92: Listening for Event .... ");
return true;
}
} // end class
/* 65-66: read string data from input fields.
68-69: convert string data to integers.
73-74: calculate answer, and manipulate decimal position.
83-87: what to do if there is a divide error.
*/
|