d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Assigment > Paying Fg
123Next
Add Reply New Topic New Poll
Member
Posts: 19,234
Joined: Aug 13 2009
Gold: 33,940.00
Apr 3 2014 12:30am
I need to 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.

In method init() create a JLabel and a JTextField. Add them into the container, and add this as the action listener to the JTextField.
You need to define a method generateQuestion that will generate two random numbers from 1 to 9, and put a question into the JLabel.
It returns a correct answer. In method actionPerformed you need to check the user answer with the correct answer.
Then call another method displayMessage that displays the message of correctness by calling showStatus.

I only have 100 but I hope someone can help :D

Heres how it should look:


This post was edited by Modification on Apr 3 2014 12:31am
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Apr 3 2014 01:05am
sounds pretty easy. What is troubling you about it?

Make sure to post all of your relevant source code.

Quote
I need to write a Java Applet


Quote
I hope someone can help


(outlined those just in case you were looking for someone to solve it completely)

This post was edited by Eep on Apr 3 2014 01:08am
Member
Posts: 19,234
Joined: Aug 13 2009
Gold: 33,940.00
Apr 3 2014 10:02am
Quote (Eep @ Apr 3 2014 12:05am)
sounds pretty easy. What is troubling you about it?

Make sure to post all of your relevant source code.





(outlined those just in case you were looking for someone to solve it completely)


Yes I am just looking for the complete code.
Thank you.
Member
Posts: 5,988
Joined: May 6 2006
Gold: 30.00
Apr 3 2014 12:12pm
If you're looking for someone to do your homework, they might be so inclined to find your university and report you for cheating and paying people to do your homework :o

This post was edited by oOn on Apr 3 2014 12:21pm
Member
Posts: 19,234
Joined: Aug 13 2009
Gold: 33,940.00
Apr 3 2014 09:39pm
Quote (oOn @ Apr 3 2014 11:12am)
If you're looking for someone to do your homework, they might be so inclined to find your university and report you for cheating and paying people to do your homework  :o


well I do have the code but it isn't working so I am hoping I could have compared with someone else's

Code
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;

import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Assign6 extends JApplet implements ActionListener
{

// graphical user interface components
JLabel questionLabel
JTextField questionField

// constant variable
int number1;
int number2;
boolean product = true;
final int CORRECT = 0, INCORRECT = 1;
private int correctAnswer;
private int userAnswer;

public void init()
{
// obtain content pane and change its layout to FlowLayout
Container container = getContentPane();
container.setLayout( new FlowLayout() );

// create label and text field for question
questionLabel = new JLabel( "How much is %d times %d?" );
container.add( questionLabel );
questionField = new JTextField( 10 );
questionField.setEditable( true );
container.add( questionField );

} // end method init

public void generateQuestion()
{
// pick random numbers
int number1 = 1 + randomNumber.nextInt(9);
int number2 = 1 + randomNumber.nextInt(9);
correctAnswer = number1 * number2;

// display results in textfields
questionField.setText( Integer.toString( number1 ) );
questionField.setText( Integer.toString( number2 ) );

} // end method generateQuestion

public void actionPerformed ( ActionEvent actionEvent )
{
userAnswer = Integer.parseInt(answerField.getText());

// first roll of dice
if ( product )
{

switch ( product )
{

// user inputs correct product
case 1
product = number1 * number2;
gameStatus = CORRECT;
questionField.setText( "" ); // clear point field
break;

// user inputs incorrect product
case 2:
product =! number1 * number2;
gameStatus = INCORRECT;
questionField.setText( "" ); // clear point field
break;

} // end switch

} // end method actionPerformed

public void displayMessage()
{
if( userAnswer == correctAnswer )
{
showStatus( "Very good!" );
generateQuestion();
} // end if
else
{
showStatus( "No. Please try again." );
} // end else


} // end method displayMessage

} // end class Assign6


This post was edited by Modification on Apr 3 2014 09:39pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 3 2014 09:48pm
why dont you describe what your problem is?
Member
Posts: 19,234
Joined: Aug 13 2009
Gold: 33,940.00
Apr 3 2014 10:04pm
updated code:
Code
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;

import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Assign6 extends JApplet implements ActionListener
{

// graphical user interface components
JLabel questionLabel;
JTextField questionField;

// constant variable
int number1;
int number2;
boolean product = true;
final int CORRECT = 0, INCORRECT = 1;
int productStatus = CORRECT;
private int correctAnswer;
private int userAnswer;

public void init()
{
// obtain content pane and change its layout to FlowLayout
Container container = getContentPane();
container.setLayout( new FlowLayout() );

// create label and text field for question
questionLabel = new JLabel( "How much is " + number1 + " times" + number2 + "?" );
container.add( questionLabel );
questionField = new JTextField( 10 );
questionField.setEditable( true );
container.add( questionField );

} // end method init

public void generateQuestion()
{
// pick random numbers
int number1 = 1 + randomNumber.nextInt(9);
int number2 = 1 + randomNumber.nextInt(9);
correctAnswer = number1 * number2;

// display results in textfields
questionField.setText( Integer.toString( number1 ) );
questionField.setText( Integer.toString( number2 ) );

} // end method generateQuestion

public void actionPerformed ( ActionEvent actionEvent )
{
userAnswer = Integer.parseInt(questionField.getText());

// first roll of dice
if ( product )
{

switch ( userAnswer )
{

// user inputs correct product
case 1:
userAnswer = correctAnswer;
productStatus = CORRECT;
questionField.setText( "" ); // clear point field
break;

// user inputs incorrect product
case 2:
userAnswer =! correctAnswer;
productStatus = INCORRECT;
questionField.setText( "" ); // clear point field
break;

} // end switch

}

} // end method actionPerformed

public void displayMessage()
{
if( userAnswer == correctAnswer )
{
showStatus( "Very good!" );
generateQuestion();
} // end if
else
{
showStatus( "No. Please try again." );
} // end else


} // end method displayMessage

} // end class Assign6


1) under "//user inputs incorrect product"
for the line: userAnswer =! correctAnswer;

