Could You help me to debug this java problm?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Jabir_Mammu
Posted On:   Thursday, September 16, 2004 07:02 AM

// : GuessGame.java // Guess the number import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GuessGame extends JFrame { private int number, guessCount; private int lastDistance; private JTextField guessInput; private JLabel prompt1, prompt2, prompt3, message; private JButton newGame; private Color background; private Container container; // set up GUI and initialize values public GuessGame() { super ("Guess Game"); /* Write a line of code that calls the superclas   More>>

// : GuessGame.java


// Guess the number


import java.awt.*;


import java.awt.event.*;


import javax.swing.*;



public class GuessGame extends JFrame {


private int number, guessCount;


private int lastDistance;


private JTextField guessInput;


private JLabel prompt1, prompt2, prompt3, message;


private JButton newGame;


private Color background;


private Container container;



// set up GUI and initialize values
public GuessGame()


{



super ("Guess Game");


/* Write a line of code that calls the superclass constructor
and sets the title of this application to "Guessing Game" */

guessCount = 0;


background = Color.lightGray;



// create GUI components
prompt1 = new JLabel( "I have a number between 1 and 1000." );


prompt2 = new JLabel( "Can you guess my number?" );


prompt3 = new JLabel( "Enter your guess: " );



guessInput = new JTextField( 5 );


guessInput.addActionListener(this);



//new ActionListener() {

public void actionPerformed( ActionEvent event )


{


/* Write code that will obtain the guess, convert it to an int and
pass that value to the react method */
}


}




message = new JLabel( "Guess result appears here." );



// button starts a new game
/* Write a statement that creates the "New Game" button */
newGame.addActionListener(



new ActionListener() {



public void actionPerformed( ActionEvent e )
{


/* Write code 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
generateRandomNumber number and repaint the GuessGame JFrame */
}


}



// add components to JFrame
container = getContentPane();


/* Write code that will set the layout of the container to a FlowLayout,
then add all the GUI components to the container */

setSize( 220, 150 );


setVisible( true );



generateRandomNumber();

// choose first random number
}

// choose a new random number
public void generateRandomNumber()
{


/* Write a statement that sets instance variable "number" to a
random number between 1 and 1000 */
}



// change background color
public void paint( Graphics g )


{


super.paint( g );


container.setBackground( background );


}



// react to new guess
public void react( int guess ) {


guessCount++;



/* Write code that 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 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 )


message.setText( "Too High. Try a lower number." );


else


message.setText( "Too Low. Try a higher number." );
}


else {


/* Write code 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 ) {


message.setText( "Too High. Try a lower number." );



/* Write code 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. */
}


else if ( guess < number ) { // guess is too low


message.setText( "Too Low. Try a higher number." );



/* Write code 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. */
}


else { // guess is correct
message.setText( " Correct! " );


background = Color.lightGray;


guessInput.setEditable( false );


guessCount = 0; // prepare for next game
}



repaint();


} // end else



} // end method react



public static void main( String args[] )


{
GuessGame application = new GuessGame();


application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );


}



} // end class GuessGame

   <<Less

Re: Could You help me to debug this java problm?

Posted By:   Christopher_Koenigsberg  
Posted On:   Sunday, September 26, 2004 05:40 AM

Who is your professor that assigned this homework to you? We need their email address or web URL, to get more info to help you.
About | Sitemap | Contact