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 (answer == 3) {
return "Without a doubt." ;
}
else if (answer == 4) {
return "You may rely on it." ;
}
}
else if (category >= 24 && category < 74) {
if (answer == 0) {
return "Reply hazy, try again." ;
}
else if (answer == 1) {
return "Ask again later." ;
}
else if (answer == 2) {
return "Better not tell you now." ;
}
else if (answer == 3) {
return "Cannot predict now." ;
}
else if (answer == 4) {
return "Concentrate and ask again." ;
}
}
else if (category < 24) {
if (answer == 0) {
return "Don’t count on it." ;
}
else if (answer == 1) {
return "My reply is no." ;
}
else if (answer == 2) {
return "My sources say no." ;
}
else if (answer == 3) {
return "Outlook not so good." ;
}
else if (answer == 4) {
return "Very doubtful." ;
}
}
}
// ************** 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!
}
}
Explanation:
Code in JAVA:
import java.util.Random;
import java.util.Scanner;
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 (answer
== 3) {
return "Without a doubt." ;
}else if (answer
== 4) {
return "You may rely on it." ;
}
}else if (category >= 24
&& category < 74) {
if (answer == 0)
{
return "Reply hazy, try again." ;
}else if (answer
== 1) {
return "Ask again later." ;
}else if (answer
== 2) {
return "Better not tell you now." ;
}else if (answer
== 3) {
return "Cannot predict now." ;
}else if (answer
== 4) {
return "Concentrate and ask again." ;
}
}else if (category < 24) {
if (answer == 0)
{
return "Don’t count on it." ;
}else if (answer
== 1) {
return "My reply is no." ;
}else if (answer
== 2) {
return "My sources say no." ;
}else if (answer
== 3) {
return "Outlook not so good." ;
}else if (answer
== 4) {
return "Very doubtful." ;
}
}
/**
* even though category variable
value conditions are covered, still
* in java if-else conditions can be
not covered all i.e not even a single condition be true
* in some cases. So we need to have
default return statement at the end of the function.
* */
return "";
}
// ************** 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!
}
}
OUTPUT:

Please provide the feedback!!
Thank You!!
what is wrong with the following code? public class EightBall { private static Scanner scanner =...
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,...
(How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...
import java.util.Scanner; public class SieveOfEratosthenes { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number"); int num = sc.nextInt(); boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j<num; j = j+i) { bool[j] = false;...
import java.util.Scanner; public class Age { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int a; System.out.println("Input your age"); a = sc.nextInt(); boolean mess = isAllowed(a); String mess2 = ?isAllowed(a)return"You are allowed to vote";:"You arent allowed"; String age = personAge(a); personAge(a); } public static String personAge(int age) { String mess = ""; if(age<18) return mess = "You are minor"; else if(age>=18 && age<=22) return mess = "You are legal you can vote"; else if(age>=22 && age<=60) return...
Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn); else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...
Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp { public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...
How could I take this code and make a JavaFX program with the same results? import java.util.Scanner; public class CellPhoneApp { private static int[] data = {8,16,20}; private static double[] dataPrice = {45.00,65,.00,99.00}; private static int[] model ={100,110,200}; private static double[] modelPrice= {299.95,399.95,499.95}; private static String[] options = {"Phone Replacement Insurance","WiFi Hotspot Capability"}; private static double[] optionsPrice ={5.00,10.00}; private static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int planIndex = getDataPlan(); int modelIndex = getModel(); int...
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...
public static void main(String[] args) { System.out.println("Welcome to the Future Value Calculator\n"); Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // get the input from the user System.out.println("DATA ENTRY"); double monthlyInvestment = getDoubleWithinRange(sc, "Enter monthly investment: ", 0, 1000); double interestRate = getDoubleWithinRange(sc, "Enter yearly interest rate: ", 0, 30); int years = getIntWithinRange(sc, "Enter number of years: ", 0, 100); System.out.println(); ...
import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String inputMonth; int inputDay; inputMonth = scnr.nextString(); inputDay = scnr.nextInt(); String season; if(inputMonth == "April","May","June"){ String season = "Spring"; }else if(inputMonth == "July","August","September"){ String season = "Summer"; }else if(inputMonth == "October","November","December"){ String season = "Autumn"; }else if(inputMonth == "January","February","March"){ System.out.println("Winter"); }else{ System.out.println("Invalid"); } if((inputMonth == "March") && (inputDay >= 20)){ String season = "Spring"; }else if((inputMonth == "June") && (inputDay >= 21)){...