Re-do the Magic8Ball program from BA5 (See below for BA5). The program should ge
ID: 3706082 • Letter: R
Question
Re-do the Magic8Ball program from BA5 (See below for BA5). The program should generally work the same as it did in BA5, but updated to include these items:
Create a method called get8BallAnswers (2 point)
The method should have no parameters
The method will build the array of possible String answers (from BA5), and return the entire array
Overload the get8BallAnswers method (2 points)
The method should have 1 integer parameter, where the user can pass a random number to it
The method should return 1 possible String answer from the array. Hint: You can use the method you created in step 2 to retrieve the array of answers.
Create a method called magic8Ball that will basically run the whole program. (2 point)
It should ask the user to enter a question for the 8 Ball, generate a random number, and pass that to one of the methods above to get a response.
You will call this magic8Ball method in the main method.
The method should be called within the do-while loop, and repeat if the user wants to run the program again or not.
Extra Credit: +1 point
Write JavaDoc style comments for all classes and all methods in the program
Run the JavaDoc tool to generate an API for your program. Zip up the doc folder that it created, and attach that along with your assignment.
Please note: You will lose points if you don't follow the standard Java conventions. This includes proper indentation, proper naming of variables and classes, etc.
BA5 Assignment
BA5 (Arrays)
Points: 6 points
Due: Thursday, March 15 at 8:00am
Late: -4 points off, and not accepting after 2 weeks
Write a program called Magic8Ball that simulates a Magic 8 Ball, which is a fortune-telling toy that displays a random response to a yes or no question.
1. Create an array that will hold all these 12 Magic 8 Ball responses. The array must be the correct data type, and have the correct size set. (2 points)
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.
2. Prompt the user to ask a question, and then display one of the responses, randomly selected from the array. (1 point)
Use the Random class or the Math class and figure out how to generate a random number. For example, if the random number generated is 4, then you will select index 4 from the array above. (2 points)
3. The program should repeat until the user is ready to quit (1 point)
Extra credit: If you can re-do the assignment using ArarayList instead of regular arrays, you will get 1 extra point. You will need to learn how to use ArrayList on your own. Submit two versions of the program, the regular array and the array list version.
HINT: If you have an issue where it skips your question the 2nd time around, then you can try adding this line of code at the end of your loop where the issue happens: scannerObject.nextLine(). This resolves the issue by consuming the newline character.
Example output:
Please ask any question?
Will I win the lottery?
I don't think so.
Do you want to try again? Yes or No
Yes
Please ask any question?
Will I pass my Java course?
I can't tell you right now.
Do you want to try again? Yes or No
No
Goodbye!
BA5 Answer
import java.util.Random;
import java.util.Scanner;
publicclassMagic8Ball{
privatestaticScanner sc;
publicstaticvoidmain(String [] args) {
String responses[] = newString[] { "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."};
boolean flag = true;
sc = newScanner(System.in);
String que;
while(flag){
System.out.print("Ask Question here : ");
que = sc.nextLine();
if(que.equals("No questions")){
flag = false;
return;
}
System.out.println();
// randomly select response from response array
Random rand = newRandom();
intv = rand.nextInt(12);
System.out.println("Ans : "+ responses[v]);
System.out.println(" ");
}
}
}
Explanation / Answer
Hi Dear,
I have implemented Q1 : methods addition.
Please repost for Extra Credit implementation.
Ask Question here : 4
Ans : No way!
Ask Question here : 5
Ans : Without a doubt, no.
Ask Question here : 7
Ans : I can't tell you right now.
Ask Question here : No questions
Process finished with exit code 0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.