Question

In: Computer Science

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

For this assignment you will write a Java program using a loop that will play a simple Guess The Number game. Create a new project named GuessANumber and create a new Java class in that project named GuessANumber.java for this assignment.

The program will randomly generate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a loop where it will prompt the user for a guess. If the user 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 message that is determined by how many guesses it takes them to get the right answer. If the user guesses incorrectly, the program should respond with a message that the user has guessed either "too high" or "too low" and let them guess again.

Use the following table to determine the final message after the score is computed (note that your code MUST respond with these messages to be considered correct):

Number of Guesses     Message
-----------------     ---------------------------------------------
1                     That was impossible!
2-3                   You're pretty lucky!
4-7                   Not bad, not bad...
8                     That was not very impressive.
9-10                  Are you having any fun at all?
11 or more            Maybe you should play something else.

Sample Output This is a sample transcript of what your program should do. Items in bold are user input and should not be put on the screen by your program.

Enter a random seed: 99
Enter a guess between 1 and 200: 100
Your guess was too low - try again.

Enter a guess between 1 and 200: 150
Your guess was too low - try again.

Enter a guess between 1 and 200: 175
Your guess was too low - try again.

Enter a guess between 1 and 200: 187
Your guess was too low - try again.

Enter a guess between 1 and 200: 193
Your guess was too high - try again.

Enter a guess between 1 and 200: 190
Your guess was too high - try again.

Enter a guess between 1 and 200: 188
Congratulations!  Your guess was correct!

I had chosen 188 as the target number.
You guessed it in 7 tries.
Not bad, not bad...

Your code will behave differently based on the random value it selects and the choice taken by the user. Here is a second possible execution of this code:

Enter a random seed: 1024
Enter a guess between 1 and 200: 120
Your guess was too low - try again.

Enter a guess between 1 and 200: 130
Your guess was too high - try again.

Enter a guess between 1 and 200: 124
Congratulations!  Your guess was correct!

I had chosen 124 as the target number.
You guessed it in 3 tries.
You're pretty lucky!

If the user enters an invalid choice, your code should inform them that their choice was invalid and ask them to guess again.

Enter a random seed: 99
Enter a guess between 1 and 200: 259
Your guess is out of range.  Pick a number between 1 and 200.
Your guess was too high - try again.

Enter a guess between 1 and 200: -1
Your guess is out of range.  Pick a number between 1 and 200.
Your guess was too low - try again.

Enter a guess between 1 and 200: 188
Congratulations!  Your guess was correct!

I had chosen 188 as the target number.
You guessed it in 3 tries.
You're pretty lucky!

(Note that since the user picked the same random number seed as the first example, the program has selected the same value as the target number.)

Random numbers: Just like in the FunWithBranching programming assignment, you must use the Random class to generate numbers between 1 and 200. Make sure you are using int values and not doubles for this assignment!

Could you help me with how to set up the out of range part. Thanks

Solutions

Expert Solution

Please find the answer below, all the details are mentioned in the comments.

GuessANumber.java

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

//class to GuessANumber
public class GuessANumber {
    public static void main(String[] args) {
        //scanner to read user value
        Scanner sc = new Scanner(System.in);

        //get the seed value from the user
        int seed;
        System.out.print("Enter a random seed: ");
        seed = Integer.parseInt(sc.nextLine());

        //create a Random object
        Random random = new Random(seed);
        //generate the secretNumber
        int secretNumber = random.nextInt(200) + 1;

        //create the counter
        int counter = 0;

        //set default guess value to the false
        boolean isGuessed = false;

        //loop till the isGuessed is not true
        while (!isGuessed) {

            //get the user input for the guess
            System.out.print("Enter a guess between 1 and 200: ");
            int guessNumber = Integer.parseInt(sc.nextLine());
            counter++;

            //check the range of the guess number
            if (guessNumber < 1 || guessNumber > 200) {
                System.out.println("Your guess is out of range.  Pick a number between 1 and 200.");
            }

            //check the guess number is correct, bigger or lower
            if (guessNumber > secretNumber) {
                System.out.println("Your guess was too high - try again.");
                System.out.println();
            } else if (guessNumber < secretNumber) {
                System.out.println("Your guess was too low - try again.");
                System.out.println();
            } else {
                System.out.println("Congratulations!  Your guess was correct!");
                System.out.println();
                isGuessed = true;
            }
        }

        System.out.println("I had chosen " + secretNumber + " as the target number.");
        System.out.println("You guessed it in " + counter +" tries.");
        //print the message based on the counter value
        if (counter == 1) {
            System.out.println("That was impossible!");
        } else if (counter == 2 || counter == 3) {
            System.out.println("You're pretty lucky!");
        } else if (counter >= 4 && counter <= 7) {
            System.out.println("Not bad, not bad...");
        } else if (counter == 8) {
            System.out.println("That was not very impressive.");
        } else if (counter == 9 || counter == 10) {
            System.out.println("Are you having any fun at all?");
        } else if (counter >= 11) {
            System.out.println(" Maybe you should play something else.");
        }
    }
}

