Littletown Café Lunch Dinner Guest Counts By Date Month ✓ Solved

Sheet1littletown Cafélunch Dinner Guest Counts By Datemonth

Write a program that generates a random number to be guessed by the user. The program should provide feedback on whether each guess is smaller or larger than the random number, and keep track of the number of guesses made, as well as how many were smaller or larger than the random number. Include both a console version and a GUI version using JOptionPane.

Paper For Above Instructions

Introduction

Developing a guessing game can serve as a practical exercise in understanding fundamental programming concepts such as loops, conditional statements, and user input handling. This paper addresses the creation of a program that generates a random number and prompts the user to guess it. The program provides feedback on each guess and tracks statistics regarding the guesses. The implementation includes a console version and a GUI version using Java's JOptionPane for user interaction.

Console Version Implementation

The console version of the guessing game is a straightforward implementation that utilizes Java's built-in libraries. The primary components include classes from the java.util package, specifically Random for generating random numbers and Scanner for capturing user inputs. Below is a detailed explanation of the critical components of the console version.


package project2;

import java.util.Random;

import java.util.Scanner;

public class Guesses {

public static void main(String[] strg) {

Random rand = new Random();

int value = rand.nextInt(32) + 1;

System.out.println("run: ");

System.out.println("Guess the number I'm thinking of, from 1-32: ");

Scanner sc = new Scanner(System.in);

int guess = sc.nextInt();

int numberguesses = 0;

int smaller = 0;

int larger = 0;

while (guess != value) {

if (guess

if (guess == -1) {

System.out.println("You gave up");

System.exit(0);

} else {

smaller++;

numberguesses++;

System.out.println("Your guess is smaller than the random value. Next guess: ");

guess = sc.nextInt();

}

} else {

larger++;

numberguesses++;

System.out.println("Your guess is larger than the random value. Next guess:");

guess = sc.nextInt();

}

}

if (guess == value) {

numberguesses++;

System.out.println("You've guessed correct!");

System.out.println("Total number of guesses: " + numberguesses);

for (int i = 0; i

System.out.printf("*");

}

System.out.println();

System.out.println("Smaller Guesses: " + smaller);

for (int i = 0; i

System.out.printf("*");

}

System.out.println();

System.out.println("Larger Guesses: " + larger);

for (int i = 0; i

System.out.printf("*");

}

System.out.println();

}

}

}

GUI Version Implementation

The GUI version enhances user engagement by employing Java's JOptionPane for graphical user interfaces. This version is almost identical in logic to the console version but replaces console inputs and outputs with dialog boxes. The randomness and guessing mechanics remain unchanged. The following is an outline of the GUI version's code.


package project2;

import java.util.Random;

import javax.swing.JOptionPane;

public class GuessesJOptionPane {

public static void main(String args[]) {

int guess = Integer.parseInt(

JOptionPane.showInputDialog(null, "Guess the number I'm thinking of, from 1-32: "));

Random rand = new Random();

int value = rand.nextInt(32) + 1;

int numberguesses = 0;

int smaller = 0;

int larger = 0;

while (guess != value) {

if (guess

if (guess == -1) {

JOptionPane.showMessageDialog(null, "You Gave Up", "You gave up", JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

} else {

smaller++;

numberguesses++;

JOptionPane.showMessageDialog(null, "Your guess is smaller than the random value. Next guess: ");

guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Guess the number I'm thinking of, from 1-32: "));

}

} else {

larger++;

numberguesses++;

JOptionPane.showMessageDialog(null, "Your guess is larger than the random value. Next guess: ");

guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Guess the number I'm thinking of, from 1-32: "));

}

}

if (guess == value) {

numberguesses++;

String message = "You've guessed correct!\nTotal number of guesses: " + numberguesses;

JOptionPane.showMessageDialog(null, message);

}

}

}

Conclusion

The implementation of both the console and GUI versions of the guessing game demonstrates the utility of user input handling and random number generation in programming. The feedback provided to the user is instrumental in guiding them towards the correct answer while also tracking their performance. Such projects serve not only as educational exercises but also as the foundation for more complex applications that could involve additional features such as scoreboards and difficulty levels.

References