Develop a Java applet that will help an elementary school student learn multipli
ID: 3672071 • Letter: D
Question
Develop a Java applet that will help an elementary school student learn multiplication. Use the Math.random method or a Random object to produce two positive one-digit integers. The program should then display a question, such as:
How much is 6 times 7?
This question can be posted anywhere on the applet window you want. You can easily have it go to the status line at the bottom via the showStatus method.
The student they types the answer into a JTextField. Next the program checks the student's answer. If the answer is correct, draw the string "Very good!" on the applet and ask another multiplication question. If the answer is wrong, draw the string "No. Please try again." on the applet and let the student try the same question repeatedly until the student finally gets it right. A separate method should be used to generate each new question. This method should be called once when the applet begins execution and each time the user answers the question correctly. All drawing on the applet should be performed by the paint method (usually indirectly through the repaint method).
Explanation / Answer
package applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NewJApplet extends JApplet implements ActionListener{
JTextField question, input;
JLabel prompt;
int answer, guess;
String questionString;
/**
* @param args the command line arguments
*/
public void init(){
// set guess to a flag value indicating no user input 17
guess = -999;
// create text fields and a label 20
question = new JTextField( 20 );
question.setEditable( false );
prompt = new JLabel( "Enter your answer: " );
input = new JTextField( 4 );
input.addActionListener( this );
// add components to applet 29
Container container = getContentPane();
container.setLayout( new FlowLayout() );
container.add( question );
container.add( prompt );
container.add( input );
// generate a question 36
createQuestion();
}
public void paint( Graphics g ){
super.paint( g );
// determine whether response is correct 44
// if guess isn't flag value 45
if (guess != -999){
if (guess != answer)
g.drawString( "No. Please try again.", 20, 70 );
else {
g.drawString( "Very Good!", 20, 70 );
createQuestion();
}
guess = -999;
}
}
// verify the entered response
public void actionPerformed( ActionEvent e ){
guess = Integer.parseInt( input.getText() );
// clear the text field
input.setText( "" );
// display the correct response
repaint();
}
// create a new question and a corresponding answer
public void createQuestion(){// get two random numbers between 0 and 9 73
int digit1 = ( int ) ( Math.random() * 10 );
int digit2 = ( int ) ( Math.random() * 10 );
answer = digit1 * digit2;
questionString = "How much is " + digit1 + " times " + digit2 + " ?";
// add to applet 81
question.setText( questionString );
}
public static void main( String[] args ) {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
NewJApplet myApplet = new NewJApplet();
myApplet.init();
myApplet.setVisible( true );
}
} );
}
} // end class}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.