Output:

Please let us know in the comments if you face any problems.


Related Solutions

Write a program that produces the following output using nested for loop in Java. ****** ////////////...
Write a program that produces the following output using nested for loop in Java. ****** //////////// ****** ***** //////////\\ ***** **** ////////\\\\ **** *** //////\\\\\\ *** ** ////\\\\\\\\ ** * //\\\\\\\\\\ * \\\\\\\\\\\\
Loop Introduction Assignment Please write a program in c# Using the conditions below, write one program...
Loop Introduction Assignment Please write a program in c# Using the conditions below, write one program that calculates a person’s BMI. Your main() function will call functions 1, 2, and 3. Your program will contain three functions: Function #1: Will ask the user for their weight in pounds and their height in inches.   Your function will convert the weight and height into Body Mass Index (BMI). The formula for converting weight into BMI is as follows: BMI = Weight *...
Using a while loop. Write a JAVA program that asks a user for an integer between...
Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms. Sample output: How many terms would like printed out for the Fibonacci Sequence? 7 Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
Write a Java program using using WHILE loop to find the sum of all integers between...
Write a Java program using using WHILE loop to find the sum of all integers between 200 to 250 which are divisible by 7. Sample Output: Numbers between 200 and 250, divisible by 7: 203 210 217 224 231 238 245 The sum is: 1568
Write a Java program using jGRASP directions are as follows: Uses a while loop to print...
Write a Java program using jGRASP directions are as follows: Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop to print the numbers from 42 - 56. Uses a for loop to print the numbers from 87 - 95. Asks the user for 2 numbers. Uses a loop to print all numbers between the given numbers, inclusive. Note: Consider that your user's second number can be lower! (see example below) Note: Also consider...
Write a program in java that deliberately contains an endless or infinite while loop. The loop...
Write a program in java that deliberately contains an endless or infinite while loop. The loop should generate multiplication questions with single-digit random integers. Users can answer the questions and get immediate feedback. After each question, the user should be able to stop the questions and get an overall result. See Example Output. Example Output What is 7 * 6 ? 42 Correct. Nice work! Want more questions y or n ? y What is 8 * 5 ? 40...
Q1:Write a Java program to find Fibonacci Series using 1) using for loop 2) using while...
Q1:Write a Java program to find Fibonacci Series using 1) using for loop 2) using while loop To Understand what the Fibonacci series is: The Fibonacci sequence is a series of numbers where a number is the sum of the previous two numbers. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. Q2: Write a Java program to find the Factorial of a number using a while loop. To...
Using Java, The program you will be writing displays a weekly payroll report. A loop in...
Using Java, The program you will be writing displays a weekly payroll report. A loop in the program should ask the user for the employee’s number, employee’s last name, number of hours worked, hourly pay rate, state tax, and federal tax rate. After the data is entered and the user hits the enter key, the program will calculate gross and net pay then displays employee’s payroll information as follows and asks for the next employees’ information. if the user works...
JAVA CODE, USE FOR LOOP PLEASE Using the PurchaseDemo program and output as your guide, write...
JAVA CODE, USE FOR LOOP PLEASE Using the PurchaseDemo program and output as your guide, write a program that uses the Purchase class to set the following prices, and buy the number of items as indicated. Calculate the subtotals and total bill called total. Using the writeOutput() method display the subtotals as they are generated as in the PurchaseDemo program. Then print the total bill at the end Use the readInput() method for the following input data Oranges: 10 for...
Java Program Use for loop 1.) Write a program to display the multiplication table of a...
Java Program Use for loop 1.) Write a program to display the multiplication table of a given integer. Multiplier and number of terms (multiplicand) must be user's input. Sample output: Enter the Multiplier: 5 Enter the number of terms: 3 5x0=0 5x1=5 5x2=10 5x3=15 2 Create a program that will allow the user to input an integer and display the sum of squares from 1 to n. Example, the sum of squares for 10 is as follows: (do not use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT