Multiplication Java Applet

Multiplication Java Applet

Page 1 of 1

5 Replies - 8570 Views - Last Post: 28 February 2010 - 07:52 PM Rate Topic: -----

#1 Guest_Craig*


Reputation:

Multiplication Java Applet

Posted 28 February 2010 - 04:51 PM

I need to 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.

This is what I have can someone help me. I am not sure on the whole appet portion.



import java.util.*;

public class Multiply
{
Random randomNumbers = new Random();

int answer; // the correct answer

// ask the user to answer multiplication problems
public void quiz()
{
Scanner input = new Scanner( System.in );
int guess; // the user's guess

createQuestion(); // display the first question

System.out.println( "Enter your answer (-1 to exit):" );
guess = input.nextInt();

while ( guess != -1 )
{
checkResponse( guess );

System.out.println( "Enter your answer (-1 to exit):" );
guess = input.nextInt();
} // end while
} // end method

// prints a new question and stores the corresponding answer
public void createQuestion()
{
// get two random numbers between 0 and 9
int digit1 = randomNumbers.nextInt( 10 );
int digit2 = randomNumbers.nextInt( 10 );

answer = digit1 * digit2;
System.out.printf( "How much is %d times %d?\n", digit1, digit2 );
} // end method createQuestion

// checks if the user answered correctly
public void checkResponse( int guess )
{
if ( guess != answer )
System.out.println( "No. Please try again." );
else
{
System.out.println( "Very Good!" );
createQuestion();
} // end else
} // end method checkResponse
} // end class Multiply

Is This A Good Question/Topic? 0

Replies To: Multiplication Java Applet

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Multiplication Java Applet

Posted 28 February 2010 - 05:48 PM

Please :code:

If you want to write an Applet your class has to extends JApplet or Applet
An Applet sould not do console I/O so your Scanner is useless you should use a JTextField to read user responses
The same way you cannot System.out.println()
Was This Post Helpful? 1
  • +
  • -

#3 Guest_Craig*


Reputation:

Re: Multiplication Java Applet

Posted 28 February 2010 - 07:32 PM

Csn you show me what you mean
Was This Post Helpful? 0

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,666
  • Joined: 27-December 08

Re: Multiplication Java Applet

Posted 28 February 2010 - 07:35 PM

Extending JApplet:
class MyClass extends JApplet{
  //now MyClass is a JApplet
}



As for the console I/O (Scanner and System.out.println), it is useless b/c it won't output to the browser or Applet container, but to a console instead. And when a user is running the program, they will not see the System.out.println() prompts. It is better to use GUI components from the javax.swing and java.awt packages instead, like JTextField, JTextArea, JButton, JRadioButton, JCheckBox, etc.
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: Multiplication Java Applet

Posted 28 February 2010 - 07:37 PM

public class MyApplet extends JApplet {
    public void init(){}
    public void start(){}
    public void stop(){}
    public void destroy(){}
}



Thos are the primary 4 components of an an applet. An applet is meant to be run in a webpage, so standard System and Scanner methods are pretty much useless. You need to look into GUIs to do most things with an applet.
http://java.sun.com/...torial/uiswing/
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Multiplication Java Applet

Posted 28 February 2010 - 07:52 PM

View PostCraig, on 28 February 2010 - 08:32 PM, said:

Csn you show me what you mean

Something like that
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import java.util.*;

public class Multiply extends JApplet implements ActionListener {

	JLabel number1lbl, number2lbl;
	int number1, number2;
	JTextField jtf;
	Random ran;
	JButton btn;
	
	public void init() {
		ran = new Random();
		JPanel p = new JPanel(new GridLayout(4,2));
		p.add(new JLabel("Number 1:"));
		number1 = ran.nextInt(10000);
		number1lbl = new JLabel("" + number1);
		p.add(number1lbl);
		p.add(new JLabel("Number 2:"));
		number2 = ran.nextInt(10000);
		number2lbl = new JLabel("" + number2);
		p.add(number2lbl);
		p.add(new JLabel("Answer:"));
		jtf = new JTextField();
		p.add(jtf);
		p.add(new JLabel("Click to validate answer"));
		btn = new JButton("Validate");
		p.add(btn);
		btn.addActionListener(this);
		add(p);
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// check here that the answer in jtf equals number1 * number2
		// ... then reset
		number1 = ran.nextInt(10000);
		number2 = ran.nextInt(10000);
		number1lbl.setText("" + number1);
		number2lbl.setText("" + number2);
		jtf.setText("");
		
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1