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:
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)
Unsure (24-73)
Negative Answers (< 24)
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("\nAnd the answer is...\n");
}
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("\nAnd the answer is...\n");
run(); // continue looping!
}
}
Code to write method getAnswer()
1. Method getAnswer(int category, int answer)
This method accepts two int parameters and returns a string value to the main function according to the values of parameters accepted.
The method used nested if conditions to find and return the answer value.
public static String getAnswer(int category, int answer) {
//Variable Declarations
String categ ="" ; //To store category
String retanswer ="" ; //Stores answer to return
// if statements here
//To Check Category
if(category>=74)
{
categ ="Positive" ;
//If statements to check answers of category
if(answer==0) retanswer = "As I see it, yes.";
else if (answer==1) retanswer = "Signs point to yes.";
else if (answer==2) retanswer = "Outlook good.";
else if (answer==3) retanswer = "Without a doubt.";
else if (answer==4) retanswer = "You may rely on it.";
}
else if(category>23 && category<74)
{
categ ="Unsure" ;
if(answer==0) retanswer = "Reply hazy, try again.";
else if (answer==1) retanswer = "Ask again later.";
else if (answer==2) retanswer = "Better not tell you now.";
else if (answer==3) retanswer = "Cannot predict now.";
else if (answer==4) retanswer = "Concentrate and ask again.";
}
else if(category<24)
{
categ ="Negative" ;
if(answer==0) retanswer = "Don’t count on it.";
else if (answer==1) retanswer = "My reply is no.";
else if (answer==2) retanswer = "My sources say no.";
else if (answer==3) retanswer = "Outlook not so good.";
else if (answer==4) retanswer = "Very doubtful.";
}
//Returns answer to Main function
return retanswer;
}
2. Complete java program to test the above 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
//Function to getAnswer by accepting two int parameters and returns
a string value
public static String getAnswer(int category, int answer) {
//Variable Declarations
String categ ="" ; //To store category
String retanswer ="" ; //Stores answer to return
// if statements here
//To Check Category
if(category>=74)
{
categ ="Positive" ;
//If statements to check answers of category
if(answer==0) retanswer = "As I see it, yes.";
else if (answer==1) retanswer = "Signs point to yes.";
else if (answer==2) retanswer = "Outlook good.";
else if (answer==3) retanswer = "Without a doubt.";
else if (answer==4) retanswer = "You may rely on it.";
}
else if(category>23 && category<74)
{
categ ="Unsure" ;
if(answer==0) retanswer = "Reply hazy, try again.";
else if (answer==1) retanswer = "Ask again later.";
else if (answer==2) retanswer = "Better not tell you now.";
else if (answer==3) retanswer = "Cannot predict now.";
else if (answer==4) retanswer = "Concentrate and ask again.";
}
else if(category<24)
{
categ ="Negative" ;
if(answer==0) retanswer = "Don’t count on it.";
else if (answer==1) retanswer = "My reply is no.";
else if (answer==2) retanswer = "My sources say no.";
else if (answer==3) retanswer = "Outlook not so good.";
else if (answer==4) retanswer = "Very doubtful.";
}
//Returns answer to Main function
return retanswer;
}
// ************** don't modify below this line ************************
public static void main(String []args){
// System.out.println("Hello World");
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("\nAnd the answer is...\n");
}
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("\nAnd the answer is...\n");
run(); // continue looping!
}
}
Output Screen

Program Screens



PROMT: Step 1 - Reading the code This is one of the few labs you will...
what is wrong with the following code? public class EightBall { private static Scanner scanner = new Scanner(System.in); private static Random rnd = new Random(); public static String getAnswer(int category, int answer) { if (category >= 74) { if (answer == 0) { return "As I see it, yes." ; } else if (answer == 1) { return "Signs point to yes." ; } else if (answer == 2) { return "Outlook good." ; } else if...
Please make the following code modular. Therefore contained in a package with several classes and not just one. import java.util.*; public class Q { public static LinkedList<String> dogs = new LinkedList<String> (); public static LinkedList<String> cats = new LinkedList<String> (); public static LinkedList<String> animals = new LinkedList<String> (); public static void enqueueCats(){ System.out.println("Enter name"); Scanner sc=new Scanner(System.in); String name = sc.next(); cats.addLast(name); animals.addLast(name); } ...
Identify a logical error in the following code and fix it. public class test1 { public static void main(String[] args){ int total; float avg; int a, b, c; a=10; b=5; c=2; total=a+b+c; avg=total/3; System.out.println("Average: "+avg); } } Answer: Write the output of the program below. import java.util.Scanner; public class test2 { public static void main(String[] arg){ int psid; String name; Scanner input=new Scanner(System.in); System.out.println("Enter your PSID"); psid=input.nextInt(); System.out.println("Enter your...
/**
*
* Jumping frog game: user enters road length and series of jumps and
* the program displays the current position of the frog.
*/
public class hw4_task5 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int playAgain = 1;
int roadLen;
System.out.println("JUMPING FROG");
while (playAgain == 1){
System.out.printf("Enter the length of the road: ");
roadLen = in.nextInt();
play(roadLen); // Starts a new game with a road this long.
System.out.printf("\nDo you want to play again...
10. Recursive Append Download the file AppendRec.java. Write your code inside the appendNTimes method and use the main method to test your code. Submit the file AppendRec.java. There is no need to delete the main method, the autograder will only grade appendNTimes. The method appendNTimes is recursive and takes two arguments, a string and an integer. It returns the original string appended to the original string n times. The method signature is as follows public static String appendNTimes ( String...
Hello, Could you please input validate this code so that the code prints an error message if the user enters in floating point numbers or characters or ANYTHING but VALID ints? And then could you please post a picture of the output testing it to make sure it works? * Write a method called evenNumbers that accepts a Scanner * reading input from a file with a series of integers, and * report various statistics about the integers to the...
Your code should contain appropriate validations and must focus on code optimization. Briefly, explain (150-200 words) the logic of the program and how the code can be optimized. Modify the code to repeat the program until the user enters “no”. (*I need the logical write out on how the program behaves when there is an input from the user. (I don't need the //comments). import java.util.Scanner; public class Q2 { public static void main(String[] args) { Scanner...
Write the example code to describe overloading and submit here. You need to put comments for your code. You need to add at least one more overloaded method to the example code. public class OverloadingExample { public static void main(String[] args) { OverloadingExample t = new OverloadingExample(); t.methodX(5,9,2.2); t.methodX(5,9,"Hi"); t.methodX(5,9,2); t.methodX(5,9); } public void methodX(int a){ System.out.println("This is the method X with 1 parameters!"); } public void methodX(int a,int b){ System.out.println("This is the method X with 2 parameters!"); } public...
JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in); System.out.println("Seed:"); int seed = scanner.nextInt(); System.out.println("Length"); int length = scanner.nextInt(); Random random...
Need Help ASAP!! Below is my code and i am getting error in
(public interface stack) and in StackImplementation class. Please
help me fix it. Please provide a solution so i can fix the
error.
thank you....
package mazeGame;
import java.io.*;
import java.util.*;
public class mazeGame {
static String[][]maze;
public static void main(String[] args)
{
maze=new String[30][30];
maze=fillArray("mazefile.txt");
}
public static String[][]fillArray(String file)
{
maze = new String[30][30];
try{...