Hi,

I'm having a hard time getting my applet to even build right. Here are the instructions my instructor gave:

"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 then 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)."

It seems that right now, no matter how I try to use the drawString() method or where I put it, I can't get this thing to build without errors. I've been working on this all week and have until 10:00pm tomorrow (Sunday) night to finish and submit this. Can someone PLEASE help me?

Here's what I have at the moment:

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

public class Mult1 extends JApplet implements ActionListener 
{
	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(Graphics brush)
			{
				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

I got it working :)

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);
	}
}

What I did is I declared brush at the start of the program acting like global variable. Hope this helps

I got it working :)

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);
	}
}

What I did is I declared brush at the start of the program acting like global variable. Hope this helps

Thank you. That does allow it to build with no errors but when the answer is right or wrong, it doesn't actually draw the appropriate string. I coded it in a way that I feel like would make it work but after clicking on the check answer button, nothing happens. Any advice?

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.