Question

In: Computer Science

Write a JAVA program that emulates a Magic Eight Ball. Start by generating a random number...

Write a JAVA program that emulates a Magic Eight Ball. Start by generating a random number and then use a switch statement to display the message.

Use this website as a resource on possible answers a Magic Eight Ball gives: https://en.wikipedia.org/wiki/Magic_8-Ball

Solutions

Expert Solution

Magic8_Ball.java:

   import java.io.*;
   import java.util.*;
   import java.util.Random;
   import java.lang.*;

   public class Magic8_Ball
   {
       public static void main(String[] args)
       {
           //generating random number using Math.Random()
           int random_magic = (int) Math.ceil(Math.random() *20);
           //aanswer variable for storing and displaying response
           String answer = "";
           //switch case for different output cases
           switch(random_magic)
           {
               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 ans 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;
               default:
                   answer = "8-Ball Error";
                   break;
           }
           //printing the response
           System.out.println("\nMagic 8-Ball says: " + answer);
       }
   }
              

Output:


Related Solutions

Magic 8 Ball(JAVA) A magic 8 ball is a popular fortune telling toy in which the...
Magic 8 Ball(JAVA) A magic 8 ball is a popular fortune telling toy in which the user places the ball face down, asks a yes-or-no question and turns the ball face up to reveal the answer. The standard magic 8 ball has 20 standard answers shown below, where 10 are positive (green), 5 are non-committal (yellow), and 5 are negative (red) (credit: Wikipedia) Write a program that does the following: 1. Stores all the responses in a String array using...
In Java: Write a program that generates a random number and asks the user to guess...
In Java: Write a program that generates a random number and asks the user to guess the number and keeps track of how many guesses it took If the user input is negative or zero then the loop must stop reading further inputs and display how many guesses they used If they guess the correct number display a message telling them they got it and exit the program If they guess the wrong number (but still a legal guess) you...
Write a Java program that calculates a random number 1 through 100. The program then asks...
Write a Java program that calculates a random number 1 through 100. The program then asks the user to guess the number.If the user guesses too high or too low then the program should output "too high" or "too low" accordingly.The program must let the user continue to guess until the user correctly guesses the number. ★Modify the program to output how many guesses it took the user to correctly guess the right number
Write a Java program that implements the Number Guessing Game: 1. First generate a random number...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number (int) between 0 and 100, call it N 2. Read user input (a guess) 3. check the number, if it's smaller than N, output "The number is larger than that" 4. If the input is larger than N, output "The number is smaller than that" 5. If the input is equal to N, output " You got it!", and exit 6. Repeat until the...
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays...
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays a random response to a yes or no question. In the student sample programs for this book, you will find a text file named 8_ball_responses.txt. The file contains 12 responses, such as “I don’t think so”, “Yes, of course!”, “I’m not sure”, and so forth. The program should read the responses from the file into a list. It should prompt the user to ask...
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays...
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays a random response to a yes or no question. In the student sample programs for this book, you will find a text file named 8_ball_responses.txt. The file contains 12 responses, such as “I don’t think so,” “Yes, of course!,” “I’m not sure,” and so forth. The program should read the responses from the file into an array or ArrayList object. It should prompt the...
You will write a Java Application program to perform the task of generating a calendar for...
You will write a Java Application program to perform the task of generating a calendar for the year 2020. You are required to modularize your code, i.e. break your code into different modules for different tasks in the calendar and use method calls to execute the different modules in your program. Your required to use arrays, ArrayList, methods, classes, inheritance, control structures like "if else", switch, compound expressions, etc. where applicable in your program. Your program should be interactive and...
A program for generating random numbers on a computer is to be tested. The program is...
A program for generating random numbers on a computer is to be tested. The program is instructed to generate 100 single-digit integers between 0 and 9. The frequencies of the observed integers were as follows. At the 0.05 level of significance, is there sufficient reason to believe that the integers are not being generated uniformly? Integer 0 1 2 3 4 5 6 7 8 9 Frequency 9 7 9 6 10 14 8 10 12 15 (a) Find the...
A program for generating random numbers on a computer is to be tested. The program is...
A program for generating random numbers on a computer is to be tested. The program is instructed to generate 100 single-digit integers between 0 and 9. The frequencies of the observed integers were as follows. At the 0.05 level of significance, is there sufficient reason to believe that the integers are not being generated uniformly? Integer 0 1 2 3 4 5 6 7 8 9 Frequency 9 7 9 6 10 14 8 10 12 15 (a) Find the...
A program for generating random numbers on a computer is to be tested. The program is...
A program for generating random numbers on a computer is to be tested. The program is instructed to generate 100 single-digit integers between 0 and 9. The frequencies of the observed integers were as follows. At the 0.05 level of significance, is there sufficient reason to believe that the integers are not being generated uniformly? Integer 0 1 2 3 4 5 6 7 8 9 Frequency 10 8 7 9 13 11 5 9 14 14 (a) Find the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT