Question

IN BASIC JAVA and using JAVA.UTIL.RANDOM write a program will randomly pick a number between one...

IN BASIC JAVA and using JAVA.UTIL.RANDOM write a program will randomly pick a number between one and 100. Then it will ask the user to make a guess as to what the number is. If the guess is incorrect, but is within the range of 1 to 100, the user will be told if it is higher or lower and then asked to guess again. If the guess was outside the valid range or not readable by Scanner class, a message to “try again” will be given. The range of acceptable guesses should change with each new piece of information. Here is a partial output of the program.

Enter 1 to 100: 45

Good guess but your value is too low

Enter 46 to 100: 87

Good guess but your value is too high

Enter 46 to 86 : 25

Value is outside of range

Enter 46 to 86: fifty

Inputs must be numerical

Enter 46 to 86: 57

That is the correct number! It took you 3 valid guesses, 5 in total.

When each guess happens, the hidden answer, this new guess, and the current min and max values will be sent to a static method called high_or_low (4 input parameters). This method will attempt to return an integer value that is the difference from the correct value. This value will help you determine if value guessed is higher(positive), lower(negative), or zero(found) then the correct answer. If the guess was outside the current range allowed (this starts with 1 to 100 but changes with each valid guess) an exception should be thrown from the method. The main method should have a loop and try and catch blocks needed to force the user to reenter for any inputs that are not the correct answer(including bad input or out of range), forcing them to reenter. Continue until the number is finally guessed and then report the total number of valid guesses and total overall guesses. Exceptions must be used and caught and NO BREAK STATEMENTS

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question.

Below is the code you will be needing  Let me know if you have any doubts or if you need anything to change.

Thank You !

===========================================================================

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

public class GuessingGame {

    private static int high_or_low(int hiddenAnswer, int userGuess, int min, int max) {

        if (userGuess == hiddenAnswer) return 0;

        if (userGuess < min || userGuess > max) {
            throw new IllegalArgumentException("Try again");
        }

        return hiddenAnswer - userGuess;
    }


    public static void main(String[] args) {

        Random random = new Random();
        Scanner scanner = new Scanner(System.in);
        int hiddenAnswer = 1 + random.nextInt(100);
        //      System.out.println("hiddenAnswer = " + hiddenAnswer);
        int min = 1;
        int max = 100;
        int difference = 100;
        int userGuess = -1;
        int validGuesses = 0;
        int totalGuesses = 0;
        do {
            System.out.print("Enter " + min + " to " + max + ": ");
            try {
                totalGuesses += 1;
                userGuess = scanner.nextInt();
                difference = high_or_low(hiddenAnswer, userGuess, min, max);
                validGuesses+=1;
                if (difference < 0) {
                    System.out.println("Good guess but your value is too high");
                    max = userGuess;
                } else if (difference > 0) {
                    System.out.println("Good guess but your value is too low");
                    min = userGuess;
                } else {
                    System.out.println("That  is the correct number! It took you "+
                            (validGuesses-1)+" valid guesses, "+totalGuesses+" in total.");
                }
            } catch (InputMismatchException e) {
                System.out.println("Inputs must be numerical");
                scanner.nextLine();
            } catch (IllegalArgumentException e) {
                System.out.println("Try again");
            }

        } while (difference != 0);

    }
}

=========================================================================

let me know if you have any doubts or if you need anything to change.

If you are satisfied with the solution, please rate the answer.

Thanks!


===========================================================================

Add a comment
Know the answer?
Add Answer to:
IN BASIC JAVA and using JAVA.UTIL.RANDOM write a program will randomly pick a number between one...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program to play "guess my number". The program should generate a random number between...

    Write a program to play "guess my number". The program should generate a random number between 0 and 100 and then prompt the user to guess the number. If the user guesses incorrectly the program gives the user a hint using the words 'higher' or 'lower' (as shown in the sample output). It prints a message 'Yes - it is' if the guessed number is same as the random number generated. ANSWER IN C LANGUAGE. ====================== Sample Input / Output:...

  • (c++) Write a program that generates a random number between 1 and 100 and asks the...

    (c++) Write a program that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Be sure that your program...

  • Java code Guessing Game Refinement. (The user thinks of a number and the program guesses it)...

    Java code Guessing Game Refinement. (The user thinks of a number and the program guesses it) Program should be able to guess correctly in 7 tries or lower. Initialize a variable that represents the lowest possible number to 0 (what type should this be?) Initialize a variable that represent the highest possible number to 100 (what type should this be?) Initialize a Boolean variable that represents if we’ve achieved the correct guess to false Initialize a variable in which to...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • Write a JAVA program that plays a number guessing game with the user. A sample run...

    Write a JAVA program that plays a number guessing game with the user. A sample run for the game follows. User input is shown in boldface in the sample run. Welcome to the game of Guess It! I will choose a number between 1 and 100. You will try to guess that number. If your guess wrong, I will tell you if you guessed too high or too low. You have 6 tries to get the number. OK, I am...

  • For this lab you will write a Java program using a loop that will play a...

    For this lab you will write a Java program using a loop that will play a simple Guess The Number game. Th gener e program will randomly loop where it prompt the user for a ate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a r has guessed the correct number, the program will end with a message indicating how many guesses it took to get the right answer and a...

  • Solve Question using While loops-MATLAB Write a program that generates a random number and asks the...

    Solve Question using While loops-MATLAB Write a program that generates a random number and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." The program should use a loop that repeats until the user correctly guesses the random number.

  • 7.30 - Guess-the-Number Game (Project Name: GuessTheNumber) - Write an app that plays “Guess the Number”...

    7.30 - Guess-the-Number Game (Project Name: GuessTheNumber) - Write an app that plays “Guess the Number” as follows: Your app chooses the number to be guessed by selecting a random integer in the range 1 to 1000. The app displays the prompt "Guess a number between 1 and 1000: ". The player inputs a first guess. If the player’s guess is incorrect, your app should display Too high. Try again. or Too low. Try again. to help the player “zero...

  • Hello! we are using Python to write this program. we are supposed to use loops in...

    Hello! we are using Python to write this program. we are supposed to use loops in this assignment. I would greatly appreciate the help! Thank you! Write a program that allows the user to play a guessing game. The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: we would have the program select a random number as the "secret number". However, for the purpose of testing...

  • Please write a C# program that where a player will play a guessing game with the...

    Please write a C# program that where a player will play a guessing game with the range of 1-100. Each time the play guesses a number, and it is not correct the program should indicate if the number is more or less than what the player guessed. The play should be able to guess until the correct number has been guessed. If an invalid number is entered, such as: 1.24 (real number), or asd (string). The program should tell user...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT