Question

In: Computer Science

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 an initializer list.
2. Generates a random number for the responses using a random object (in the range 0 to 19)
3. Prompts the user to enter a question.
4. Uses the random number to access and display the corresponding response (example: if the
random number generates 4 it should display “You may rely on it.”)
5. Asks the user if they want to ask another question (using a do – while loop). The code should
repeat as long as the user says “yes”.
A sample of the output are shown below:
What is your question? Will John get me some Panera tomorrow?
Reply hazy, try again.
Would you like to ask another question? (Answer yes or no): yes
What is your question? Will my dog become an instagram star?
Yes.
Would you like to ask another question? (Answer yes or no): yes
What is your question? Will I pass this class if I study hard?
Without a doubt.
Would you like to ask another question? (Answer yes or no): no
Notes:
• Use the equalsIgnoreCase method to accept any version of the word yes. You can use it
like str.equalsIgnoreCase(“yes”) where str is a variable of type string.
• You may have to deal with the annoying problem of capturing the [Enter] that is pressed
between responses. Remember, you can capture it with an extra nextLine method – but
be sure to put it in the right place!

Solutions

Expert Solution

// Java program to implement the Magic 8 ball popular fortune telling toy
import java.util.Random;
import java.util.Scanner;

public class Magic8Ball {

   public static void main(String[] args) {

       Scanner scan = new Scanner(System.in);
       // array to store 20 repsonses
       String responses[] = {"Yes.","Reply hazy, try again.","It is certain.","No.","Most likely.","Without a doubt.","Don't count on it.","You may rely on it.","It is decidedly so.","Ask again later.","Yes definitely.","As I see it, yes.","Signs point to yes.","Cannot predict now.","Concentrate and ask again.","My sources say no.","Outlook not so good.","Very doubtful.","Better not tell you now.","Outlook good."};
       String question;
       String contRes;
       int index;
      
       Random ran = new Random();
       // loop that continues till the user wats
       do
       {
           // input the question
           System.out.print("What is your question? ");
           question = scan.nextLine();
           // randomly generate the index of response
           index = ran.nextInt(responses.length);
           // display the response
           System.out.println(responses[index]);
           // ask the user if he wants to ask another question
           System.out.print("Would you like to ask another question? (Answer yes or no): ");
           contRes = scan.next(); // read the response
           scan.nextLine(); // ignore the enter
       }while(contRes.equalsIgnoreCase("yes"));
  
       scan.close();
   }

}
//end of program

Output:


Related Solutions

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...
1. The demand curve for the Magic 8-Ball toy is P = 15 – 0.5Q. Frances...
1. The demand curve for the Magic 8-Ball toy is P = 15 – 0.5Q. Frances currently has a patent on the concept of predictive billiards accessories, and is thus the only person able to sell Magic 8-Balls. For now, Frances is a monopoly. Her costs are C(QF) = 2QF a. Calculate the (monopoly) market price, quantity produced, and Frances’s profit.   b. Frances’s patent is about to expire, and another producer (Simon) is planning on entering the market. Simon has...
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
-create a magic 8 ball program in Javascript. -use a loop to ask for the question...
-create a magic 8 ball program in Javascript. -use a loop to ask for the question -use a random number to get the answer let randAnswer = Math.round(Math.random()*10); -must have at least10 different answers -must use elseif or switch statement -must outputs both answers and user to input the console -program should repeat indefinitely until either blank input or cancel is selected
Based on Mankiw's Why It Matters: Can Investors Outperform the Magic 8-Ball? in ch 27, what...
Based on Mankiw's Why It Matters: Can Investors Outperform the Magic 8-Ball? in ch 27, what is the answer from the Magic 8-Ball for the stock market predictions over the next year?
Create a java Swing GUI application that presents the user with a “fortune”. Create a java...
Create a java Swing GUI application that presents the user with a “fortune”. Create a java Swing GUI application in a new Netbeans project called FortuneTeller. Your project will have a FortuneTellerFrame.java class (which inherits from JFrame) and a java main class: FortuneTellerViewer.java. Your application should have and use the following components: Top panel: A JLabel with text “Fortune Teller” (or something similar!) and an ImageIcon. Find an appropriate non-commercial Fortune Teller image for your ImageIcon. (The JLabel has a...
The toy​ hoop, a popular​ children's toy in the​ 1950s, has gained popularity as an exercise...
The toy​ hoop, a popular​ children's toy in the​ 1950s, has gained popularity as an exercise in recent years. But does it​ work? To answer the​ question, an exercise council conducted a study to evaluate the cardio and​calorie-burning benefits of using this​ children's toy. Researchers recorded heart rate and oxygen consumption of adult​ participants, as well as their individual ratings of perceived​ exertion, at regular intervals during a​30-minute workout. Identify Who and What were investigated and the Population of interest....
A paddle ball toy consists of a flat wooden paddle and a small rubber ball that...
A paddle ball toy consists of a flat wooden paddle and a small rubber ball that are attached to each other by an elastic band (figure). You have a paddle ball toy for which the mass of the ball is 0.014 kg, the stiffness of the elastic band is 0.890 N/m, and the relaxed length of the elastic band is 0.325 m. You are holding the paddle so the ball hangs suspended under it, when your cat comes along and...
Magic Tyme is considering using existing machinery to produce a new magic kit. Which of the...
Magic Tyme is considering using existing machinery to produce a new magic kit. Which of the following items should be included in the analysis of the new product? I. Developing the product cost $50,000 II. Magic Tyme already sells a similar, but not identical, product III. The new product will be sold on credit IV. The fixed cost of the machinery is $20,000 per year A. II and III only B. II and IV only C. I, II, and III...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT