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

This assignment will give you practice with while loops and pseudorandom numbers

ID: 3528487 • Letter: T

Question

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, so you may want to look that over first.


At a minimum, your program should have the following static methods in addition to method main:


? a method to give instructions 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

Explanation / Answer

Please rate - thanks

I'm depending on you



import java.util.*;

public class Guess

{public static final int RANGE=100;

public static void main(String[] args)

{int guesses,games=0,maxguesses=-1,total=0;

Random r=new Random();

Scanner in=new Scanner(System.in);

char choice;

instructions();

do

{

guesses=playGame(r,in);

games++;

total+=guesses;

if(guesses>maxguesses)

maxguesses=guesses;

System.out.print("another game(y/n)? ");

choice=Character.toUpperCase(in.next().charAt(0));

}while(choice=='Y');

report(total,maxguesses,games);

}

public static void report(int total,int maxguesses,int games)

{System.out.println("Games played: "+games);

System.out.println("Total guesses made: "+total);

System.out.println("Average guesses made per game: "+((double)total/games));

System.out.println("Maximum guesses made in a game: "+maxguesses);


}

public static void instructions()

{System.out.println("This program allows you to play a simple guessing game");

System.out.println("I will think up an integer between 1 and "+RANGE);

System.out.println(" and allow you to make guesses until you get it right.");

}

public static int playGame(Random r,Scanner in)

{int num=r.nextInt(RANGE)+1;

int guess=0,n;

do

{System.out.print("Enter guess: ");

n=in.nextInt();

guess++;

if(num>n)

System.out.println("My number is higher ");

else if(num<n)

System.out.println("My number is lower ");

}while(n!=num);

System.out.println("You got it--That's the number ");

return guess;

}

}


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