it says: "The operator ! is undefined for the argument type(s) int

2) the "randomNumber" under //pick random number under public void generateQuestion()
is underlined saying that it cannot be resolved.


if i can fix these errors I can at least run the project to see what I can improve on...

thanks in advance.

This post was edited by Modification on Apr 3 2014 10:26pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Apr 3 2014 10:30pm
Quote
1) under "//user inputs incorrect product"
for the line: userAnswer =! correctAnswer;

it says: "The operator ! is undefined for the argument type(s) int

2) the "randomNumber" under //pick random number under public void generateQuestion()
is underlined saying that it cannot be resolved.

3) When I run the program, the window shows "How much is %d times %d?" instead of actually inputting the numbers.

if i can fix these errors I can at least run the project to see what I can improve on...

thanks in advance.


off the top of my head:

1: the correct syntax for that expression should be
Code
userAnswer != correctAnswer


as far as I can tell, the way the language is interpreting your expression is: userAnswer = ( ! correctAnswer), aka using the unary operator ! (logical negation)

2: As far as I can see, you never initialized/declared an object from the Random class. Maybe do
Code
Random randomNumber = new Random();


3: I am a bit rusty on java, but wouldn't you do something like "How much is" + someVar + " times " + someOtherVar ?

The way you do it currently, is similar to doing like a printf, but you aren't passing it any variables to grab values from.

I am sure our resident java experts can be more thorough

This post was edited by Eep on Apr 3 2014 10:34pm
Member
Posts: 19,234
Joined: Aug 13 2009
Gold: 33,940.00
Apr 3 2014 10:35pm
Quote (Eep @ Apr 3 2014 09:30pm)
off the top of my head:

1: the correct syntax for that expression should be userAnswer != correctAnswer

as far as I can tell, the way the language is interpreting your expression is: userAnswer = ( ! correctAnswer), aka using the unary operator ! (logical negation)

2: As far as I can see, you never initialized/declared an object from the Random class.

3: I am a bit rusty on java, but wouldn't you do something like "How much is" + someVar + " times " + someOtherVar ?

I am sure our resident java experts can be more thorough


#3 I fixed.

#1 I tried the != but it says that it's an Invalid AssignmentOperator. I also tried using ">" (just to see if it worked) but that doesnt work as well. I can't do anything other than a simple "=".

#2 I put "private Random randomNumber;" in the class. I don't know if that was the correct solution though.

new problem:

I ran the program and:

1) the numbers are not random. it says 0 times 0 every time. could be because of problem #2

2) I cannot press enter when i input my number. I am missing this coding somewhere...


edit: on your edit
i tried "Random randomNumber = new Random();"
but still 0 times 0 every time.

This post was edited by Modification on Apr 3 2014 10:37pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Apr 3 2014 10:39pm
Quote (Modification @ Apr 3 2014 11:35pm)
#3 I fixed.

#1 I tried the != but it says that it's an Invalid AssignmentOperator. I also tried using ">" (just to see if it worked) but that doesnt work as well. I can't do anything other than a simple "=".

#2 I put "private Random randomNumber;" in the class. I don't know if that was the correct solution though.

new problem:

I ran the program and:

1) the numbers are not random. it says 0 times 0 every time. could be because of problem #2

2) I cannot press enter when i input my number. I am missing this coding somewhere...


Well, at a glance it looks like your switch statement is not doing what you are trying to accomplish,even remotely.

Maybe start by fixing that.

Code
case correctAnswer:
(do stuff for correct answer)
default:
(do stuff for wrong answer)


(or however you want to implement default)

Conversely, this will also fix problem 1 (which, after the expression was fixed, was still causing an error because you are essentially evaluating an expression but doing nothing with the result).

you need to initialize the Random object with 'new'. Use the syntax I gave you prev. post.

This post was edited by Eep on Apr 3 2014 10:45pm
Go Back To Programming & Development Topic List
123Next
Add Reply New Topic New Poll