Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.Scanner; import java.util.Random; class MathTutor { public stat

ID: 3908404 • Letter: I

Question

import java.util.Scanner;
import java.util.Random;

class MathTutor
{
public static void main(String[] args)
{
int number1;
int number2;
int sum;
int userAnswer;


Scanner keyboard = new Scanner(System.in);


Random randomNumbers = new Random();


number1 = randomNumbers.nextInt(100);
number2 = randomNumbers.nextInt(100);

// Display an addition problem.
System.out.println("What is the answer to the " +
"following problem?");
System.out.print(number1 + " + " +
number2 + " = ? ");


sum = number1 + number2;


userAnswer = keyboard.nextInt();


if (userAnswer == sum)
System.out.println("Correct!");
else
{
System.out.println("Sorry, wrong answer. " +
"The correct answer is " +
sum);
}
}
}

2. Using MathTutor.java on page 250 make the following changes a. Do a random selection on the operation to perform. You can do this by running a generator with only 4 numbers and using a switch to pick your operator. b. Keep count of right and wrong answers and display the running totals C. "Emcompass" program in a loop to continue the game.

Explanation / Answer

MathTutor.java (Changes made are in bold)

import java.util.Scanner;

import java.util.Random;

//MathTutor class begins

class MathTutor

{

       //main() begins

       public static void main(String[] args)

       {

             //Variable declarations

             int number1;

             int number2;

             int result = 0;

             int userAnswer;

             int correctCount = 0;

             int incorrectCount = 0;

             char operator = ' ';

             char playAgain;

            

             //Objects of Scanner and Random class

             Scanner keyboard = new Scanner(System.in);

             Random randomNumbers = new Random();

            

             //Loop runs until user does not want to play anymore

             do

             {

                    //Generate 2 random numbers (operands) from 0 to 99

                    number1 = randomNumbers.nextInt(100);

                    number2 = randomNumbers.nextInt(100);

                   

                    //Generates a random number and it sets the operator

                    switch(randomNumbers.nextInt(4))

                    {

                    //Addition

                    case 0:

                           operator = '+';

                           result = number1 + number2;

                           break;

                          

                    //Subtraction

                    case 1:

                           operator = '-';

                           result = number1 - number2;

                           break;

                          

                    //Multiplication

                    case 2:

                           operator = '*';

                           result = number1 * number2;

                           break;

                          

                    //Division

                    case 3:

                           operator = '/';

                           result = number1 / number2;

                           break;

                    }

                   

                    //Print the problem

                    System.out.println("What is the answer to the following problem?");

                    System.out.print(number1 + " " + operator + " " + number2 + " = ? ");

                    //Also tell the user to enter only the integer for division problems

                    if(operator == '/')

                           System.out.print("(Enter integer part only) ");

                   

                    //Input answer

                    userAnswer = keyboard.nextInt();

                   

                    //If answer entered by user is correct, then print "Correct!" and increase count for correct answers by 1

                    if (userAnswer == result)

                    {

                           System.out.println("Correct!");

                           correctCount++;

                    }

                    //Otherwise print message and increase count for incorrect answers by 1

                    else

                    {

                           System.out.println("Sorry, wrong answer. The correct answer is " + result);

                           incorrectCount++;

                    }

  //Output the count for correct and incorrect answers

                    System.out.println("Correct answer: " + correctCount);

                    System.out.println("Incorrect answer: " + incorrectCount);

                   

                    //Ask user if want to play again

                    System.out.print("Do you want to play again? (Y/N) ");

                    //Input user choice

                    //The following statement is used to input a single character and ignores the characters except 1st character

                    playAgain = keyboard.next().charAt(0);

                    System.out.println();

                   

                    //Run the loop again if user entered 'Y' or 'y'

             }while(playAgain == 'Y' || playAgain == 'y');

            

             //Always remember to close Scanner class object

             keyboard.close();

       }

       //main() ends

}

//MathTutor class ends

Output for a test run is also provided. User input is in bold.

What is the answer to the following problem?

96 * 30 = ? 2880

Correct!

Correct answer: 1

Incorrect answer: 0

Do you want to play again? (Y/N) Y

What is the answer to the following problem?

1 - 28 = ? -27

Correct!

Correct answer: 2

Incorrect answer: 0

Do you want to play again? (Y/N) Y

What is the answer to the following problem?

66 / 91 = ? (Enter integer part only) 0

Correct!

Correct answer: 3

Incorrect answer: 0

Do you want to play again? (Y/N) Y

What is the answer to the following problem?

39 / 8 = ? (Enter integer part only) 5

Sorry, wrong answer. The correct answer is 4

Correct answer: 3

Incorrect answer: 1

Do you want to play again? (Y/N) Y

What is the answer to the following problem?

14 - 0 = ? 14

Correct!

Correct answer: 4

Incorrect answer: 1

Do you want to play again? (Y/N) N

Kindly give a thumbs up, if found useful. Comment for queries. :)