Question: PROMT: Step 1 - Reading the code This is one of the few labs you will only have to write a single method. While you

PROMT:

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)

  • Dont 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.

CODE:

import java.util.Random; import java.util.Scanner;

/** * * Basic magic 8 ball, with an equal number of options in each category. Teaches * basic if / else statements. * * * @author MARGOT NAFF * MGNAFF@RAMS.COLOSTATE.EDU * Computer Science Department * Colorado State University * @version 201990 */ public class EightBall { private static Scanner scanner = new Scanner(System.in); private static Random rnd = new Random();

// STUDENT CODE HERE

// ************** don't modify below this line ************************ public static void main(String[] args) { printSpash(); run(); }

public static void printSpash() { System.out.println("88888888888888888888"); System.out.println("8 Magic 8 Ball 8"); System.out.println("8 Ask away 8"); System.out.println("88888888888888888888"); System.out.println(" And the answer is... "); }

public static void run() { int cat = rnd.nextInt(100); int an = rnd.nextInt(5); System.out.println("category num: " + cat + " answer num: " + an); System.out.println(getAnswer(cat, an)); // gets 0-99 and 0-4 (one less than what you put in) System.out.println(); System.out.print("Ask another question (Yes/No?): "); String input = scanner.nextLine(); if(input.toLowerCase().startsWith("n")) { // this ia fun trick, worth remembering it. System.out.println("Thank you for playing!"); return; } System.out.println(" And the answer is... "); run(); // continue looping! }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!