Develop a Java applet that will help an elementary school student learn multipli
ID: 3768400 • 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Multiply2 extends JFrame
{
JPanel panel;
JLabel status,prompt,response;
JTextField question,input;
String questionString;
int correctAnswer,userInput;
public Multiply2 ()
{
super ("Multiplication Practice for Kids");
ActionEventHandler handler = new ActionEventHandler();
question = new JTextField(20);
question.setEditable(false);
status = new JLabel ("Are you ready to have fun with Multiplication",SwingConstants.CENTER);
prompt = new JLabel("Enter your answer: ");
response = new JLabel ();
input = new JTextField(4);
input.addActionListener(handler);
Container c = getContentPane();
panel = new JPanel();
c.setLayout (new FlowLayout());
getContentPane().setBackground(Color.yellow);
panel.setBackground (Color.BLUE);
c.add(status);
c.add(panel);
panel.add(question);
panel.add(prompt);
panel.add(input);
panel.add(response);
createQuestion();
}
private class ActionEventHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
userInput = Integer.parseInt(input.getText());
input.setText("");
//repaint();
verifyAnswer();
}
}
public void verifyAnswer()
{
//super.paint(g);
if (userInput != correctAnswer)
response.setText("No.Please try again.");
else
{
response.setText("Very Good");
createQuestion();
}
}
public void createQuestion()
{
int digit1 = (int) (Math.random() * 10);
int digit2 = (int) (Math.random() * 10);
correctAnswer = digit1 * digit2;
questionString = "How much is " + digit1 + " times " + digit2 + "?";
question.setText(questionString);
}
public static void main (String args[])
{
Multiply2 window = new Multiply2();
window.addWindowListener
(
new WindowAdapter()
{
public void windowClosing (WindowEvent e)
{
System.exit (0);
}
}
);
window.setSize (500,200);
window.show();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.