/* ****************************************************************************** * Source Code : NineDivisorAxiomApplet.java * * Description : Determine which digit the user circled by using a * * known axiom. * * Version : 1.0.0 * * Programmer : Christopher V. Bellini * * * * ---------------- * * | REVISION LOG | * *----------------------------------------------------------------------------* * Programmers Notes: * * - cvb (07/29/2005): V1.0.0 completed * * * * ///////////////////////////////////////////////////////////////////////////* * This library is free software; you can redistribute it and/or modify it * * under the terms of the GNU Lesser General Public License as published by * * the Free Software Foundation; either version 2.1 of the License, * * or (at your option) any later version. This library is distributed in the* * hope that it will be useful, but WITHOUT ANY WARRANTY; without even the * * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * See the GNU Lesser General Public License for more details. * * * * You should have received a copy of the GNU Lesser General Public * * License along with this library; if not, visit: * * http://www.gnu.org/licenses/lgpl.txt or write to the Free Software * * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA* * ///////////////////////////////////////////////////////////////////////////* *----------------------------------------------------------------------------* */ import java.applet.*; import java.awt.*; import java.lang.*; import java.awt.event.*; import java.util.*; public class NineDivisorAxiomApplet extends Applet implements ActionListener { private Button cmdEnterButton; private TextField txtNumber; private String strResult; private String strNumberEntered; private boolean bError; // Purpose: Initialize the applet. // I: (none) // O: (none) public void init() { // Use a FlowLayout to hold a text field for the user to enter their // number into and a button to begin the "guessing". setLayout(new FlowLayout()); txtNumber = new TextField("", 4); cmdEnterButton = new Button("Enter"); add(txtNumber); add(cmdEnterButton); // Attach action listener to the "Enter" button. cmdEnterButton.addActionListener(this); } // Purpose: Process onPaint events in the applet's window. // I: Graphics object // O: (none) public void paint(Graphics g) { // If the user didn't enter a 2 or 3 digit number // or they tried to enter non-digits, then set // set the font colour to red to show that we // mean business. // // Write the result to the screen. if (bError == true) g.setColor(Color.red); g.drawString(strResult, 20, 80); } // Prupose: Listen for when events are fired. // I: ActionEvent object // O: (none) public void actionPerformed(ActionEvent evt) { String strMissingNumber; int iMissingNumber; // If the user clicked the "Enter" button, retrieve the number and // check to see if a valid number was entered (contains only 2 or // 3 digits). Store an error message in the result string if the // number wasn't valid. If the number was valid, determine which // digit the user circled. if (evt.getSource() == cmdEnterButton) { strNumberEntered = txtNumber.getText(); if ((strNumberEntered.length() < 2) || (strNumberEntered.length() > 3)) { bError = true; strResult = "Error: Enter a 2 or 3 digit number."; } else if (!IsNumeric(strNumberEntered)) { bError = true; strResult = "Error: Numbers only, please."; } else { bError = false; iMissingNumber = DetermineDigit(Integer.parseInt(strNumberEntered), strNumberEntered.length()); strResult = "Chris predicts your number is " + Integer.toString(iMissingNumber) + "."; } repaint(); } } // Purpose: Find which digit the user circled by using the // axiom that states that the sum of the digits of a // 3 or 4 digit number is always evenly divisble by 9. // I: number entered // I: number of digits in the number entered // O: the circled digit public int DetermineDigit(int iEntered, int iLen) { int iSum = 0; // Sum up the digits of the number. while (iEntered > 0) { iSum += (iEntered % 10); iEntered /= 10; } // Find which digit, when added to the existing sum of // digits of the entered number, results in a new number // evenly divisible by 9. for (int j = 1; j <= 9; j++ ) { if (((iSum + j) % 9) == 0) { return j; } } return -1; } // Purpose: Determine if the entered string value contains // only numeric values. // I: string // O: true=numeric, false=contains non-numeric characters public boolean IsNumeric(String var) { int iNum = 0; boolean bNumeric = false; StringBuffer sbTemp = new StringBuffer(); byte[] b = var.getBytes(); for (int i = 0; i < b.length; i++) { // check if each byte is 0-9 if((int)b[i] < 58 && (int)b[i] >= 48) { // add it to the StringBufer as a char sbTemp.append((char)b[i]); bNumeric = true; } else { bNumeric = false; // non-numeric character found, don't continue with this string break; } } return bNumeric; } }