The purpose of this exercise is
- practice arrays
- practice generating random numbers
- practice reading from a file
- practice using loops
Your GTA will lead you through the following:
Objective: To create a magic 8 ball program
Write a java program that will read in a list of Magic 8-ball responses, and provide random responses when prompted to by the user. Call your program magic8ball.java.
First, prompt the user for the name of the response file. Files to be used are here, here and here.
Read through the file line by line, and enter each response into a String array. It is safe to assume that there will never be more than 20 lines in a response file.
HINT - this means you can start by declaring a String array of size 20:
String [] responses = new String[20];
Use a loop to ask the user if they have another question. If the user replies with a Y, randomly choose a response from your response array. If the user replies with a N, exit the program. If the user enters anything else, inform them that the input is incorrect, and prompt again.
Sample loop 1 (valid input):
What response file do you want to load? response1.txt
Welcome to the magic 8 ball oracle.
Do you have another question? (Y or N) Y
The magic 8 ball says: Outlook Cloudy
Do you have another question? (Y or N) N
Goodbye!
Sample output 2 (invalid input):
What response file do you want to load? response2.txt
Welcome to the magic 8 ball oracle.
Do you have another question? (Y or N) 42
I'm sorry, I don't understand.
Do you have another question? (Y or N) Y
The magic 8 ball says: That looks promising!
Do you have another question? (Y or N) N
Goodbye!
You must show your GTA your work to get credit for this lab completion.
© 2009 CSU