Develop an application that helps children learn multiplication (Fig. 15.29). Use random-number generation to produce two positive one-digit integers that display in a question, such as “How much is 6 times 7?” The student should type the answer into a JTextField. If the answer is correct, then the application ran- domly displays one of three messages: Very Good!, Excellent! or Great Job! in a JLabel and displays the next question. If the student is wrong, the JLabel displays the message No. Please try again.





a) Copying the template to your working directory. Copy the C:Examples Tutorial15ExercisesMultiplicationTeacher directory to your C:Simply- Java directory.

b) Opening the template file. Open the MultiplicationTeacher.java file in your text editor.

c) Generating the questions. Declare a method named generateQuestion in your application to generate and display each new question in questionJLabel.

d) Adding a call to the generateQuestion method. Add a call to the generate- Question method at the end of the createUserInterface method.

e) Displaying a random message. Add a method named generateOutput that displays a random message congratulating the student if they answer correctly.

f) Determining whether the right answer was entered. Add code to the submit- JButtonActionPerformed method declared in your application. Determine whether the student answered the question correctly and display an appropriate message. If the student answered the question correctly, ca


```
1 // MultiplicationTeacher.java
2 // This game asks the user to enter the product of two
3 // randomly generated numbers in the range 0-9.
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.util.Random;
7 import javax.swing.*;
8
9 public class MultiplicationTeacher extends JFrame
10 {
11 // JLabel and JTextField to question user and get answer
12 private JLabel questionJLabel;
13 private JTextField answerJTextField;
14
15 // JButton to submit answer
16 private JButton submitJButton;
17
18 // JLabel and JTextField to display if user is correct
19 private JLabel resultJLabel;
20 private JTextField responseJTextField;
21
22 // declare instance variables
23 private int correctAnswer;
24
25 // create new Random object
26 private Random generator = new Random();
27
28 // no-argument constructor
29 public MultiplicationTeacher()
30 {
31 createUserInterface();
32 }
33
34 // create and position GUI components; register event handlers
35 private void createUserInterface()
36 {
37 // get content pane for attaching GUI components
38 Container contentPane = getContentPane();
39
40 // enable explicit positioning of GUI components
41 contentPane.setLayout( null );
42
43 // set up questionJLabel
44 questionJLabel = new JLabel();
45 questionJLabel.setBounds( 20, 16, 148, 23 );
46 contentPane.add( questionJLabel );
47
48 // set up answerJTextField
49 answerJTextField = new JTextField();
50 answerJTextField.setBounds( 176, 16, 48, 21 );
51 contentPane.add( answerJTextField );
52
53 // set up submitJButton
54 submitJButton = new JButton();
55 submitJButton.setBounds( 56, 48, 128, 23 );
56 submitJButton.setText( "Submit Answer" );
57 contentPane.add( submitJButton );
58 submitJButton.addActionListener(
59
60 new ActionListener() // anonymous inner class
61 {
62 // event handler called when submitJButton is clicked
63 public void actionPerformed( ActionEvent event )
64 {
65 submitJButtonActionPerformed( event );
66 }
67
68 } // end anonymous inner class
69
70 ); // end call to addActionListener
71
72 // set up resultJLabel
73 resultJLabel = new JLabel();
74 resultJLabel.setBounds( 8, 80, 40, 23 );
75 resultJLabel.setText( "Result:" );
76 contentPane.add( resultJLabel );
77
78 // set up responseJTextField
79 responseJTextField = new JTextField();
80 responseJTextField.setBounds( 56, 80, 152, 23 );
81 responseJTextField.setHorizontalAlignment( JTextField.CENTER );
82 responseJTextField.setEditable( false );
83 contentPane.add( responseJTextField );
84
85 // set properties of application’s window
86 setTitle( "Multiplication Teacher" ); // set title bar string
87 setSize( 247, 143 ); // set window size
88 setVisible( true ); // display window
89
90 generateQuestion(); // ask the first question
91
92 } // end method createUserInterface
93
94 // determine if user’s guess is right
95 private void submitJButtonActionPerformed( ActionEvent event )
96 {
97 // get user data
98 int userAnswer = Integer.parseInt(
99 answerJTextField.getText() );
100
101 // if input == answer display random correct message
102 if ( userAnswer == correctAnswer )
103 {
104 generateOutput(); // generate correct message
105 generateQuestion(); // generate new question
106 }
107 else // display wrong answer
108 {
109 responseJTextField.setText( "No. Please try again." );
110 }
111
112 answerJTextField.setText( "" ); // clear JTextField
113
114 } // end method submitJButtonActionPerformed
115
117 private void generateQuestion()
118 {
119 // create 2 random numbers
120 int first = generator.nextInt( 10 );
121 int second = generator.nextInt( 10 );116 // generate new question
122
123 // display question
124 questionJLabel.setText( "How much is " + first + " times "
125 + second );
126
127 correctAnswer = first * second; // set correct answer
128
129 } // end method generateQuestion
130
131 //
132 private void generateOutput()
133 {
134 // generate random message
135 int number = 1 + generator.nextInt( 3 );
136
137 switch (number)
138 {
139 case 1:
140 responseJTextField.setText( "Very Good!" );
141 break;
142
143 case 2:
144 responseJTextField.setText( "Excellent!" );
145 break;
146
147 case 3:
148 responseJTextField.setText( "Great Job!" );
149 break;
150
151 } // end switch statement
152
153 } // end method generateOutput
154
155 // main
156 public static void main( String[] args )
157 {
158 MultiplicationTeacher application =
159 new MultiplicationTeacher();
160 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
161
162 } // end method main
163
164 } // end class MultiplicationTeacher
```

Computer Science & Information Technology

You might also like to view...

An investment analyst started a free nonprofit educational video service called the ________

Fill in the blank(s) with correct word

Computer Science & Information Technology

Clicking a list number or bullet symbol is an efficient way to select an entire bullet point

Indicate whether the statement is true or false

Computer Science & Information Technology

VoIP sends packets using Real-time Transport Protocol (RTP), which is a ________ protocol

A) Layer 4 B) Layer 2 C) Layer 3 D) Layer 1

Computer Science & Information Technology

Storyboarding is like a wireframe because both provide a visual way to communicate your ideas to others.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology