/* COSC 304 section 2 LUKE BURGESS luke_burgess@hotmail.com @00205854 Oct 08 2000 */ // Lab task: Chapter 6, Exercise 6.32, page 264 // Exercise32p264.java // Applet generates single digit multiplication problems // and random text responses from a pre-defined list. import javax.swing.*; import java.awt.*; import java.awt.Color; import java.awt.event.*; // Note: Applets have a problem rendering Paint graphics when // Swing components are attached to the UI. Consequently, this // solution does not use paint public class Exercise32p264 extends JApplet implements ActionListener { JTextField input, response, question; JLabel prompt; String questionString, correctResponse, incorrectResponse; int answer; public void init() { input = new JTextField( 4 ); input.addActionListener( this ); prompt = new JLabel( "Enter your answer: " ); response = new JTextField( 20 ); response.setEditable( false ); response.setBackground(Color.decode("#ccccff")); question = new JTextField( 20 ); question.setEditable( false ); question.setBackground(Color.decode("#ccccff")); Container c = getContentPane(); c.setLayout( new FlowLayout() ); c.setBackground(Color.decode("#ccccff")); c.add( question ); c.add( prompt ); c.add( input ); c.add( response ); createQuestion(); } // init () public void start() { question.setText( questionString ); } // Create a new question and corresponding answer public void createQuestion() { int digit1 = getNumber(); int digit2 = getNumber(); answer = digit1 * digit2; questionString = "How much is " + digit1 + " times " + digit2 + " ?"; } // createQuestion() public int getNumber() { return ( ( int ) ( Math.random() * 10 ) ); } public void actionPerformed( ActionEvent e ) { int guess = Integer.parseInt( input.getText() ); input.setText( "" ); switch( getResponseNumber() ) { case 1: correctResponse = "Very good!"; incorrectResponse = "No. Please try again."; break; case 2: correctResponse = "Excellent!"; incorrectResponse = "Wrong. Try once more."; break; case 3: correctResponse = "Nice work!"; incorrectResponse = "Don't give up!"; break; case 4: correctResponse = "Keep up the good work!"; incorrectResponse = "No. Keep trying."; break; case 5: correctResponse = "Correct."; incorrectResponse = "Incorrect."; break; case 6: correctResponse = "Cool !"; incorrectResponse = "Nope!"; break; case 7: correctResponse = "Brilliant!"; incorrectResponse = "Not bad! Try again."; break; case 8: correctResponse = "Superb!"; incorrectResponse = "Unlucky. Have another go."; break; case 9: correctResponse = "Way to go!"; incorrectResponse = "I know you can do this!"; break; case 10: correctResponse = "Keep it up!"; incorrectResponse = "Try harder."; break; case 11: correctResponse = "Good job!"; incorrectResponse = "You can do better than that."; break; case 12: correctResponse = "Well done!"; incorrectResponse = "Come on!"; break; } // switch () if ( guess == answer ) { response.setText( correctResponse ); createQuestion(); } else response.setText( incorrectResponse ); question.setText( questionString ); } // actionPerformed() public int getResponseNumber() { return ( 1 + ( int ) ( Math.random() * 12 ) ); } } // class Exercise32p264