/*-------------------------------------------------------------------* * NTU Student: Java With Engineering Applications, Spring 1999 * Assignment # 2 - Java How To Program 2nd edition, Deitel & Deitel * Problem 4.31 on page 223 * * Michael Kusmierz * Xerox Corporation * Rev 1.0 Apr 14, 1999 * * This code represents a students work in progress * and is not to be used beyond that purpose. *-------------------------------------------------------------------*/ import java.applet.*; import java.awt.*; import java.awt.event.*; /** * This type was created in VisualAge. * Learn - is an educational program that generates two random numbers * and the user is asked to multiply them. Thier answer is verified. * A correct answer by the user results in the MultiplicationQuestion * method to be called again, otherwise the user is just prompted to try * again. Uses: Status Bar, Text field and Text Area. */ public class Learn extends Applet implements ActionListener { private Label ivjlblAppletTitle = null; private Label ivjlblIPrompt = null; private TextField ivjtxtFieldAnswer = null; private TextArea ivjtxtArea = null; //---instance variables private int iRndNum1, iRndNum2; //Random numbers to be multiplied private int iUserInputAns; //User input private int iTestStatus; //init,pass,fail private final int k_PASS = 0; private final int k_FAIL = -1; private final int k_INIT = 1; //---carriage return and line feed constant //String crlf = System.getProperties().getProperty("line.separator"); //- THE ABOVE WORKS in IBM VisualAge & DOES NOT WORK ON JDK 1.2 APPLETVIEWER String crlf = "\n"; public void actionPerformed( ActionEvent e) { try { //--- get the users guess iUserInputAns = Integer.parseInt(ivjtxtFieldAnswer.getText()); //--- compute answer and verify results.. display to user if ( (iRndNum1 * iRndNum2) == iUserInputAns) iTestStatus = k_PASS; else iTestStatus = k_FAIL; //--- verify answer in paint repaint(); } catch (Throwable exception) { iTestStatus = k_FAIL; } } /** * Gets the applet information. * @return java.lang.String */ public String getAppletInfo() { return "MultiplicationPkg.Learn created using VisualAge for Java."; } /** * Return the lblAppletTitle property value. * @return java.awt.Label */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private Label getlblAppletTitle() { if (ivjlblAppletTitle == null) { try { ivjlblAppletTitle = new java.awt.Label(); ivjlblAppletTitle.setName("lblAppletTitle"); ivjlblAppletTitle.setText("Learn Multiplication"); ivjlblAppletTitle.setBounds(12, 8, 117, 23); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjlblAppletTitle; } /** * Return the lblIPrompt property value. * @return java.awt.Label */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private Label getlblIPrompt() { if (ivjlblIPrompt == null) { try { ivjlblIPrompt = new java.awt.Label(); ivjlblIPrompt.setName("lblIPrompt"); ivjlblIPrompt.setText(" Your answer:"); ivjlblIPrompt.setBounds(9, 31, 135, 24); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjlblIPrompt; } /** * Return the txtArea property value. * @return java.awt.TextArea */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private TextArea gettxtArea() { if (ivjtxtArea == null) { try { ivjtxtArea = new java.awt.TextArea(); ivjtxtArea.setName("txtArea"); ivjtxtArea.setBounds(10, 73, 197, 73); ivjtxtArea.setEditable(false); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjtxtArea; } /** * Return the txtFieldAnswer property value. * @return java.awt.TextField */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private TextField gettxtFieldAnswer() { if (ivjtxtFieldAnswer == null) { try { ivjtxtFieldAnswer = new java.awt.TextField(); ivjtxtFieldAnswer.setName("txtFieldAnswer"); ivjtxtFieldAnswer.setBounds(147, 30, 58, 24); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjtxtFieldAnswer; } /** * Called whenever the part throws an exception. * @param exception java.lang.Throwable */ private void handleException(Throwable exception) { /* Uncomment the following lines to print uncaught exceptions to stdout */ // System.out.println("--------- UNCAUGHT EXCEPTION ---------"); // exception.printStackTrace(System.out); } /** * Handle the Applet init method. */ public void init() { super.init(); try { setName("Learn"); setLayout(null); setSize(215, 153); add(getlblIPrompt(), getlblIPrompt().getName()); add(gettxtFieldAnswer(), gettxtFieldAnswer().getName()); add(getlblAppletTitle(), getlblAppletTitle().getName()); add(gettxtArea(), gettxtArea().getName()); // user code begin {1} //--- Initialize instance variables iTestStatus = k_INIT; //--- Wait for an answer from the user ivjtxtFieldAnswer.addActionListener(this); //--- Start the learning game in paint //multiplicationQuestion(); // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } } /** * main entrypoint - starts the part when it is run as an application * @param args java.lang.String[] */ public static void main(java.lang.String[] args) { try { Frame frame; try { Class aFrameClass = Class.forName("com.ibm.uvm.abt.edit.TestFrame"); frame = (Frame)aFrameClass.newInstance(); } catch (java.lang.Throwable ivjExc) { frame = new Frame(); } Learn aLearn; Class iiCls = Class.forName("Learn"); ClassLoader iiClsLoader = iiCls.getClassLoader(); aLearn = (Learn)java.beans.Beans.instantiate(iiClsLoader,"Learn"); frame.add("Center", aLearn); frame.setSize(aLearn.getSize()); frame.setVisible(true); } catch (Throwable exception) { System.err.println("Exception occurred in main() of java.applet.Applet"); exception.printStackTrace(System.out); } } /** * Propose a multiplication question... */ private void multiplicationQuestion() { randomNumber(); showStatus("How much is "+ iRndNum1 + " times " + iRndNum2 + " :"); //---wait for the user to answer (action listener >>> action performed) } /** * Print the results on the applet screen */ public void paint(Graphics g) { //--- display results to the user switch (iTestStatus) { case k_INIT: ivjtxtArea.setText("...waiting for input."+ crlf); multiplicationQuestion(); //showStatus("How much is "+ iRndNum1 + " times " + iRndNum2 + " :"); break; case k_PASS: ivjtxtArea.append(iRndNum1+"x"+iRndNum2+"="+iUserInputAns +" - VERY GOOD!"+ crlf); //--- Since the user is so good...give it another try. ivjtxtFieldAnswer.setText(""); multiplicationQuestion(); break; case k_FAIL: ivjtxtArea.append(iUserInputAns + " - No, Please try again!"+ crlf); //--- Clear the textField ivjtxtFieldAnswer.setText(""); break; } //end switch } /** * Create two random numbers. */ private void randomNumber() { //--- Get random numbers between 0 and 9 iRndNum1 = (int) (Math.random() * 10); //Math.random returns a double iRndNum2 = (int) (Math.random() * 10); //type cast to integer is ok } }