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...
-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.round()*10); - must have at least 10 different answers -Must use either elseif or switch statement for answer -must output both answer and user input to console - the program should repeat indefinitely until either blank input, or cancel is selected Bug in jsbin.com Please don't use HTML thanks
-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 Fortran program which simulates placing 100 molecules into the boxes of a 20 by...
Write a Fortran program which simulates placing 100 molecules into the boxes of a 20 by 20 square grid. Each box can hold at most one molecule. Your program should count and report how many molecules in the final arrangement have no neighbors. Two molecules are considered neighbors if they are directly above or below or directly side by side (diagonals don't count). For instance, if molecules were placed at the locations labelled by letters in the following 5 by...
For this lab you will implement the class Ball which represents a toy ball. Ball should...
For this lab you will implement the class Ball which represents a toy ball. Ball should have a method bounce() whose return type is void, and should have an integer member bounces that counts the number of times the ball has been bounced. Ball should also have a static method named getTotalBounces which counts the total number of times that any Ball object has been bounced. To do this, you will want to include a static integer member totalBounces which...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT