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

I need help with figuring out how to modify because I am very confused. code for

ID: 3794348 • Letter: I

Question

I need help with figuring out how to modify because I am very confused.

code for a lab in Java:

// import a class for random numbers
import java.util.Random;

public class GuessGame
{
   // method to generate a random number
   public static int RandomNumber()
   {
       // local variable to hold a random number
       int rNum = 0;
       // a random object is created
       Random rand = new Random();
       // a random number is generated
       rNum = rand.nextInt(20) + 1;
       // random number is returned to main()
       return rNum;
   }

   // the main() method
   public static void main(String args[])
   {
       // local variable to hold a random integer   
       int x = 0;
       // local variable is assigned a random number
       x = RandomNumber();
       //display the random number
       System.out.println(x);
   }
}

I have to define a viod return type method named guessingGame() and place it outside the main() method but within the class definition and this method will be used to call RandomNumber() method from the program.

In the guessingGame() method, I have to declare local variables: int x=0,guess=0,response=1;

Open a dowhile() looping structure that executes its loop body as long as the variable response equals 1 .

                                   

                        •           Assign local variable x to the call of the RandomNumber() method.

                       

                        •           Display this message to the user.

                "Welcome: the guessing game. I have a number "

                + "from 1 " +"and 20."+"Can you guess it?"+

                "Please type your first guess."

                       

                        •           Input the users guess .

Any help would be greatly appreciated. Please and Thank you.

Explanation / Answer

//GuessGame.java
//import a class for random numbers
import java.util.Random;
import java.util.Scanner;
public class GuessGame
{
   // main() method
   public static void main(String args[])
   {      
       //calling guessingGame
       guessingGame();
   }

   /*
   * Method guessingGame that prompts user to enter a guess
   * number in a range of 1 to 20. and continues to prompt
   * until user guess is correct.
   * */
   public static void guessingGame()
   {
       //create a scanner class object
       Scanner scanner=new Scanner(System.in);
       int x=0,guess=0;
       // local variable is assigned a random number
       //calling RandomNumber method
       x = RandomNumber();
       System.out.println("Welcome: the guessing game. I have a number "
                + "from 1 " +"and 20."+"Can you guess it?"+
                "Please type your first guess.");
       do
       {      
           //read user guess
           guess=scanner.nextInt();
           //Checking if x is not guess then
           //print invalid guess
           if(x!=guess)
               System.out.println("Invalid guess. Please re-enter guess.");
          
       }while(x!=guess);

       //checking if x is guess
       if(x==guess)
           System.out.println("Congratulations!.Your guess is correct");
      
   }

   // Method to generate a random number
   public static int RandomNumber()
   {
       // local variable to hold a random number
       int rNum = 0;
       // a random object is created
       Random rand = new Random();
       // a random number is generated
       rNum = rand.nextInt(20) + 1;
       // random number is returned to main()
       return rNum;
   }
}

Sample Output:


Welcome: the guessing game.
I have a number from 1 and 20.Can you guess it?Please type your first guess.
5
Invalid guess.
Please re-enter guess.
15
Invalid guess.
Please re-enter guess.
3
Congratulations!.Your guess is correct

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