For this lab, you are going to build a simple magic eight ball, so you no longer have to make any choices. On that note, if you haven’t, you should watch Interstate 60. All joking aside, you will build a 15 sided magic 8 ball, just for simplicity (an actual one has 20 sides for choices).
If you are curious about all the choices, you can read up more on them here.
Also, if you need a refresher on if statements, go here.
Step 1 - Reading the code
This is one of the few labs you will only have to write a single method. While you are free to write more methods (hint: it may be easier with more), you are not required to write more. Before you start, take a moment to read the code provided.
You will notice that the method missing is:
public static String getAnswer(int category, int answer). This is the method you will write. For reference, you method could look like the following, and it would make sense to put in the follow method stub and make sure the program runs.
public static String getAnswer(int category, int answer) {
// if statements here
return "Misty";
}
getAnswer() has two ints that are passed in as parameters. They represent a category and an answer.
Category
The first int is between 0 and 99 and it defines your category. The higher the number the better.
You will write three if statements for the categories below:
- Positive Answers are equal to 74 or above
- Unsure Answers are 24 to 73
- Negative answers are less than 24
Once your category is defined, the second int determines the answer that will be returned.
Answer Options
Based on the category, you will have one of five possible answers to be returned as a String. It is important to note, that the answers range from 0-4 (ints only), as such, if you are in the positive category (74 or above) and answer is equal to 0, you will return the String “As I see it, yes.”
Here are the different answer options in order (0-4) for each category:
Positive (>= 74)
- As I see it, yes.
- Signs point to yes.
- Outlook good.
- Without a doubt.
- You may rely on it.
Unsure (24-73)
- Reply hazy, try again.
- Ask again later.
- Better not tell you now.
- Cannot predict now.
- Concentrate and ask again.
Negative Answers (< 24)
- Don’t count on it.
- My reply is no.
- My sources say no.
- Outlook not so good.
- Very doubtful.
Using this knowledge, build your if/else statements (note, you will need to use nested if statements) to return an answer based on the combination.
If you need a refresher about comparision operators used in if statements click here
Testing
While you can run the program with random to test, you may find this difficult. Looking back at the other programs the past couple weeks, how can you incrementally test to make sure your method is working?
Turning in
As per usual, you will have 5 attempts for graded solutions, so make sure you test it before you submit for grading.