Question

Write a GUI-based Java application that implements the "Math Game" shown below. The program asks the...

Write a GUI-based Java application that implements the "Math Game" shown below. The program asks the user to enter the answer of multiplying two one-digit random integers.  If the user enters a correct answer, a message will be displayed at the bottom of the frame, and the text field will be cleared.  If the user enters a wrong answer, a message will be displayed at the bottom of the frame asking for another answer.  If the user clicks on Next, a new question will be displayed.  If the user clicks on Quit, the user score will be displayed.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The required code is given below.

Quiz.java

import javax.swing.*;
import java.awt.Window;
import java.awt.event.*;
import java.io.*;
import java.util.Random;
public class Quiz implements ActionListener
{
JFrame frame;
JButton NextButton, QuitButton;
JTextField AnswerTextField;
JLabel QuestionLabel , RemarkLabel;
Random random;
int correctAnswer,totalCorrectAnswer;

public Quiz()
{
//initialize with 0
totalCorrectAnswer = 0;
//object initializes
random = new Random();
  
//getting fram
frame = new JFrame();
frame.setSize(400,500);
frame.setLayout(null);

//making button
NextButton = new JButton("Next");
NextButton.setSize(120,30);
NextButton.setLocation(90, 40);
NextButton.addActionListener(this);

//making button
QuitButton = new JButton("Quit");
QuitButton.setSize(120, 30);
QuitButton.setLocation(190, 40);
QuitButton.addActionListener(this);

//making label
QuestionLabel = new JLabel("Question comes here");
QuestionLabel.setSize(350, 30);
QuestionLabel.setLocation(150,100);

//making textfield
AnswerTextField = new JTextField();
AnswerTextField.setSize(150, 50);
AnswerTextField.setLocation(120, 180);
AnswerTextField.addActionListener(this);

//making label
RemarkLabel = new JLabel();
RemarkLabel.setSize(380,50);
RemarkLabel.setLocation(10, 300);

//adding to frames
frame.add(QuitButton);
frame.add(NextButton);
frame.add(RemarkLabel);
frame.add(QuestionLabel);
frame.add(AnswerTextField);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
setQuestion();
}
  
private void setQuestion()
{
int randomNumber1 = random.nextInt(10) + 1;
int randomNumber2 = random.nextInt(10) + 1;
correctAnswer = randomNumber1 * randomNumber2;
QuestionLabel.setText("How much is " + randomNumber1 + " x " + randomNumber2 + "?" );
NextButton.setEnabled(false);
AnswerTextField.setEditable(true);
}

public void actionPerformed(ActionEvent event)
{
String str = event.getActionCommand();
if(str.equals("Quit")){
NextButton.setEnabled(false);
QuitButton.setEnabled(false);
QuestionLabel.setText("Your Score is: " + totalCorrectAnswer);
}
else
if(str.equals("Next"))
{
RemarkLabel.setText("");
setQuestion();
}
else{
System.out.println("Enter is pressed");
try
{
int answer = Integer.parseInt(AnswerTextField.getText());
if(correctAnswer == answer)
{
AnswerTextField.setText("");
RemarkLabel.setText("<html>Correct! Click on Next Button<br>Or Quit to stop the Game.</html>");

totalCorrectAnswer++;
NextButton.setEnabled(true);
AnswerTextField.setEditable(false);
}
else
{
RemarkLabel.setText("Wrong! Try it Again");
}
}
catch (Exception ex)
{
RemarkLabel.setText("Enter a valid number");
}
}

}
//Driver Code
public static void main(String[] args) {
Quiz quiz = new Quiz();
}

}
Screenshot

Add a comment
Know the answer?
Add Answer to:
Write a GUI-based Java application that implements the "Math Game" shown below. The program asks the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please, build calculator to exact image as below using Python. Write a GUI that implements the...

    Please, build calculator to exact image as below using Python. Write a GUI that implements the calculator shown in the following image: Calculator х Plus Equals: Add Clear Quit - User enters two integers into the text fields. - When Add button is pressed, the sum of the values in the text fields are shown after the Equals: as a label. - The Clear button clears the values in the text fields. The cleared values can be blank or zero....

  • Write a program which implements a dice game. Name your program file 'YourUsernameA105.py, e.g. afer023A1Q5.py. The...

    Write a program which implements a dice game. Name your program file 'YourUsernameA105.py, e.g. afer023A1Q5.py. The aim of the game is to reach a score as close as possible to 100 (but not over 100) in three rounds. Each round consists of throwing five random dice, the user then chooses two of the dice values where the two dice values chosen form a two digit score which is added to the user's current total, e.g., if the user first chooses...

  • Write a java application that is including a three-question multiple choice quiz about Java programming language....

    Write a java application that is including a three-question multiple choice quiz about Java programming language. Each question must have four possible answers (numbered 1 to 4). Also, ask user to type 0 to exit the test. [Assume that a user enters only integers 1,2,3,4, or 0]. Firstly, display a message that this quiz includes three questions about the Java programming language and display that each question has four possible answers. If the answer is correct for the given question,...

  • Write a java program that demonstrates recursion. This program can be in a java GUI frame...

    Write a java program that demonstrates recursion. This program can be in a java GUI frame or as a console application. On this one it is up to you. (It would probably work better as a console program for this particular program.) The input for this program is a number of boxes. Ask the user for this with a textbox or a prompt on the command line. Demonstrate recursion with a function called LoadTruck() that takes a number of boxes...

  • Using Java IDE: Write an application that asks elementary students a set of 10 math problems...

    Using Java IDE: Write an application that asks elementary students a set of 10 math problems ● First ask the user for a level and a problem type. ● You need to validate the level and problem type and loop until the user enters a correct one. ● There should be 3 levels. Level 1 operands would have values in the range of 0-9, level 2 operands would have values in the range of 0-99, and level 3 operands would...

  • QUESTION 3 [49 MARKS 3.1) Write the Java statements for a class called Calculator to produce...

    QUESTION 3 [49 MARKS 3.1) Write the Java statements for a class called Calculator to produce the GUI below. Use 3 panels to arrange the components. You must use an array when defining the numbered buttons (o-9. Do not declare 10 buttons individually. The textfield must be initialized with the text as shown below, and be able to accommodate up to 25 characters. The spacing between the numbered buttons is 10. (28) for caloula Add Equals 3.2 Rewrite any existing...

  • Using Java develop a GUI based simple quiz game. The questions and answers of the game...

    Using Java develop a GUI based simple quiz game. The questions and answers of the game will be stored in a text file (i.e. our database). On the first page the users will be provided with some instructions on how to play the game and next button to go to the next page. On the next page will be a start button to either start the game and an exit button to exit the game. Once the game starts there...

  • Can someone help me with the GUI c# card game please?

    You are required to design a simple GUI game: guess a card. At the beginning, a form shows the back of a card with two radio buttons and one button. Moreover, your program randomly generates an integer between 1 (it means the card is Spade Ace) and 2 (it means the card is Heart Ace). Then the user selects a radio button and then clicks the button“Guess” to guess (see Figure 1). After clicking the button, the form shows the...

  • In Java. Write a GUI contact list application. The program should allow you to input names...

    In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...

  • x= Suppose you are building a program for teaching kids' math. Write a java program the...

    x= Suppose you are building a program for teaching kids' math. Write a java program the does the following: 1. Ask the user if he/she wants to sign-up a. If yes continue to step 2 b. If no Display the message "Thank you, Have a nice Day 2. Ask the user to enter a username. 3. Ask the user to enter a password with at least 8 characters long 4. Write a method with the following header: public static Boolean...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT