I have the following problem:
Write a Java Applet that will help an elementary school student learn multiplication. Use either Random class or Math.random() method to produce two positive one-digit integers. The applet should prompt the user with a question. The student then enters an answer. If the answer is correct, display the message "Very good!" and ask another question. If the answer is wrong, display the message "No. Please try again." and let the student try the same question repeatedly until the student finally gets it right.

This is what I have so far but when I input an answer it does not tell me if its correct or wrong. Any help would be greatly appreciated.

import java.awt.*;
import java.awt.Graphics;
import java.lang.Object;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class NewJApplet extends JApplet implements ActionListener
{
public Graphics brush;
Random rand = new Random();
int number1 = rand.nextInt(10);
int number2 = rand.nextInt(10);
JLabel question = new JLabel("What is " + number1 + " times " + number2 + "?");
JTextField answer = new JTextField(3);
JButton checkAnswer = new JButton("Check Answer");
Font font1 = new Font("Teen", Font.BOLD, 30);
Font font2 = new Font("Teen", Font.ITALIC, 36);
String right = "Very good!!!";
String wrong = "No.  Please try again.";
Container con = getContentPane();

public void init()
{
setLayout(new FlowLayout());
con.setBackground(Color.BLUE);
question.setLocation(20, 20);
question.setFont(font1);
con.add(question);
answer.setLocation(20, 40);
con.add(answer);
checkAnswer.setLocation(20, 60);
con.add(checkAnswer);

checkAnswer.addActionListener(new ActionListener()
{
public void paint()
{
brush.setFont(font2);
}
public void actionPerformed(ActionEvent e)
{
int ans = Integer.parseInt(answer.getText());

if(ans == number1 * number2)
{
answer.setText("");
Random rand = new Random();
int number1 = rand.nextInt(9) + 1;
int number2 = rand.nextInt(9) + 1;
brush.drawString(right, 20, 80);
repaint();
validate();
}

else
{
answer.setText("");
brush.drawString(wrong, 20, 80);
repaint();
validate();
}
}
}
);
}

@Override
public void actionPerformed(ActionEvent e)
{
answer.setText("");
Random rand = new Random();
int number1 = rand.nextInt(10);
int number2 = rand.nextInt(10);
}
}

Recommended Answers

All 2 Replies

You seem to be drawing strings to a Graphics called "brush" (an odd name for a Graphics, but nevermind), but I can't see where that variable is initialised. You also have a "paint" method in your ActionListener that doesn't seems to be called from anywhere.
Overall it looks like you have got confused about the way that the screen gets painted in an Applet - just review any of the decent tutorials on the web.

I am pretty new to this could someone point me in the right direction please

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.