/** * Implements a test for simple unsigned binary numbers * It uses BinaryException and BinaryTextField Classes * * @author Mr J * @version 20040221 */ import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class BinaryQuiz extends Applet implements ActionListener { // Using our new TextField objects BinaryTextField numberB = new BinaryTextField(20); IntegerTextField numberD = new IntegerTextField(20); // awt interface objects Label messages = new Label("Messages appear here......................."); Button answer = new Button("Check Answer"); Button again = new Button("Try again"); // check if they have answered the question yet boolean answered = false; /** * Add objects to Applet, initialise Choice */ public void init() { // set up panels for the TextFields and Buttons Panel fieldPanel = new Panel(); fieldPanel.setLayout(new GridLayout( 2, 1 ) ); Panel buttonPanel = new Panel(); buttonPanel.setLayout(new GridLayout( 1, 2 ) ); // add to panels fieldPanel.add(numberB); fieldPanel.add(numberD); buttonPanel.add(answer); buttonPanel.add(again); // action Listeners for the buttons answer.addActionListener(this); again.addActionListener(this); // set up the interface add(fieldPanel); add(buttonPanel); add(messages); // start off with a new question generateQuestion(); } /** * When the press button is pressed the answer is checked * When the again button is pressed a new number is generated * * @param e the event that generated the call to this method */ public void actionPerformed(java.awt.event.ActionEvent e) { if (e.getSource() == answer) { if (processAnswer()) { messages.setText("Correct, well done!"); answered = true; } } else { if (answered) { generateQuestion(); answered = false; } else { messages.setText("But you haven't answered this one yet!"); } } } /** * Process the answer * * @return true if the question was answered correctly */ private boolean processAnswer() { try { // numberB holds the answer, check it, an exception will be thrown // if the answer is not valid binary if (numberD.getInteger() == numberB.convert()) { return true; } else { messages.setText("Sorry, please try again"); } } catch(BinaryTextFieldException btfe) { messages.setText(btfe.getMessage()); } return false; } /** * Generate a decimal or binary value, depending on direction * disable the TextField holding the question */ private void generateQuestion() { // decinmal to Binary conversion // Generate decimal number numberD.setText(generateDecimal()); numberD.setEditable(false); } /** * This method generates a binary byte - useful if you want to reverse the quiz (hint, hint) * It returns more 1's than zeros on average * * @return java.lang.String */ private String generateBinary() { String s = ""; int r, p; for (p = 0; p < 8; p++) { r = (int) (Math.random() * 500); if( r >= 300) { s += "0"; } else { s += "1"; } } return s; } /** * This method generates an int in the range 0 to 255 * * @return a representation of the integer */ public String generateDecimal() { return ("" + (int) (Math.random() * 256)); } }