/* GuessGrameFrame.java */ package guessinggame; import java.awt.Color; import j
ID: 3690213 • Letter: #
Question
/* GuessGrameFrame.java */
package guessinggame;
import java.awt.Color;
import java.awt.FlowLayout; // specifies how components are arranged
import java.awt.Graphics;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.JFrame; // provides basic window features
import javax.swing.JTextField;
import javax.swing.JLabel; // display text and image
import javax.swing.JButton;
import javax.swing.JOptionPane;
public class GuessGameFrame extends JFrame
{
private static Random generator = new Random();
private int number; // number chosen by application
final int MAX = 1000;
private String userGuess;
private final FlowLayout layout;
private final Container container;
private int guess;
private int guessCount; // number of guesses
private int lastDistance; // distance between last guess and number
private int currentDistance;
private JTextField guessInputJTextField; // for guessing (text field with set size)
private JLabel prompt1JLabel; // first prompt to user (JLabel with just text and icon)
private JLabel prompt2JLabel; // second prompt to user (JLabel with just text and icon)
private final JLabel messageJLabel; // displays message of game status (JLabel with just text and icon)
private final JButton newGameJButton; // creates new game
private Color background; // background color of application
// GuessGameFrame constructor set up GUI and initialize values
public GuessGameFrame()
{
super( "Guessing Game" ); //calls the superclass constructor and sets the title of this application to "Guessing Game"
guessCount = 0; // initialize number of guesses to 0
background = Color.MAGENTA; // set background to magenta
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." );
layout = new FlowLayout();
container = getContentPane();
setLayout( layout ); newGameJButton = new JButton( "New Game" );// creates the "New Game" button
newGameJButton.addActionListener
(
new ActionListener() // anonymous inner class
{
public void actionPerformed( ActionEvent e )
{
guessCount = 0;
guessInputJTextField.setText(" "); background = Color.MAGENTA;
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 revalidate();
repaint();
} // end method actionPerformed
} // end anonymous inner class );
// end call to addActionListener
layout.setAlignment( FlowLayout.CENTER );
layout.layoutContainer( container );
add(prompt1JLabel); // add prompt1JLabel to JFrame
add(prompt2JLabel); // add prompt2JLabel to JFrame
add(guessInputJTextField) ; // add InputJTextField to JFrame
add(messageJLabel); // add messageJLabel to JFrame
add ( newGameJButton ); // add newGameJButton to JFrame
theGame(); // start new game
} // end GuessGameFrame constructor
// choose a new random number
public void theGame()
{ int guess = 0;
number = generator.nextInt( MAX ) + 1;
} // end method theGame
// change background color
public void paint( Graphics g )
{
super.paint( g );
getContentPane().setBackground( background ); // set background
} // end method paint
// react to new guess
public void react( int guess )
{
guessCount++; // increment guesses
currentDistance=1000; // first guess
if ( guessCount == 1 )
{
lastDistance = Math.abs( guess - number );
if(lastDistance==0) { messageJLabel.setText( "Correct!" );
repaint();
}
else
{
if ( guess > number )
messageJLabel.setText( "Too High. Try a lower number." );
else
messageJLabel.setText( "Too Low. Try a higher number." );
}
} // end else if
else
{
currentDistance = Math.abs( guess - number );
// guess is too high
if ( guess > number )
{
messageJLabel.setText( "Too High. Try a lower number." );
background = ( currentDistance <= lastDistance ) ? Color.RED : Color.ORANGE;
lastDistance = currentDistance;
} // end if
else if ( guess < number ) // guess is too low
{
messageJLabel.setText( "Too Low. Try a higher number." );
background = ( currentDistance <= lastDistance ) ? Color.RED : Color.ORANGE;
lastDistance = currentDistance;
} // end else if
else // guess is correct
{
messageJLabel.setText( "Correct!" );
} // end else
repaint();
} // end else
} // end method react
// inner class acts on user input
class GuessHandler implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
String inputString; inputString = guessInputJTextField.getText();
guess = Integer.parseInt( inputString ); react(guess);
} // end method actionPerformed
} // end inner class GuessHandler
} // end class GuessGameFrame
========================================================================================================================================================
/* GuessGame.java */
package GuessingGame;
import javax.swing.JFrame;
public class GuessGame
{
public static void main( String args[] )
{
GuessGameFrame guessGame = new GuessGameFrame ();
guessGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
guessGame.setSize( 300, 150 ); // set frame size
guessGame.setVisible( true ); // display frame
} // end main
} // end class GuessGame
When I compile in CMD
javac *.java
I got the error cannot find symbol - class GuessGameFrame
GuessGameFrame guessGame = new GuessGameFrame ();
Explanation / Answer
The error is caused as you stored GuessGame.java in package GuessingGame; and GuessGameFrame.java in package guessinggame;
Here both package names are different so you got that error.and it recommended to start the package name with small letters.
so you have to store GuessGame.java in package package guessinggame;
This will rectify your error.
___________________________________________________________________________________________
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.