import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** * This Applet gives a number conversion test, designed for the page: * http://www.ib-computing.com/program/core/number_systems.html * * @author Richard Jones * @version September 2004 */ public class BinDecHexText extends Applet implements ActionListener { // GUI objects private Label explain = new Label( "Click new to get a problem, fill in " + "the empty boxes, press Check answer."); private TextField bin_t = new TextField(10); private TextField dec_t = new TextField(10); private TextField hex_t = new TextField(10); private Label bin_l = new Label("binary:", Label.RIGHT); private Label dec_l = new Label("decimal:", Label.RIGHT); private Label hex_l = new Label("hexadecimal:", Label.RIGHT); private Button bin_b = new Button("new"); private Button dec_b = new Button("new"); private Button hex_b = new Button("new"); private Button check = new Button("Check answer"); private Label messages = new Label("Any system messages will appear here"); // data members private boolean answered; private String currentProblem; private int currentAnswer; /** * Set up the Applet */ public void init() { add(explain); // main Panel Panel main = new Panel(); main.setLayout(new GridLayout(3, 3, 5, 5)); main.add(bin_l); main.add(bin_t); main.add(bin_b); main.add(dec_l); main.add(dec_t); main.add(dec_b); main.add(hex_l); main.add(hex_t); main.add(hex_b); Panel system = new Panel(); // Check answer button and message label system.setLayout(new GridLayout(2, 1, 5, 5)); system.add(check); system.add(messages); add(main); add(system); // Causes button presses to be detected // set the listeners bin_b.addActionListener(this); dec_b.addActionListener(this); hex_b.addActionListener(this); check.addActionListener(this); // Initial states - set a binary problem answered = false; currentProblem = "bin"; currentAnswer = (int) (100 + Math.random() * 156); newBinaryProblem(currentAnswer); } /** * Detects button presses and acts on them * * @param e carries details about the event that occurred */ public void actionPerformed(ActionEvent e) { // see if the question has been answered correctly messages.setText(""); if (e.getSource() == check) { if (checkAnswers()) // problem completed { messages.setText("Yes, well done"); answered = true; clearBoxes(); currentAnswer = (int) (100 + Math.random() * 156); } else { messages.setText("Sorry, not yet, please try again"); answered = false; } } if (answered) // if it was anwered correctly allow a new problem { // check the button presses if(e.getSource() == bin_b) { newBinaryProblem(currentAnswer); currentProblem = "bin"; } else if (e.getSource() == dec_b) { newDecimalProblem(currentAnswer); currentProblem = "dec"; } else if (e.getSource() == hex_b) { newHexProblem(currentAnswer); currentProblem = "hex"; } } } /** * Clears the text boxes and messages */ private void clearBoxes() { bin_t.setEnabled(true); dec_t.setEnabled(true); hex_t.setEnabled(true); bin_t.setText(""); dec_t.setText(""); hex_t.setText(""); bin_t.setBackground(Color.WHITE); dec_t.setBackground(Color.WHITE); hex_t.setBackground(Color.WHITE); } /** * Sets a new binary conversion problem */ private void newBinaryProblem(int n) { bin_t.setText(Integer.toBinaryString(n)); bin_t.setEnabled(false); } /** * Sets a new decimal conversion problem */ private void newDecimalProblem(int n) { dec_t.setText(Integer.toString(n)); dec_t.setEnabled(false); } /** * Sets a new hexadecimal conversion problem */ private void newHexProblem(int n) { hex_t.setText(Integer.toHexString(n).toUpperCase()); hex_t.setEnabled(false); } /** * Checks the answers * * @return true if the answers were correct */ private boolean checkAnswers() { boolean correct = false; if (currentProblem.equals("bin")) { // Check the decimal and hexadecimal answers String hexAnswer = hex_t.getText().trim().toLowerCase(); String decAnswer = dec_t.getText().trim(); boolean hex_c = (hexAnswer.equals(Integer.toHexString(currentAnswer))); boolean dec_c = (decAnswer.equals(Integer.toString(currentAnswer))); setTextFieldColour(hex_t, hex_c); setTextFieldColour(dec_t, dec_c); correct = (hex_c && dec_c); } else if (currentProblem.equals("dec")) { // Check the binary and hexadecimal answers String hexAnswer = hex_t.getText().trim().toLowerCase(); String binAnswer = bin_t.getText().trim(); boolean hex_c = (hexAnswer.equals(Integer.toHexString(currentAnswer))); boolean bin_c = (binAnswer.equals(Integer.toBinaryString(currentAnswer))); setTextFieldColour(hex_t, hex_c); setTextFieldColour(bin_t, bin_c); correct = (hex_c && bin_c); } else if (currentProblem.equals("hex")) { // Check the binary and decimal answers String decAnswer = dec_t.getText().trim(); String binAnswer = bin_t.getText().trim(); boolean dec_c = (decAnswer.equals(Integer.toString(currentAnswer))); boolean bin_c = (binAnswer.equals(Integer.toBinaryString(currentAnswer))); setTextFieldColour(dec_t, dec_c); setTextFieldColour(bin_t, bin_c); correct = (dec_c && bin_c); } return correct; } /** * Sets the background colour of the TextField * * @param the TextField to set the background colour of * @param the state to set - true (correct) or false */ private void setTextFieldColour(TextField tf, boolean state) { if (state) { tf.setBackground(Color.GREEN); } else { tf.setBackground(Color.RED); } } }