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

Write a java program that simulates the Magic 8 Ball game. Upon running the prog

ID: 3825196 • Letter: W

Question

Write a java program that simulates the Magic 8 Ball game. Upon running the program, generate a random number such that one of the following 8 responses is output:

It is certain
It is decidedly so
Most likely
Signs point to yes
Reply hazy, try again
Ask again later
Don't count on it
My sources say no

There should be an equal chance for any one of the eight responses to come up. The program should ask the user if he or she would like to repeat the program and loop if the user opts to repeat. Random number generation is discussed more extensively in Chapter 6 of the textbook, but for this program you can use the following line of code to generate a random number between 1 and 8:

Random rng = new Random();
int answer = rng.nextInt(8) + 1;

Below is a sample transaction:

What question would you like to ask the Magic 8 ball?
Will it rain today? [user input]
The answer is: Don't count on it

Would you like to ask another question (type Y or N)? Y

What question would you like to ask the Magic 8 ball?
Am I doing well in life? [user input]
The answer is: Signs point to yes

Would you like to ask another question (type Y or N)? N

Thank you for playing the Magic 8 Ball.

Explanation / Answer

Ans: The below is the java program of Magic 8 ball as required.

import java.util.Random; // Random

// Magic 8 ball class

public class Magic8Ball   

{

public static void main( String[] args )

{

// random

Random rng = new Random();

// coice number as int

int choice = 1 + rng.nextInt(20);

// response

String response = "";

// if choice 1

if ( choice == 1 )

response = "It is certain";

// if choice 2

else if ( choice == 2 )

response = "It is decidedly so";

// if choice 3

else if ( choice == 3 )

response = "Most likely";

// if choice 4

else if ( choice == 4 )

response = "Signs point to yes";

// if choice 5

else if ( choice == 5 )

response = "Reply hazy, try again";

// if choice 6

else if ( choice == 6 )

response = "Ask again later";

// if choice 7

else if ( choice == 7 )

response = "Don't count on it";

// if choice 8

else if ( choice == 8 )

response = "My sources say no";

// question

System.out.println("What question do you want to ask Magic 8 ball");

// response

System.out.println( "MAGIC 8-BALL SAYS: " + response );

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote