s23 Assignment 2 Creating a Guessing Game with Betting GUI : DUE August 8

Your deliverable work on this:
Add a field to show a running tally of how much money won/lost.
Add a field to show how many rounds they have played.

Keep track of your code for this: Hand it in at the end.
Assignment is due: August 8
Next week we will see how to add the additional features you must provide:
Give user 50% chance of winning.
Iterate the game to keep track of user’s amount of money won or lost for 10 rounds.

Put your code into a GitHUB repository.
This will count as your assignment 2:
To submit your GITHUB REPO URL: Make a text file: Save this file as your StudentName_StudentID.txt
Copy/paste your GitHUB URL into this file:
Upload this GITHUB Repository URL to the Dropbox Location:

In this Java lab, you will learn how to create a simple Java project with a graphical user interface (GUI) to play a guessing game with betting. The user will be prompted to place a bet, guess a random number, and find out if they won or lost the bet. The game will allow the player to play again if they wish to continue.
Basic understanding of Java programming concepts.
Familiarity with object-oriented programming.
Tools Used:
JDK (Java Development Kit) for compiling and running Java code.
An Integrated Development Environment (IDE) like Eclipse or IntelliJ for ease of development.

Step 1: Setting Up the Project

Open your IDE and create a new Java project named "GuessingGameGUI."

Step 2: Designing the GUI

We'll be using the Swing framework to create the GUI for our guessing game.
Create a new class called "GuessingGameGUI" that extends javax.swing.JFrame.
In the GuessingGameGUI class, create the following components:
JLabel to display instructions and messages to the player.
JTextField to allow the player to enter their bet and guess.
JButton to initiate the game and check the result.
JTextArea to display the game's outcome.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class GuessingGameGUI extends JFrame {
private JLabel betLabel;
private JTextField betField;
private JLabel guessLabel;
private JTextField guessField;
private JButton playButton;
private JTextArea resultArea;
private int randomNumber;
private int playerBet;

public GuessingGameGUI() {
// Set up the JFrame properties
setTitle("Guessing Game with Betting");
setSize(300, 200);
setLayout(new GridLayout(4, 2));

// Initialize components
betLabel = new JLabel("Enter your bet:");
betField = new JTextField();
guessLabel = new JLabel("Enter your guess (1-10):");
guessField = new JTextField();
playButton = new JButton("Play");
resultArea = new JTextArea();

// Add components to the JFrame

// Add ActionListener to the playButton
playButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

private void playGame() {
// Get the player's bet and guess from the text fields
try {
playerBet = Integer.parseInt(betField.getText());
int playerGuess = Integer.parseInt(guessField.getText());

// Generate a random number between 1 and 10
randomNumber = new Random().nextInt(10) + 1;

// Determine if the player won or lost the bet
if (playerGuess == randomNumber) {
resultArea.setText("Congratulations! You guessed correctly!\nYou won " + playerBet + " coins.");
} else {
resultArea.setText("Sorry, the correct number was " + randomNumber + ".\nYou lost " + playerBet + " coins.");
} catch (NumberFormatException ex) {
resultArea.setText("Please enter valid numbers for bet and guess.");

public static void main(String[] args) {
// Create and display the GUI
SwingUtilities.invokeLater(() -> {
GuessingGameGUI gui = new GuessingGameGUI();

Step 3: Compile and Run the Program

Save the file and compile the program in your IDE.
Run the program to see the GUI window.

Step 4: Test and Play the Game

Enter a bet in the "Enter your bet" field and a number between 1 and 10 in the "Enter your guess" field.
Click the "Play" button to see the result of your guess and if you won or lost the bet.
The game outcome will be displayed in the text area.
You can continue playing the game by entering new bets and guesses.


Congratulations! You have successfully created a Java project with a GUI to play a guessing game with betting. You've learned how to use Swing components to build the user interface and interact with the player.
Remember to further improve the game, you can add features such as tracking the player's total coins, allowing them to play multiple rounds, adding sound effects, or creating a leaderboard. Enjoy experimenting and enhancing your Java skills!

The code provided will work to provide the basic functionality of a guessing game with betting and a simple GUI. However, there are a few potential improvements and considerations to make the code more robust and user-friendly:
Input Validation: Currently, the code assumes that the user will always enter valid integer values for the bet and guess. It's essential to add input validation to handle cases where the user enters non-numeric values or values outside the valid range.
Feedback for Invalid Input: When the user enters invalid input for the bet and guess, the code should provide clear feedback about the error rather than just displaying a generic message.
Clearing Fields: After each game, it's a good idea to clear the text fields (betField and guessField) to make it easier for the user to enter new values.
Error Handling for Exception: The code should handle exceptions more gracefully, especially when parsing integers from text fields. It should display appropriate error messages instead of relying on the generic message for NumberFormatException.
Edge Cases: The code does not consider edge cases where the user enters a negative bet or a guess outside the range of 1 to 10.
Player's Coin Balance: If you plan to allow the player to have a starting balance of coins and track their balance as they play, you'll need to add this functionality.
Play Again Option: The current code doesn't provide an option for the player to choose whether they want to play again or not. You can add a "Play Again" button or prompt the user to play again after each game.
GUI Layout: The current GridLayout used may not be the best layout choice for this application. Consider using other layouts like BorderLayout or GroupLayout, depending on the specific GUI design you want.
Handling Multiple Players: If you want to make the game multiplayer or allow multiple players to place bets and guess, the code will need further modifications.
It's important to consider these points to enhance the user experience and make the application more robust. The provided code is a starting point, and you can build upon it to create a more comprehensive and polished guessing game with betting GUI.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
) instead.