Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For the 5th case im supposed to generate a random problem and I am stuck import java.security.SecureRandom; import java.util.Scanner; public class Project1 { static Scanner

For the 5th case im supposed to generate a random problem and I am stuck

import java.security.SecureRandom;

import java.util.Scanner;

public class Project1 {

static Scanner scanner = new Scanner(System.in);

static SecureRandom random = new SecureRandom();

static int Level;

static int currentLevel;

// levels start at 1 and keep going until the user exits

public static void main(String[] args) {

Level = 1;

currentLevel = Level;

// keeps the game going for different levels

while(true) {

int choice = menu();

// generate 2 random numbers based on the current level

boolean success = generateProblem(currentLevel, choice);

// checks if the current level is cleared

if(success) {

Level++;

currentLevel = Level;

}

else {

System.out.println("Next Student");

}

}

}

// lines that will be displayed on the menu

public static int menu() {

int choice = -1;

do {

System.out.println("1. Addition");

System.out.println("2. Subtraction");

System.out.println("3. Multiplication");

System.out.println("4. Division");

System.out.println("5. Random Mixture");

System.out.println("6. Exit");

System.out.println("Enter choice from[1,2,3,4,5,6]: ");

choice = scanner.nextInt();

if(choice < 1 || choice > 6)

System.out.println("Error. Enter choice from 1-6");

if(choice == 6) {

System.out.println("Game Over");

System.exit(0);

}

}

while(choice < 1 || choice > 5);

return choice;

}

public static boolean generateProblem(int level, int choice) {

//# of questions

double Maximum_Tries = 10;

int questionCount = 1;

//storing the percentage

double percentage = 0;

//counting # of correct answers

int correct = 0;

double useranswer;

boolean success = false;

//4 random messages for correct and incorrect scores

String correctMessages[] = {"Very Good!", "Excellent!", "Nice Work!", "Keep up the good work!"};

String incorrectMessages[] = {"Try again.", "Keep trying.", "Try once more.", "Don't give up."};

// minimum and maximum based on student's level

int minimum = (int)Math.pow(10, level -1);

int maximum = (int)(Math.pow(10, level)-1);

/*generating 2 random numbers based on level

* level goes up every time you get 75% correct

* level 1 starts with random numbers from 0-9

* level 2 starts with random numbers from 10-99

* other levels go up by 1 place for example, for level 3 it would be 100-999

*/

int num1 = random.nextInt(maximum - minimum +1) + minimum;

int num2 = random.nextInt(maximum - minimum +1) + minimum;

int num3 = random.nextInt(maximum - minimum +1) + minimum;

int num4 = random.nextInt(maximum - minimum +1) + minimum;

int num5 = random.nextInt(maximum - minimum +1) + minimum;

System.out.println("Level-" + level);

//while loop to prompt student to answer the maximum of ten questions

while(questionCount <= Maximum_Tries) {

success = false;

/* calls the methods and prompts the user for input

* if correct it creates a new problem with random integers

* if false it will keep asking for the answer from the same problem until correct or you run out of attempts

*/

switch(choice) {

case 1: System.out.print(num1 + "+" + num2 + "=");

useranswer = addition(num1,num2);

useranswer = scanner.nextInt();

if(useranswer == (num1 + num2))

success = true;

break;

case 2: System.out.print(num1 + "-" + num2 + "=");

useranswer = subtraction(num1,num2);

useranswer = scanner.nextInt();

if(useranswer == (num1 - num2))

success = true;

break;

case 3: System.out.print(num1 + "*" + num2 + "=");

useranswer = multiplication(num1,num2);

useranswer = scanner.nextInt();

if(useranswer == (num1 * num2))

success = true;

break;

case 4: System.out.print(num1 + "/" + num2 + "=");

useranswer = division(num1,num2);

useranswer = scanner.nextInt();

if(useranswer == (num1 / num2))

success = true;

break;

case 5: String randomQuestion = new String();

randomQuestion == num1 + num2;

System.out.println(randomQuestion);

useranswer = randomMixture(num1,num2);

double randomAnswer = scanner.nextDouble();

double result = (num1 * num2) - ((num3 + num4) / 1.0);

if(Math.abs(randomAnswer - result) < 0.01)

success = true;

else success = false;

break;

}

// Checking to see if student answered the question correctly and then displaying a positive message if they did

if(success) {

System.out.println(correctMessages[random.nextInt(correctMessages.length)]);

questionCount++;

correct++;

num1 = random.nextInt(maximum - minimum + 1) + minimum;

num2 = random.nextInt(maximum - minimum + 1) + minimum;

}

// the wrong answer brings up a positive message to encourage the user to try again

else {

System.out.println(incorrectMessages[random.nextInt(incorrectMessages.length)]);

questionCount++;

}

}

//Checks if the % is greater than 75

percentage = (correct / Maximum_Tries) * 100.0;

if(percentage >= 75) {

System.out.println("Congratulations, You are ready to go to the next level!");

success = true;

}

else {

System.out.println("Please ask your teacher for extra help");

success = false;

}

return success;

}

private static double randomMixture(int num1, int num2) {

int multiplication = (int) multiplication(num1, num2);

int division = (int) division(multiplication, num2);

int sum = (int) addition(num1, division);

int subtraction = (int) subtraction(sum, num2);

return subtraction;

}

private static double division(int num1, int num2) {

return (double) (num1/num2);

}

private static double multiplication(int num1, int num2) {

return num1*num2;

}

private static double subtraction(int num1, int num2) {

return num1-num2;

}

private static double addition(int num1, int num2) {

return num1+num2;

}

}

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_2

Step: 3

blur-text-image_3

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

Conceptual Database Design An Entity Relationship Approach

Authors: Carol Batini, Stefano Ceri, Shamkant B. Navathe

1st Edition

0805302441, 978-0805302448

More Books

Students also viewed these Databases questions

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago