Question

In: Computer Science

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 user to ask a question, and then display one of the responses, randomly selected from the array or ArrayList object. The program should repeat until the user is ready to quit.

You will need to create two .java files for this assignment: a Magic 8 Ball Class and a Demo class.

All methods should be documented with javadoc style comments.

Contents of 8_ball_responses.txt:

 Yes, of course!
 Without a doubt, yes.
 You can count on it.
 For sure!
 Ask me later.
 I’m not sure.
 I can’t tell you right now.
 I’ll tell you after my nap.
 No way!
 I don’t think so.
 Without a doubt, no.
 The answer is clearly NO.

Solutions

Expert Solution

Note:

We have paste the input file in the project folder so that our program can able to detect.

Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

// 8_ball_responses.txt (Input file)

Yes, of course!
Without a doubt, yes.
You can count on it.
For sure!
Ask me later.
I'm not sure.
I can't tell you right now.
I'll tell you after my nap.
No way!
I don't think so.
Without a doubt, no.
The answer is clearly NO.

==================================

// Magic8BallGame.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class Magic8BallGame {
   private ArrayList<String> arl=null;
public Magic8BallGame() {
   String line;
       arl=new ArrayList<String>();
       try {
           Scanner sc=new Scanner(new File("8_ball_responses.txt"));
           while(sc.hasNext())
           {
               line=sc.nextLine();
               arl.add(line);
           }
           sc.close();
       } catch (FileNotFoundException e) {
           System.out.println("** File Not Found **");
       }
      
   }

public void askQuestion()
{
int num = ((int)(Math.random() * 100) % (arl.size()-1));
System.out.println(arl.get(num));
}
}

=============================

// Test.java

import java.util.Scanner;

public class Test {

   public static void main(String[] args) {
       String ques;
       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);
       Magic8BallGame mbg = new Magic8BallGame();
       System.out.println("I AM THE MAGIC 8 BALL");
       // Read the Question entered by the user
       System.out.print("Ask me a question about the future (Stop to exit):");
       ques = sc.nextLine();
       while (!ques.equalsIgnoreCase("Stop")) {

           mbg.askQuestion();
           System.out.print("\nAsk me a question abouty the future (Stop to exit):");
           ques = sc.nextLine();  
       }

   }

}

===============================

Output:

I AM THE MAGIC 8 BALL
Ask me a question about the future (Stop to exit):How is my future
I don't think so.

Ask me a question abouty the future (Stop to exit):Will I become rich?
Yes, of course!

Ask me a question abouty the future (Stop to exit):Will I fall in love?
I'm not sure.

Ask me a question abouty the future (Stop to exit):Stop

=====================Could you plz rate me well.Thank You


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...
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...
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...
-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
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
6-Write a module in pseudocode called magicSix(), which accepts two integers and displays a message “Magic...
6-Write a module in pseudocode called magicSix(), which accepts two integers and displays a message “Magic 6!” if either of the two integers is a 6 or if their sum or difference is a 6. Otherwise, the program will display “Not a magic 6.” Note, you will need to determine the larger number when calculating the difference, to get a positive difference. You cannot use any built-in Python functions to do this. Type your pseudocode into your answer document. 7-...
Objective: Write a program which simulates a hot potato game. In this version of a classic...
Objective: Write a program which simulates a hot potato game. In this version of a classic game, two or more players compete to see who can hold onto a potato the longest without getting caught. First the potato is assigned a random value greater than one second and less than three minutes both inclusive. This time is the total amount of time the potato may be held in each round. Next players are put into a circular list. Then each...
Write a C++ program that displays the numbers between 1000 and 9999, which are divisible by...
Write a C++ program that displays the numbers between 1000 and 9999, which are divisible by sum of the digits in them. For example, 2924 is divisible by (2+9+2+4 = 17). Your program should display all these possible numbers in the given range, and each number should be separated from the other by a space.
In C++  Write a program that simulates coin tossing. For each toss of the coin the program...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program should print heads or tails. Let the program toss the coin 100 times and count the number times each side of the coin appears. Print the results. 0 represents tails and 1 for heads.
Please use Phyton to write a program: Write a program that calculates and displays the total...
Please use Phyton to write a program: Write a program that calculates and displays the total bill at a restaurant for a couple that is dining. The program should collect from the couple, cost of each meal, and the percentage of the final cost that they would like to tip. The sales tax in the state where the restaurant exists is 7.5%. Display to the user, line by line: Total Cost of Both Meals Sales Tax in dollars Tip in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT