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.
Prerequisites:
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;
publicclassGuessingGameGUIextendsJFrame {
private JLabel betLabel;
private JTextField betField;
private JLabel guessLabel;
private JTextField guessField;
private JButton playButton;
private JTextArea resultArea;
private int randomNumber;
private int playerBet;
publicGuessingGameGUI() {
// Set up the JFrame properties
setTitle("Guessing Game with Betting");
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(newGridLayout(4, 2));
// Initialize components
betLabel = newJLabel("Enter your bet:");
betField = newJTextField();
guessLabel = newJLabel("Enter your guess (1-10):");
// 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 = newRandom().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.");
}
}
publicstaticvoidmain(String[] args) {
// Create and display the GUI
SwingUtilities.invokeLater(() -> {
GuessingGameGUI gui = newGuessingGameGUI();
gui.setVisible(true);
});
}
}
Step 3: Compile and Run the Program
Save the GuessingGameGUI.java 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.
Conclusion:
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 (