Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help me This assignment will give you practice with while loops and pseud

ID: 3759270 • Letter: P

Question

Please help me

This assignment will give you practice with while loops and pseudorandom numbers. You are going to write a program that allows the user to play a simple guessing game in which your program thinks up an integer and allows the user to make guesses until the user gets it right. For each incorrect guess you will tell the user whether the right answer is higher or lower. Your program is required to exactly reproduce the format and behavior of the log of execution at the end of this write-up At a minimum, your program should have the following static methods in addition to method main a method that introduces the game to the user a method to play one game with the user (just one game, not multiple games) a method to report overall results to the user You may define more methods than this if you find it helpful, although you will find that the limitation that methods can return only one value will tend to limit how much you can decompose this problem You are to define a class constant for the maximum number used in the guessing game. The sample log shows the user making guesses from 1 to 100, but the choice of 100 is arbitrary. By introducing a constant for 100, you should be able to change just the value of the constant to make the program play the game with a range of 1 to 50 or a range of 1 to 250 or some other range starting with 1 When you ask the user whether or not to play again, you should use the “next()" method of the Scanner class to read a one-word answer from the user. You should continue playing if this answer begins with the letter "y" or the letter "Y”. Notice that the user is allowed to type words like "yes". You are to look just at the first letter of the user's response and see whether it begins with a "y" or "n" (either capitalized or not) to determine whether to play again. Assume that the user always types an integer when guessing, that the integer is always in an appropriate range and that the user gives you a one-word answer beginning with "y whether to play again. You may assume that no game involves more than 9,999 guesses You will notice at the end of the log that you are to report various statistics about the series of games played by the user. You are to report the total number of games played, the total number of guesses made (all games included), the average number of guesses per game, and the best (fewest) number of guesses used in any single game. The average number of guesses per games should be rounded to one decimal place (you can use either the roundl method or a printf) Because this program uses pseudorandom numbers, you won't be able to recreate the sample log. We will provide sample logs where the answer is always 42. Obviously you won't want your program to always pick 42 as the number to be guessed. You should modify your program to set the answer to 42 and check its behavior against the sample logs. Then you should put it back to the normal behavior of picking a different number for every game before you turn it in Here are a few helpful hints to keep in mind "Y", "n" or "N" when asked To deal with the yes/no response from the user, you will want to use some of the String class methods described in section 3.3 and 4.1 of the book or the lecture slides for the lecture on Strings You should use the next() method of the Scanner class to read a word from the console It's a good idea to change the value of your class constant and run the program to make sure that everything works correctly with the new value of the constant. For example, turn it into a guessing »

Explanation / Answer

import java.util.Random;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        boolean win = false;

        boolean running = true;

        System.out.println("Please enter the upper limit: ");

        Game game = new Game(scan.nextInt());

        while (running) {

            System.out.println("Please enter a guess ");

            while (!win) {

                if (game.guess(scan.nextInt())) {

                    win = true;

                }

            }

            System.out.println("Would you like to play again y/n?");

            String playAgain = scan.next();

            if (playAgain.equalsIgnoreCase("y")) {

                game.reset();

                running = true;

                win = false;

            } else {

                running = false;

                System.out.println("Game over");

            }

        }

    }

}

Game class:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote