Question: A getter and setter creating problem in Java.. need helps creating a Magic8Ball class: write a class for the Magic 8 Ball. The class should
A getter and setter creating problem in Java.. need helps
creating a Magic8Ball class:
write a class for the Magic 8 Ball. The class should have a method that returns an answer at random by generating a random number that allows choosing one of the following 8 responses below; call that method getAnswer() it should take no parameters and return a String.
These Java statements will generate a random number between 1 and 8: import java.util.Random; // at the top or import java.util.*; static Random r = new Random(); // put this statement inside the class, but outside/above all methods int num = r.nextInt(8) + 1; // put this inside the getAnswer() method // use num in an if or switch statement to return a response below // your Magic8Ball class should start like this: import java.util.Random; public class Magic8Ball { static Random r = new Random(); ... public String getAnswer() { int num = r.nextInt(8) + 1; // use if-else/if or switch to return the appropriate String below }
These are the 8 responses that getAnswer() should randomly choose from and return: 1. It is certain 2. It is decidedly so 3. Most likely 4. Signs point to yes 5. Reply hazy, try again 6. Ask again later 7. Dont count on it 8. My sources say no
Test your class by creating a Magic8Ball object and printing what its getAnswer() method returns several times it should print several different answers, but they are not guaranteed to all be different; that is, some answers might be duplicates. You can put your tests in main.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
