patternjavaMinor
Guessing game - Is it a 40?
Viewed 0 times
gameguessingstackoverflow
Problem
User has number in head, the program must guess the number.
```
public class GuessingGame extends JFrame{
JButton newGameButton, highButton, lowButton, correctButton, exitButton;
JLabel initialTextLabel, enterLabel;
JTextArea commentTextArea;
JScrollPane scroll;
private int guess, high = 101, low = 0, tries = 1;
public guessingGame()
{
title("Guessing Game");
newGameButton = new JButton("Start Game");
exitButton = new JButton("Exit Game");
highButton = new JButton("Too High");
lowButton = new JButton("Too Low");
correctButton = new JButton("Correct!");
commentTextArea = new JTextArea(null,10,30);
scroll = new JScrollPane(commentTextArea);
commentTextArea.setEditable(false);
initialTextLabel = new JLabel("Think of a number between 0 & 100 can the computer guess it");
enterLabel = new JLabel("Is your number: ");
//add components
highButton.setVisible(false);
lowButton.setVisible(false);
correctButton.setVisible(false);
//set default jframe size
//create and register the button event handlers
}//end of GuessGame constructor
//highButtonHandler class
class highButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
high = guess;
guess = low + (guess - low) / 2;
tries++;
commentTextArea.append("Is the number " + guess + " too small, too high or correct.\n");
}
}//end of high class
//lowButtonHandler class
class lowButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
low = guess;
guess = guess + (high - guess) / 2;
tries++;
commentTextArea.append("Is the number " + guess + " too small, too high or correct.\n");
}
```
public class GuessingGame extends JFrame{
JButton newGameButton, highButton, lowButton, correctButton, exitButton;
JLabel initialTextLabel, enterLabel;
JTextArea commentTextArea;
JScrollPane scroll;
private int guess, high = 101, low = 0, tries = 1;
public guessingGame()
{
title("Guessing Game");
newGameButton = new JButton("Start Game");
exitButton = new JButton("Exit Game");
highButton = new JButton("Too High");
lowButton = new JButton("Too Low");
correctButton = new JButton("Correct!");
commentTextArea = new JTextArea(null,10,30);
scroll = new JScrollPane(commentTextArea);
commentTextArea.setEditable(false);
initialTextLabel = new JLabel("Think of a number between 0 & 100 can the computer guess it");
enterLabel = new JLabel("Is your number: ");
//add components
highButton.setVisible(false);
lowButton.setVisible(false);
correctButton.setVisible(false);
//set default jframe size
//create and register the button event handlers
}//end of GuessGame constructor
//highButtonHandler class
class highButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
high = guess;
guess = low + (guess - low) / 2;
tries++;
commentTextArea.append("Is the number " + guess + " too small, too high or correct.\n");
}
}//end of high class
//lowButtonHandler class
class lowButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
low = guess;
guess = guess + (high - guess) / 2;
tries++;
commentTextArea.append("Is the number " + guess + " too small, too high or correct.\n");
}
Solution
Pros
cons
- dedicated classes for the ActionListener
- calls
JFramw.setVisible()from outside any constructor
cons
- Naming conventions: class names always start with upper case letter
- Inheritance: never extend a class unless you want to change behavior, you only configure JFrame, so no need to extend it.
- comments: comments in code should explain why the code is like it is and only if it is somewhat unusual or unexpected. Your comments only repeat what the code expresses already.
- System.exit(0);: This kills the JVM. There will be no change to i.e. close database connections correctly. Use it only in a catch block inside
main.
Context
StackExchange Code Review Q#146257, answer score: 3
Revisions (0)
No revisions yet.