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

Hi, so I need help with an assignment I\'m doing called Magic 8 Ball. It require

ID: 3821666 • Letter: H

Question

Hi, so I need help with an assignment I'm doing called Magic 8 Ball. It requires that I use Strings and Switch statements.

For this assignment, I'm using the Java language. Thanks in advance!

Assignment:

In 1950, Mattell produced a toy shaped like the 8 ball in pool. On the bottom of the ball was a window that displayed a random message. Each time the ball was shaken the ball would provide a randomly different message, purportedly as a clairvoyant answer to a question posed.

The answers were as follows –

1.It is certain

2. It is decidedly so

3. Without a doubt

4. Yes, definitely

5. You may rely on it

6. As I see it, yes

7. Most likely

8. Outlook good

9. Yes

10. Signs point to yes

11. Reply hazy try again

12. Ask again later

13. Better not tell you now

14. Cannot predict now

15. Concentrate and ask again

16. Don't count on it

17. My reply is no

18. My sources say no

19. Outlook not so good

20. Very doubtful

Directions for the assignment: Write a program that simulates the Magic 8 Ball. Prompt for a question, then provide a random answer selected from those given on the Magic 8 Ball. If the user does not enter a question, respond with the message “The 8 Ball can only answer questions.”. Questions end with a query mark. The program continues prompting until the user types “No more questions for the Magic 8 Ball.”.

Sample Output

Ask the Magic 8 Ball: Will I get an A on this assignment?

The 8 Ball says: Don’t count on it.

Ask the Magic 8 Ball: Should I use a switch statement?

The 8 Ball says: It is decidedly so.

Ask the Magic 8 Ball: No more questions for the Magic 8 Ball.

Explanation / Answer

package myProject;
import java.util.*;
//Class definition
public class MagicEightBall
{
   //Returns a random number
   int generateRandomNo()
   {
       //Random class object created
       Random rm = new Random();
       //Generates a random number between 1 and 20
       int num = rm.nextInt(20 - 1) + 1;
       //Returns the number
       return num;
   }//End of method
   //Method to generate answer
   void questionAnswer()
   {
       //Scanner class object created
       Scanner sc = new Scanner(System.in);
       //To store answer
       String answer = "";
       //Loops till user choice
       do
       {
           //Constant value for the answer
           answer = "The 8 Ball says: ";
           //Message for the user
           System.out.println(" To Stop Askign questions type: No more questions for the Magic 8 Ball.");
           System.out.println("Ask the Magic 8 Ball:");
           //Accepts question from the user
           String question = sc.nextLine();
           //Checks if the question is no more then stop
           if(question.equalsIgnoreCase("No more questions for the Magic 8 Ball."))
               break;
           //Otherwise generate a random number
           int ans = generateRandomNo();
           //Check the random number to generate answer
           switch(ans)
           {
               case 1:
                   answer += "It is certain.";
                   break;
               case 2:
                   answer += "It is decidedly so.";
                   break;
               case 3:
                   answer += "Without a doubt.";
                   break;
               case 4:
                   answer += "Yes, definitely.";
                   break;
               case 5:
                   answer += "You may rely on it.";
                   break;
               case 6:
                   answer += "As I see it, yes.";
                   break;
               case 7:
                   answer += "Most likely.";
                   break;
               case 8:
                   answer += "Outlook good.";
                   break;
               case 9:
                   answer += "Yes.";
                   break;
               case 10:
                   answer += "Signs point to yes.";
                   break;
               case 11:
                   answer += "Reply hazy try again.";
                   break;
               case 12:
                   answer += "Ask again later.";
                   break;
               case 13:
                   answer += "Better not tell you now.";
                   break;
               case 14:
                   answer += "Cannot predict now.";
                   break;
               case 15:
                   answer += "Concentrate and ask again.";
                   break;
               case 16:
                   answer += "Don't count on it.";
                   break;
               case 17:
                   answer += "My reply is no.";
                   break;
               case 18:
                   answer += "My sources say no.";
                   break;
               case 19:
                   answer += "Outlook not so good.";
                   break;
               case 20:
                   answer += "Very doubtful.";
                   break;
           }//End of switch
           //Displays the answer
           System.out.println(answer);
           //Re initialize the answer to null for next answer
           answer = "";
       }while(true);      
   }//End of method
   //Driver method to run the above functions
   public static void main(String ss[])
   {
       //Class object created
       MagicEightBall meb = new MagicEightBall();
       //Calls the method for question answer
       meb.questionAnswer();
   }//End of main method
}//End of class

Sample Run:

To Stop Askign questions type: No more questions for the Magic 8 Ball.
Ask the Magic 8 Ball:
Will I get an A on this assignment?
The 8 Ball says: My sources say no.

To Stop Askign questions type: No more questions for the Magic 8 Ball.
Ask the Magic 8 Ball:
Should I use a switch statement?
The 8 Ball says: As I see it, yes.

To Stop Askign questions type: No more questions for the Magic 8 Ball.
Ask the Magic 8 Ball:
Can I work hard?
The 8 Ball says: Without a doubt.

To Stop Askign questions type: No more questions for the Magic 8 Ball.
Ask the Magic 8 Ball:
No more questions for the Magic 8 Ball.

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