In: Computer Science
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.
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