Google
 
Site navigation: [ Home | Theory | Java | Moodle courses | Resource wiki | About ]

Binary Quiz

 

 

 

 

This is quite a long Applet and involves many Classes which have been developed in previous pages.

It will repay some study.

If you can modify the Applet successfully you will learn a lot.

 

 

 

 

 

 

 

 

 

Nothing new here, just a bit longer than usual.

 

 

 

 

 

 

Call the method that generates a question.

 

 

 

 

Notice that the main logical structure is put here, the actual processing parts are handled by separate methods for convenience.

 

 

Answered is a flag or sentinel - an SL mastery aspect.

 

 

 

 

 

 

Use a try block because the BinaryTextFiedl will throw an exception if the String typed into it is not a valid binary byte.

 

 

We can then use our custom exception messages.

 

 

 

We've used a method here because it will make it easier to extend the example to Binary Decimal conversions or mixtures of both.

 

 

 

 

Start with an empty String

 

Generate random number 0 - 499

3/5 of the numbers will be 1's on average.

 

 

 

 

Math.random returns a long primitive in the range 0 >= x > 1.0

(int) is a cast used to convert a long to an int.

 

 

 

sources:

BinaryQuiz.java , BinaryTextField.java , IntegerTextField.java , BinaryTextFieldException.java .

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

/**
* Implements a test for simple unsigned binary numbers
* It uses BinaryException and BinaryTextField Classes
*
* @author Mr J
* @version 20040221
*/

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
   */

   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 question (integer value)
   * disable the TextField holding the question
   */

   private void generateQuestion()
   {
     // decimal 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 = s + "0";
       }
       else
       {
         s = 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));
   }
}

Exercise:

The quiz needs improvement in the following areas:

  • It should keep score for a person and only give the results at the end of the quiz
  • It should test (perhaps randomly) binary to decimal conversion as well

Related: [ Java Home | previous:Inheritance ]

Applet that runs a binary conversion quiz.

You have to enter a valid , 8-bit binary byte .

For example , if you are asked to convert 2, enter 00000010, not just 10.


 
The site is partly financed by advertising revenue, partly by online teaching activities and partly by donations. If you or your organisation feel these resouces have been useful to you, please consider a donation, $9.95 is suggested. Please report any issues with the site, such as broken links, via the feedback page, thanks.

Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy.

Creative Commons License


This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2009 Richard Jones, PO BOX 246, Cambridge, New Zealand;
This page was last modified: May 31, 2009