package GuessGame; import java.awt.EventQueue; import javax.swing.JFrame; import
ID: 3560261 • Letter: P
Question
package GuessGame;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class GuessGame extends JFrame
{
public static void main(String[] args) {
GuessGame a = new GuessGame();
a.setSize(500,400);
a.setVisible(true);
a.setDefaultCloseOperation(EXIT_ON_CLOSE);
} // end main
} // end class GuessGame
package GuessGame;
//GuessGameFrame.java
//Guess the number
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
public class GuessGameFrame extends GuessGame
{
private static Random generator = new Random();
private int number = 0; // number chosen by application
private int guessCount; // number of guesses
private int lastDistance; // distance between last guess and number
private JTextField guessInputJTextField; // for guessing
private JLabel prompt1JLabel; // first prompt to user
private JLabel prompt2JLabel ; // second prompt to user
private JLabel messageJLabel ; // displays message of game status
private JButton newGameJButton; // creates new game
private Color background; // background color of application
//set up GUI and initialize values
/***********************************************************************
Write code (25 pts.) to:
1. call the superclass constructor and sets the title
of this application to "Guessing Game"
2. initialize number of guesses to 0
3. set background to light gray
***********************************************************************/
prompt1JLabel = new JLabel(
"I have a number between 1 and 1000." ); // describe game
prompt2JLabel = new JLabel(
"Can you guess my number? Enter your Guess:" ); // prompt user
guessInputJTextField = new JTextField(5); // to enter guesses
guessInputJTextField.addActionListener(new GuessHandler());
messageJLabel = new JLabel("Guess result appears here." );
/*********************************************************
Write code (10 pts.) that creates the "New Game" button
*********************************************************/
newGameJButton.addActionListener(
/*************************************************************************
Write code (25 pts.) for the anonymous inner class and its interface
method that resets the application to an appropriate state to start
a new game. Reset the background color to light gray, set the
JTextFields to their initial text, call method theGame and repaint
the GuessGame JFrame
*************************************************************************/
); // end call to addActionListener
/***********************************************************************
Write code (25 pts.) that will set the layout of the container to a
Flowlayout, then add all the GUI components to the container
***********************************************************************/
theGame(); // start new game
} // end GuessGameFrame constructor
// choose a new random number
public void theGame()
{
/***********************************************************************
Write code (25 pts.) that sets instance variable number to
a random number between 1 and 1000
***********************************************************************/
} // end method theGame
// change background color
public void paint(Graphics g )
{
/********************************************************
Write code (15 pts.) that sets the background color
*********************************************************/
} // end method paint
// react to new guess
public void react(int guess )
{
/***********************************************************************
Write code (25 pts.) that increments the number of guesses and sets
instance variable currentDistance to 1000.
This variable's value will be used to determine if the background
color should be set to red or blue to indicate that the last guess
was getting closer to or further from the actual number.
***********************************************************************/
// first guess
if (guessCount == 1 )
{
/***********************************************************************
Write code (25 pts.) to set instance variable lastDistance to the
absolute value of the difference between variables guess and number.
This value will be used with subsequent guesses to help set the
background color.
***********************************************************************/
if (guess > number )
messageJLabel.setText("Too High. Try a lower number." );
else
messageJLabel.setText("Too Low. Try a higher number." );
} // end if
else
{
/**************************************************************************
Write code (25 pts.) that sets instance variable currentDistance to the
absolute value of the difference between variables guess and number.
This variable's value will be compared with lastDistance to determine
the background color.
**************************************************************************/
// guess is too high
if (guess > number )
{
messageJLabel.setText("Too High. Try a lower number." );
/*************************************************************************
Write code (25 pts.) that sets Color variable background to red if the
currentDistance is less than or equal to lastDistance; otherwise,
set background to blue. Then assign currentDistance to lastDistance.
*************************************************************************/
} // end if
else if (guess < number )// guess is too low
{
messageJLabel.setText("Too Low. Try a higher number." );
background = ( currentDistance <= lastDistance )?
Color.RED : Color.BLUE;
lastDistance = currentDistance;
}// end else if
else // guess is correct
{
messageJLabel.setText("Correct!" );
/*************************************************************************
Write code (25 pts.) that sets Color variable background to red if the
currentDistance is less than or equal to lastDistance; otherwise,
set background to blue. Then assign currentDistance to lastDistance.
*************************************************************************/
} // end else
repaint();
} // end else
} // end method react
// inner class acts on user input
class GuessHandler implements ActionListener
{
/**************************************************************************
Write code (25 pts.) that overrides the interface method and will obtain
the guess, convert it to an int and pass that value to the react method
**************************************************************************/
} // end inner class GuessHandler
}// end class GuessGameFrame
Explanation / Answer
import java.awt.EventQueue;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
class GuessGameFrame extends GuessGame
{
private static Random generator = new Random();
private int number = 0; // number chosen by application
private int guessCount; // number of guesses
private int lastDistance; // distance between last guess and number
private JTextField guessInputJTextField; // for guessing
private JLabel prompt1JLabel; // first prompt to user
private JLabel prompt2JLabel ; // second prompt to user
private JLabel messageJLabel ; // displays message of game status
private JButton newGameJButton; // creates new game
private Color background; // background color of application
//set up GUI and initialize values
GuessGameFrame()
{
prompt1JLabel = new JLabel("I have a number between 1 and 1000." ); // describe game
prompt2JLabel = new JLabel("Can you guess my number? Enter your Guess:" ); // prompt user
guessInputJTextField = new JTextField(5); // to enter guesses
guessInputJTextField.addActionListener(new GuessHandler());
messageJLabel = new JLabel("Guess result appears here." );
newGameJButton=new JButton("New Game");
newGameJButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
getContentPane().setBackground(Color.lightGray);
guessInputJTextField.setText("*****");
theGame();
System.out.println("You clicked the button");
}
}); // end call to addActionListener
FlowLayout newLayout = new FlowLayout();
newLayout.add(newGameJButton);
theGame(); // start new game
} // end GuessGameFrame constructor
// choose a new random number
public void theGame()
{
generator=random()%1000+1;
} // end method theGame
// change background color
public void paint(Graphics g )
{
getContentPane().setBackground(Color.RED);
} // end method paint
// react to new guess
public void react(int guess)
{
// first guess
if (guessCount == 1 )
{
/***********************************************************************
Write code (25 pts.) to set instance variable lastDistance to the
absolute value of the difference between variables guess and number.
This value will be used with subsequent guesses to help set the
background color.
***********************************************************************/
if (guess > number )
messageJLabel.setText("Too High. Try a lower number." );
else
messageJLabel.setText("Too Low. Try a higher number." );
} // end if
else
{
/**************************************************************************
Write code (25 pts.) that sets instance variable currentDistance to the
absolute value of the difference between variables guess and number.
This variable's value will be compared with lastDistance to determine
the background color.
**************************************************************************/
// guess is too high
if (guess > number )
{
messageJLabel.setText("Too High. Try a lower number." );
/*************************************************************************
Write code (25 pts.) that sets Color variable background to red if the
currentDistance is less than or equal to lastDistance; otherwise,
set background to blue. Then assign currentDistance to lastDistance.
*************************************************************************/
} // end if
else if (guess < number )// guess is too low
{
messageJLabel.setText("Too Low. Try a higher number." );
background = ( currentDistance <= lastDistance )?
Color.RED : Color.BLUE;
lastDistance = currentDistance;
}// end else if
else // guess is correct
{
messageJLabel.setText("Correct!" );
/*************************************************************************
Write code (25 pts.) that sets Color variable background to red if the
currentDistance is less than or equal to lastDistance; otherwise,
set background to blue. Then assign currentDistance to lastDistance.
*************************************************************************/
} // end else
repaint();
} // end else
} // end method react
// inner class acts on user input
class GuessHandler implements ActionListener
{
/**************************************************************************
Write code (25 pts.) that overrides the interface method and will obtain
the guess, convert it to an int and pass that value to the react method
**************************************************************************/
} // end inner class GuessHandler
}// end class GuessGameFrame
public class GuessGame extends JFrame
{
public static void main(String[] args)
{
GuessGameFrame a = new GuessGameFrame();
a.setSize(500,400);
a.setVisible(true);
a.setDefaultCloseOperation(EXIT_ON_CLOSE);
} // end main
} // end class GuessGame
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.