Question

Integer Math Create an application that uses random integers to test the user’s knowledge of arithmetic....

Integer Math
Create an application that uses random integers to test the user’s knowledge of arithmetic. Let the user choose from addition, subtraction, multiplication, and division. The integers used in the problems should range from 20 to 120. When giving feedback, use color to differentiate between a correct answer response, versus an incorrect answer response. Also check for non-integer input. Preparing division problems requires special consideration because the quotient must be an integer. Therefore, you can use a loop to generate new random values for the second operand until you find one that divides the first operand evenly. Use the Mod operator to verify that the integer division remainder is zero.

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

package GUI;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

// Class IntegerMath definition

public class IntegerMath

{

// Declares component and container

JFrame jf;

JRadioButton addRB, subRB, mulRB, divRB;

ButtonGroup bg;

JLabel expressionL;

JTextField resultT;

JButton checkB, exitB, nextB;

JPanel jp1, jp2, jp3, mainP;

// To store the random numbers generated

int no1, no2;

// Default constructor definition

IntegerMath()

{

// Component and container created

jf = new JFrame("Math Test");

addRB = new JRadioButton("Addition", true);

subRB = new JRadioButton("Subtraction");

mulRB = new JRadioButton("Multiplication");

divRB = new JRadioButton("Division");

bg = new ButtonGroup();

bg.add(addRB);

bg.add(subRB);

bg.add(mulRB);

bg.add(divRB);

expressionL = new JLabel();

resultT = new JTextField(10);

checkB = new JButton("CHECK");

exitB = new JButton("EXIT");

nextB = new JButton("NEXT");

jp1 = new JPanel();

jp2 = new JPanel();

jp3 = new JPanel();

mainP = new JPanel();

// Adds the components to panels

jp1.add(addRB);

jp1.add(subRB);

jp1.add(mulRB);

jp1.add(divRB);

// Sets the layout to 4 row and 1 column

jp1.setLayout(new GridLayout(4, 1));

jp2.add(expressionL);

jp2.add(resultT);

jp3.add(checkB);

jp3.add(nextB);

jp3.add(exitB);

// Adds the panels to main panel

mainP.add(jp1);

mainP.add(jp2);

mainP.add(jp3);

// Sets the layout to 3 row and 1 column

mainP.setLayout(new GridLayout(3, 1));

// Adds the main panel to frame

jf.add(mainP);

// Sets the frame property

jf.setVisible(true);

jf.setLocationRelativeTo(null);

jf.pack();

// Calls the method to generate expression and sets it to expression label

// 1 for addition

expressionL.setText(getExpression(1));

// Registers action listener to check button using anonymous class

checkB.addActionListener(new ActionListener()

{

// Overrides the actionPerformed() method

public void actionPerformed(ActionEvent ae)

{

// Checks if add radio button selected

if(addRB.isSelected())

{

// Checks if addition of two numbers is equals to the

// number entered by the user

if(no1 + no2 == Integer.parseInt(resultT.getText()))

{

JOptionPane.showMessageDialog(null, "Correct Answer");

// Sets the back ground color to yellow

resultT.setBackground(Color.YELLOW);

}// End of if condition

// Otherwise wrong answer

else

{

// Displays the correct answer

JOptionPane.showMessageDialog(null, "Wrong Answer Correct Answer: "

+ (no1 + no2));

// Sets the back ground color to red

resultT.setBackground(Color.RED);

}// End of else

}// End of if condition

// Otherwise checks if subtract radio button selected

else if(subRB.isSelected())

{

// Checks if subtraction of two numbers is equals to the

// number entered by the user

if(no1 - no2 == Integer.parseInt(resultT.getText()))

{

JOptionPane.showMessageDialog(null, "Correct Answer");

// Sets the back ground color to yellow

resultT.setBackground(Color.YELLOW);

}// End of if condition

// Otherwise wrong answer

else

{

// Displays the correct answer

JOptionPane.showMessageDialog(null, "Wrong Answer Correct Answer: "

+ (no1 - no2));

// Sets the back ground color to red

resultT.setBackground(Color.RED);

}// End of else

}// End of if condition

// Otherwise checks if multiplication radio button selected

else if(mulRB.isSelected())

{

// Checks if multiplication of two numbers is equals to the

// number entered by the user

if(no1 * no2 == Integer.parseInt(resultT.getText()))

{

JOptionPane.showMessageDialog(null, "Correct Answer");

// Sets the back ground color to yellow

resultT.setBackground(Color.YELLOW);

}// End of if condition

// Otherwise wrong answer

else

{

// Displays the correct answer

JOptionPane.showMessageDialog(null, "Wrong Answer Correct Answer: "

+ (no1 * no2));

// Sets the back ground color to red

resultT.setBackground(Color.RED);

}// End of else

}// End of if condition

// Otherwise division radio button selected

else

{

// Checks if division of two numbers is equals to the

// number entered by the user

if(no1 / no2 == Integer.parseInt(resultT.getText()))

{

JOptionPane.showMessageDialog(null, "Correct Answer");

// Sets the back ground color to yellow

resultT.setBackground(Color.YELLOW);

}// End of if condition

// Otherwise wrong answer

else

{

// Displays the correct answer

JOptionPane.showMessageDialog(null, "Wrong Answer Correct Answer: "

+ (no1 / no2));

// Sets the back ground color to red

resultT.setBackground(Color.RED);

}// End of else

}// End of else

}// End of method

});// End of anonymous class

// Registers action listener to next button using anonymous class

nextB.addActionListener(new ActionListener()

{

// Overrides the actionPerformed() method

public void actionPerformed(ActionEvent ae)

{

// Sets the back ground color to white

resultT.setBackground(Color.WHITE);

// Reset the text to null

resultT.setText("");

// Checks if add radio button selected

if(addRB.isSelected())

// Calls the method to generate expression and sets it to expression label

// 1 for addition

expressionL.setText(getExpression(1));

// Otherwise checks if subtract radio button selected

else if(subRB.isSelected())

// Calls the method to generate expression and sets it to expression label

// 2 for subtraction

expressionL.setText(getExpression(2));

// Otherwise checks if multiplication radio button selected

else if(mulRB.isSelected())

// Calls the method to generate expression and sets it to expression label

// 3 for multiplication

expressionL.setText(getExpression(3));

// Otherwise division radio button selected

else

// Calls the method to generate expression and sets it to expression label

// 4 for division

expressionL.setText(getExpression(4));

}// End of method

});// End of anonymous class

// Registers action listener to exit button using anonymous class

exitB.addActionListener(new ActionListener()

{

// Overrides the actionPerformed() method

public void actionPerformed(ActionEvent ae)

{

System.exit(0);

}// End of method

});// End of anonymous class

}// End of default constructor

// Method to generate an expression and returns it

String getExpression(int ch)

{

// To store expression

String exp = "";

// To store operator

String opt = "";

// Checks if parameter ch is 1 then set the operator to "+"

if(ch == 1)

opt = "+";

// Otherwise checks if parameter ch is 2 then set the operator to "-"

else if(ch == 2)

opt = "-";

// Otherwise checks if parameter ch is 3 then set the operator to "X"

else if(ch == 3)

opt = "X";

// Otherwise ch is 3 then set the operator to "/"

else

opt = "/";

// Calls the method to generate random numbers

no1 = getRandomNumber();

no2 = getRandomNumber();

// Generates the expression

exp += no1 + " " + opt + " " + no2;

// Returns the expression

return exp;

}// End of method

// Method to generate random number between 20 and 120 and returns it

int getRandomNumber()

{

// Random class object created

Random rand = new Random();

// Generates random number and returns it

return (rand.nextInt(120 - 20) + 20);

}// End of method

// main method definition

public static void main(String ss[])

{

// anonymous object to call default constructor

new IntegerMath();

}// End of main method

}// End of class

