Question

In: Computer Science

package week_2; import java.util.Random; import static input.InputUtils.*; /** * Your program should generate a random number...

package week_2; import java.util.Random; import static input.InputUtils.*; /** * Your program should generate a random number between 0 and 9, and challenge the user to guess the number. Write a loop that asks the user to guess a number that the computer is thinking of. Print a success message if they guess correctly. If the user does not guess correctly, tell the user that they need to guess higher, or lower, and ask the user to try again. The user should be able to have as many guesses as they need. Once the user guesses correctly, tell the user how many guesses they needed to get the right number. Read the instructions in the TODO messages. */ import java.util.Random; import java.util.Scanner; public class Question_4_Random_Number_Guessing_Game { Random rnd = new Random(); String CORRECT = "Correct!!"; String LOW = "Too low"; String HIGH = "Too high"; public static void main(String[] args) { new Question_4_Random_Number_Guessing_Game().play(); } public void play() { int secret = generateSecretNumber(10); while (true) { // ask user for their guess int guess = getGuess(); // Check if the guess is correct String result = checkGuess(secret, guess); // Print the result (correct, too high, too low) for the user System.out.println(result); // TODO Write an if-statement comparing the global CORRECT variable to result if (secret == guess) System.out.println(CORRECT); else if (secret > guess) System.out.println(LOW); else System.out.println(HIGH); break; // to test if result is correct. If so, end the loop with a break statement } } public int getGuess() { // TODO use intInput to print a message like "Enter your guess?" and get the user's guess. // TODO modify the following line to return the number the user entered. return 0; } public String checkGuess(int secret, int guess) { // TODO Return CORRECT if secret is the same as guess - use the global CORRECT variable // TODO Return LOW if guess is too low - use the global LOW variable // TODO return HIGH if guess is too high - use the global HIGH variable return null; //replace with your code } public int generateSecretNumber(int max) { // TODO generate a random number between 0 and max-1 // The smallest value possible should be 0 // The largest value possible should be max - 1 // Example: if max = 4, the random number should be a random selected value from 0, 1, 2, 3 // Use the global Random rnd variable (declared at the top of the program) to generate the number. Don't create another Random. return 0; //replace with your code } }

Solutions

Expert Solution

import java.util.Random;

import java.util.Scanner;

public class Question_4_Random_Number_Guessing_Game {

Random rnd = new Random();

String CORRECT = "Correct!!";

String LOW = "Too low";

String HIGH = "Too high";

//Scanner Object

Scanner input=new Scanner(System.in);

public static void main(String[] args) {

new Question_4_Random_Number_Guessing_Game().play();

}

public void play() {

int secret = generateSecretNumber(10);

int count=0;

while (true) {

// ask user for their guess int guess = getGuess();

// Check if the guess is correct String result = checkGuess(secret, guess);

// Print the result (correct, too high, too low) for the user System.out.println(result);

// TODO Write an if-statement comparing the global CORRECT variable to result

int guess=getGuess();

String result=checkGuess(secret,guess);

System.out.println(result);

count++;

if(result==CORRECT)

break;

}

System.out.println(count+" guess taken");

}

public int getGuess() {

// TODO use intInput to print a message like "Enter your guess?" and get the user's guess.

// TODO modify the following line to return the number the user entered.

System.out.print("Enter Your guess? ");

int guess=input.nextInt();

return guess;

}

public String checkGuess(int secret, int guess) {

// TODO Return CORRECT if secret is the same as guess - use the global CORRECT variable

// TODO Return LOW if guess is too low - use the global LOW variable

// TODO return HIGH if guess is too high - use the global HIGH variable return null;

//replace with your code

if (secret == guess)

return CORRECT;

else if (secret > guess)

return LOW;

return HIGH;

}

public int generateSecretNumber(int max)

{

// TODO generate a random number between 0 and max-1

// The smallest value possible should be 0

// The largest value possible should be max - 1

// Example: if max = 4, the random number should be a random selected value from 0, 1, 2, 3

// Use the global Random rnd variable (declared at the top of the program) to generate the number.

//Don't create another Random.

int randomNumber=rnd.nextInt(max);

return randomNumber;

}

}

PS: In case of any doubt do post in comments.


Related Solutions

My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) {...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) { int N; Scanner keybd = new Scanner(System.in); int[] counts = new int[12];    System.out.print("Enter the number of trials: "); N = keybd.nextInt();    Random die1 = new Random(); Random die2 = new Random(); int value1, value2, sum; for(int i = 1; i <= N; i++) { value1 = die1.nextInt(6) + 1; value2 = die2.nextInt(6) + 1; sum = value1 + value2; counts[sum-1]++; }   ...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts)...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts) { return quarts * 0.25; } public static double milesToFeet(double miles) { return miles * 5280; } public static double milesToInches(double miles) { return miles * 63360; } public static double milesToYards(double miles) { return miles * 1760; } public static double milesToMeters(double miles) { return miles * 1609.34; } public static double milesToKilometer(double miles) { return milesToMeters(miles) / 1000.0; } public static double...
I need a pseudocode and UML for this program. import java.util.Random; public class SortandSwapCount {   ...
I need a pseudocode and UML for this program. import java.util.Random; public class SortandSwapCount {    public static void main(String[] args) {        //Generate random array for test and copy that into 3 arrays        int[] arr1=new int[20];        int[] arr2=new int[20];        int[] arr3=new int[20];        int[] arr4=new int[20];        Random rand = new Random();        for (int i = 0; i < arr1.length; i++) {            //Generate random array...
import java.util.Random; import java.util.Scanner; public class Compass { public Random r; public Compass(long seed){ r =...
import java.util.Random; import java.util.Scanner; public class Compass { public Random r; public Compass(long seed){ r = new Random(seed); }    public static String numberToDirection(int a){ if(a==0) return "North";    if(a==1) return "NorthEast"; if(a==2) return "East"; if(a==3) return "Southeast"; if(a==4) return "South"; if(a==5) return "Southwest"; if(a==6) return "West";    if(a==7) return "Northwest";    return "Invalid Direction" ; } public String randomDirection(){ return numberToDirection(r.nextInt()% 4 + 1); } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter seed: ");...
Using import java.util.Random, create a simple java code that populates an array with 10 random numbers...
Using import java.util.Random, create a simple java code that populates an array with 10 random numbers and then outputs the largest number.
Write a Java program that implements the Number Guessing Game: 1. First generate a random number...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number (int) between 0 and 100, call it N 2. Read user input (a guess) 3. check the number, if it's smaller than N, output "The number is larger than that" 4. If the input is larger than N, output "The number is smaller than that" 5. If the input is equal to N, output " You got it!", and exit 6. Repeat until the...
Suppose you ran your elevator simulation program N times with different random number seeds to generate...
Suppose you ran your elevator simulation program N times with different random number seeds to generate N independent measurements of the average travel time X1, X2,⋅⋅⋅, XN a. State the correct formula for the 95% confidence interval for the global average travel time across all N runs assuming that each run of your simulation program was long enough to assume that the average travel times across different runs have an i.i.d. normal distribution. [HINT: Remember to use the t-distribution, because...
Write a program that uses a custom function to generate a specified number of random integers...
Write a program that uses a custom function to generate a specified number of random integers in a specified range. This custom function should take three arguments; the number of integers to generate, the lower limit for the range, and the upper limit for the range. Values for these arguments should be entered by the user in main. The custom function should display the random integers on one line separated by single spaces. The function should also report how many...
create code for deletestudentbyID Number for choice == 4 package courseecom616; import java.util.Scanner; import java.util.ArrayList; public...
create code for deletestudentbyID Number for choice == 4 package courseecom616; import java.util.Scanner; import java.util.ArrayList; public class CourseeCOM616 {        private String courseName;     private String[] studentsName = new String[1];     private String studentId;        private int numberOfStudents;        public CourseeCOM616(String courseName) {         this.courseName = courseName;     }     public String[] getStudentsName() {         return studentsName;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public String getStudentId() {         return studentId;    ...
Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void...
Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void main(String[] args) { Rectangle r1 = new Rectangle(0,0,100,150); System.out.println(r1);    Rectangle r2 = new Rectangle(50,75,100,150); System.out.println(r2);    Rectangle r3 = r1.intersection(r2);    } } Write a program that takes both Rectangle objects, and uses the intersection method to determine if they overlap. If they do overlap, then print it's coordinates along with its width and height. If there is no intersection, then have the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT