Develop a Java applet that will help an elementary school student learn multipli
ID: 3671633 • 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).
Three scenarios are required! You must run your application for three different successive multiplication results. On the first problem, make two deliberate mistakes before providing the correct result. Then on the second problem, make one deliberate mistake before providing the correct result. Finally on the third problem provide the correct result on the first try. Note that zero is not a positive number.
Explanation / Answer
Maths.java
//
//
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Maths extends JApplet implements ActionListener
{
JTextField question, input, response;
JLabel prompt;
int answer, guess;
String questionString;
public void init()
{
// set guess to validate value and existence of user input
guess = -999;
// create test fields and a label
question = new JTextField( 20 );
question.setEditable( false );
prompt = new JLabel( "Input your answer: ");
input = new JTextField( 4 );
input.addActionListener( this );
// add components to applet
Container container = getContentPane();
container.setLayout( new FlowLayout() );
container.add( question );
container.add( prompt );
container.add( input );
// generate a question
generateQuestion();
}
// show if user input is correct or not
public void paint( Graphics g )
{
super.paint( g );
// determine whether user input is correct
// if value is not valid, indicate as such
if ( guess != -999 )
{
if ( guess != answer )
g.drawString( "No. Please try again.", 20, 70 );
else
{
g.drawString( "Very good!" , 20, 70 );
generateQuestion();
}
guess = -999;
}
}
// verify the user input
public void actionPerformed( ActionEvent e )
{
guess = Integer.parseInt( input.getText() );
// clear the text field
input.setText( "" );
// display the correct answer
repaint();
}
// generate new question and correct answer
public void generateQuestion()
{
// generate new numbers between 1 and 9
int number1 = ( int ) ( Math.random() * 10 );
int number2 = ( int ) ( Math.random() * 10 );
answer = number1 * number2;
questionString = "How much is " + number1 + " times " +
number2 + "?";
// add to applet
question.setText( questionString );
}
}// end class Maths
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.