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

Chapter 5 Programming Exercise Random Number Guessing Game Create an application

ID: 3703591 • Letter: C

Question

Chapter 5 Programming Exercise Random Number Guessing Game

Create an application that generates a random number in the range of 1 through 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number the program should display “Too high, try again.” If the user’s guess is lower than the random number the program should display “Too low, try again.” If the user guesses the number, the application should congratulate the user.

The game should keep count of the number of guesses and display the results of the guess in a list box, as shown in the example below. The “get random number” button should reset the number & set the count back to 0.

You need to “hold on” to the value for the number of guesses and the random number for longer than one button click, so be careful of that. You need to count how many guesses it takes the user to be successful. Please place a “cheat box” by displaying the random number. This helps you when you’re testing your program, to be sure your code is working properly.

Needs to be done in Visual Studio 2017.

Need the code for the program.

Form must have Guess Button, Exit Button, Get Random Number Button, an area for the user to enter the number they are guessing, and a "CheatBox" area that gets the number they entered and displays it for the programmer to ensure the program is functioning property, as well as a ListBox and a few other items.

Explanation / Answer

Visual Studio is an integrated development environment its is not a programming language. As you have not mentioned in which programming language do in need to write the code i am considering Java Programming language

Source Code

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class GuessGame extends JFrame {

private static final long serialVersionUID = 1L;
public static Object prmpt1;
private JTextField userIn;
private JLabel cmnt = new JLabel(" ");
private JLabel cmnt2 = new JLabel(" ");
private int randNum;
private int count = 0;

public GuessGame() {
super("Guessing Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(new FlowLayout());
Container c = getContentPane();

JButton guessBtn = new JButton("Try the number");
JButton newGameBtn = new JButton("New Game");
JButton quitButton = new JButton("Quit");
JLabel prmpt1 = new JLabel("I have a number between 1 and 1000.");
JLabel prompt2 = new JLabel("Can you guess the number?");
JLabel prompt3 = new JLabel("Please enter your guess: ");
cmnt = new JLabel(" ");
cmnt2 = new JLabel(" ");
userIn = new JTextField(5);

c.add(prmpt1);
c.add(prompt2);
c.add(prompt3);
c.add(userIn);
c.add(guessBtn);
c.add(newGameBtn);
c.add(quitButton);
c.add(cmnt);
c.add(cmnt2);

guessBtn.setMnemonic('T');
newGameBtn.setMnemonic('N');
quitButton.setMnemonic('Q');

setSize(300, 200);
setLocationRelativeTo(null);
setVisible(true);
setResizable(false);

initializeNumber();

GuessButtonHandler ghandler = new GuessButtonHandler(); // instantiate
guessBtn.addActionListener(ghandler); // add event listener

NewButtonHandler nhandler = new NewButtonHandler();
newGameBtn.addActionListener(nhandler);

QuitButtonHandler qhandler = new QuitButtonHandler();
quitButton.addActionListener(qhandler);
}
private void initializeNumber() {
randNum = new Random().nextInt(1000) + 1;
}

private class GuessButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {

int getUserInput;
int diff;
int Difference;

try {
getUserInput = Integer.parseInt(userIn.getText().trim());
count++;

if (getUserInput == randNum) {
JOptionPane.showMessageDialog(null, "Correct! It took you "
+ count + " guesses", "Random Number: "
+ randNum, JOptionPane.INFORMATION_MESSAGE);
initializeNumber();
return;
}
if (getUserInput > randNum) {
cmnt2.setText("The guess was too HIGH. Try a lower number.");
cmnt2.setForeground(Color.WHITE);
diff = getUserInput - randNum;
Difference = Math.abs(diff);
} else {
cmnt2.setText("The guess was too LOW. Try a higher number.");
cmnt2.setForeground(Color.WHITE);
diff = randNum - getUserInput;
Difference = Math.abs(diff);
}

if (Difference >= 30) {
cmnt.setText("You are Cold. ");
cmnt.setForeground(Color.WHITE);
GuessGame.this.setBackgroundColor(Color.BLUE);
}

if (Difference <= 15) {
cmnt.setText("You are getting Warm");
cmnt.setForeground(Color.WHITE);
GuessGame.this.setBackgroundColor(Color.RED);
}
} catch (NumberFormatException ex) {
cmnt.setText("Enter a VALID number!");
}
}
}
private class NewButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
GuessGame app = new GuessGame();

}
}

private class QuitButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}

private void setBackgroundColor(Color RED) {
getContentPane().setBackground(RED);
}

public static void main(String args[]) {
GuessGame app = new GuessGame();

}
}

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