Sample Output:

Add a comment
Know the answer?
Add Answer to:
Integer Math Create an application that uses random integers to test the user’s knowledge of arithmetic....
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
  • Integer Math- Console based---uses Windows Form in Visual Basic   Create an application that uses random integers...

    Integer Math- Console based---uses Windows Form in Visual Basic   Create an application that uses random integers to test the user’s knowledge of arithmetic. Let the user choose from addition, subtraction, multiplication, and division. The integers used in the problems should range from 20 to 120. When giving feedback, use color to differentiate between a correct answer response, versus an incorrect answer response. Also check for non-integer input. Preparing division problems requires special consideration because the quotient must be an integer....

  • 1. [10 marks] Modular Arithmetic. The Quotient-Remainder theorem states that given any integer n and a positive integer...

    1. [10 marks] Modular Arithmetic. The Quotient-Remainder theorem states that given any integer n and a positive integer d there exist unique integers q and r such that n = dq + r and 0 r< d. We define the mod function as follows: (, r r>n = qd+r^0<r< d) Vn,d E Z d0 Z n mod d That is, n mod d is the remainder of n after division by d (a) Translate the following statement into predicate logic:...

  • Write an assembler program that asks the user (as shown below) for two integers and a...

    Write an assembler program that asks the user (as shown below) for two integers and a single character representing the arithmetic operations: addition, subtraction, multiplication and integer division (displays both quotient and remainder). Perform the requested operation on the operands and display the result as specified below. Assume SIGNED NUMBERS. The program should not divide by 0! If the user attempts to divide by 0, display the error message: "Cannot divide by zero." If this error occurs, the program should...

  • The Arithmetic Logic Unit The first topic for the project is to create an Arithmetic Logic...

    The Arithmetic Logic Unit The first topic for the project is to create an Arithmetic Logic Unit, using a structured approached with a Virtual Hardware Design Language such as Verilog. Mainly, the program is very close to a simulator for a programming calculator. An ALU typically has the following operations Math Functions: Add, Subtract, Multiply, Divide, Modulus Logic Functions: And, Or, XOR, Not, Nand, Nor, XNOR Error Modes: Divide by Zero, Overflow Support Functions: No Operation, Shift Left, Shift Right,...

  • Write a MIPS math quiz program in MARS. The program should start with a friendly user...

    Write a MIPS math quiz program in MARS. The program should start with a friendly user greeting. From there, it should generate a random arithmetic problem. Your program will need to generate three random things: the first and second operand and the operator to use. Your program should generate random positive integers no greater than 20 for the operands. The possible operators are +, -, * and / (division). The user should be prompted for an answer to the problem....

  • Write a C or C++ program A6p2.c[pp] that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60 random integers between 1 and 39 inclusive and store them in a 5 by 12 two dimensional integer array (e.g.,int a[5][12]

    1.      Write a C or C++ program A6p2.c[pp] that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60 random integers between 1 and 39 inclusive and store them in a 5 by 12 two dimensional integer array (e.g.,int a[5][12];). Use pthread to create n threads to convert all 60 array elements modulo 11 (i.e. take the remainder after division by 11) in place. You should divide this update task among the n threads as evenly as possible. Print the array both before and after...

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

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