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

Java Programming : Problem 2 (name this Lab10_Problem2) This is a guessing-game:

ID: 3763517 • Letter: J

Question

Java Programming :

Problem 2 (name this Lab10_Problem2)

This is a guessing-game: you (the app user) against the computer. You will only play one "go" for this problem. In other words, the program will not continue to play—you have one shot at it. Here's how it should work:

The human user types in an integer value, 1 to 21

The computer tries to guess the number

If the computer guesses correctly, the computer wins; if it guesses incorrectly, the human wins

Design of main():

With the exception of variable declarations, the majority of the code in main() should be contained in a captive loop validation structure since the program relies on end-user input. End-user input should always be validated. See your notes. Continue to nag the user until he or she enters an integer value between 1 and 21.

Within your captive loop structure, prompt the human for a number between 1 and 21, then store it to an input-capture variable. Use a method named fiGetNum() to do this and return the integer value to the main().

Evaluate the input returned by fiGetNum(). There are two possible outcomes:

The input value is not okay—it's 0 or less or 22 or more; nag and try again by ensuring the captive loop will continue.

The input value is okay—it's between 1 and 21; if so, proceed with have the computer guess by calling a method fiGuess()

If the input value is okay, then call a method fiGuess() to generate a random number within the designated range. Pass it the minimum acceptable integer value (1) and the maximum (21) as numeric literals. Build the method so that it receives these parameters and uses the Random class to generate the random number within the range.

Compare the number returned by fiGuess() with the number returned by fiGetNum(). If the values match, the computer wins—it guessed the number. If not, the human wins—the computer didn't guess it right.

Display the results in main(). Here are three scenarios your program should handle:

User input is out of range; display an error and force the user to try again:

Enter a number from 1 to 21: 35

Error: must be from 1 to 21, inclusive

User input is within range; computer wins; results displayed and program stops:

Enter a number from 1 to 21: 13
Computer guessed 13
Computer won. It guessed your number.

User input is within range; human wins; results displayed and program stops:

Enter a number from 1 to 21: 13
Computer guessed 7
Human won! The computer guessed wrong.

Explanation / Answer

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

public class GuessGame {

   public static void main(String args[]) {

       int input_capture = fiGetNum();
       if (input_capture >= 1 && input_capture <= 21) {
           int guessNo = fiGuess();
           System.out.println("Computer guessed " + guessNo);
           if (guessNo == input_capture) {
               System.out.println("Computer won. It guessed your number.");
           } else {
               System.out.println("Human won! The computer guessed wrong.");
           }

       } else {
           System.out.println("Error: must be from 1 to 21, inclusive");
       }
       System.out
               .println("User input is within range; computer wins; results displayed and program stops:");

   }

   public static int fiGuess() {
       int min = 1, max = 21;
       Random rand = new Random();
       int randomNum = rand.nextInt((max - min) + 1) + min;
       return randomNum;

   }

   public static int fiGetNum() {
       Scanner scanner = new Scanner(System.in);
       System.out.print("Enter a number from 1 to 21:");
       int input_capture = scanner.nextInt();
       return input_capture;

   }
}

OUTPUT:

Enter a number from 1 to 21:35
Error: must be from 1 to 21, inclusive
User input is within range; computer wins; results displayed and program stops:

Enter a number from 1 to 21:4
Computer guessed 4
Computer won. It guessed your number.
User input is within range; computer wins; results displayed and program stops:

Enter a number from 1 to 21:15
Computer guessed 2
Human won! The computer guessed wrong.
User input is within range; computer wins; results displayed and program stops:

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