Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import javax.swing.JOptionPane; // Needed for the JOptionPane class import java.util.Random; // Needed for the Random class /** @@@@ ADD YOUR NAME @@@@@ This program demonstrates

image text in transcribed

image text in transcribed

import javax.swing.JOptionPane; // Needed for the JOptionPane class import java.util.Random; // Needed for the Random class

/** @@@@ ADD YOUR NAME @@@@@ This program demonstrates the Random class. */

public class MathT_utor { public static void main(String[] args) { int number1; // A number int number2; // Another number int sum; // The sum of the numbers int userAnswer; // The user's answer String inputString; // Keyboard input // Create a Random class object. Random randomNumbers = new Random(); // Get two random numbers. number1 = randomNumbers.nextInt(100); number2 = randomNumbers.nextInt(100);

// Calculate the answer. sum = number1 + number2; // Display an addition problem and get the user's answer inputString = JOptionPane.showInputDialog("Enter the answer to the following problem " + number1 + " + " + number2 + " = ?"); // Convert the user's answer to a number userAnswer = Integer.parseInt(inputString); // Display the user's results. if (userAnswer == sum) { JOptionPane.showMessageDialog(null, number1 + " + " + number2 + " = " + userAnswer + " Correct!"); } else { JOptionPane.showMessageDialog(null, number1 + " + " + number2 + " = " + userAnswer + " Sorry, wrong answer The correct answer is " + sum); } } //end of main() } // end of MathT_utor

Pls check instructions carefully. Thank you. Ignore the underscore for MathT_utor

Download the MathTutoriava E file. You must use this version of the file. A similar version appears in our textbook on p.250-251. Before making any changes, first compile and run the program. Add a loop to the program so that the user can determine if they want to try another math problem. After the user is informed whether they answered the problem correctly or not, ask the user if they want to try again. Based on their answer, either repeat the code or quit. Also, add a farewell message at the end (after they choose to quit.) Give the user the following prompt: Do you want to try another problem? Enter 1 for Yes or 2 for No. Read the value the user enters as an integer. Other variable types will cause a potential problem. Hints: . You will be inserting code, but you should not alter any of the existing lines of code. The portion of the program that should loop occurs from lines 22-47. Code Listing 4-6 shows an example of the user controlling whether or not to repeat but please note: in your program the user MUST enter a number (1 or 2), NOT a character (lines 48-52 in the Code Listing) when prompted whether or not to repeat. Submit your java file in our course shell. A working program might look like this

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions