Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

GuessGame.java program problem

$
0
0
Ok I am currently working on the last bit of my program and it needs to include the lastDistance and currentDistance forumla to tell the user whether their current guess is closer or further from the actual number.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("unused")
public class GuessGame extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// define class variables here
	private JButton tryButton;
	private JButton newgameButton;
	private JButton exitButton;
	private JTextField guessField;
	private JLabel guessLabel;
	private JLabel headerLabel;
	private JTextField results;
	int num;
	int count;
	/**
	 * Class constructor. It is in this method where you will initialize all
	 * your class variables and build the GUI
	 */
	public GuessGame() {
		//set title of frame
		setTitle("Guessing Game");
		//set size of frame
		setSize(325, 210);
		//create the random value
		Random rand=new Random();
		num=rand.nextInt(1000)+1;
		count=0;
		// initialize all class variables
		tryButton = new JButton("Try the number");
		exitButton = new JButton("Exit");
		newgameButton = new JButton("New Game");
		guessLabel = new JLabel("Enter your guess:");
		headerLabel = new JLabel(
				"<html>I have a number between 1 and 1000,<br>can you guess the number?</html>");
		guessField = new JTextField(5);
		results = new JTextField(25);
		
		// add a default close operation to the main frame
		// so that it closes when x is pressed on the top
		// right corner of the GUI.
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// add an action listener to your buttons
		// each button can have its own separate class
		// since each will act differently
		TryButtonHandler tbHandler = new TryButtonHandler();
		tryButton.addActionListener(tbHandler);

		ExitButtonHandler eHandler = new ExitButtonHandler();
		exitButton.addActionListener(eHandler);

		NewGameButtonHandler ngHandler = new NewGameButtonHandler();
		newgameButton.addActionListener(ngHandler);

		// set the mnemonic of each button
		tryButton.setMnemonic('T');
		exitButton.setMnemonic('E');
		newgameButton.setMnemonic('N');

		// set the results field properties
		results.setBackground(Color.white);
		results.setEditable(false);
		results.setText("");

		// set the  layout
		setLayout(new FlowLayout(FlowLayout.CENTER));

		// add all components to the frame
		add(headerLabel);
		add(guessLabel);
		add(guessField);
		add(tryButton);
		add(exitButton);
		add(newgameButton);
		add(results);
		
	}

	/**
	 * Inner class for the try button This class contains code that will act on
	 * the try button when it is pressed.
	 */
	class TryButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			// TODO: you need to put the code here for the try button
			int guess=0;
			count++;
			try{
				guess=Integer.parseInt(guessField.getText());
			}catch (Exception e) {
				// TODO: handle exception
			}
			if(guess==num){
				results.setText("Correct! The number of guesses: "+count);
			}else if(guess<num){
				results.setText("Too Low!");
				getContentPane().setBackground(Color.white);
				
			}else {
				results.setText("Too High!");
				getContentPane().setBackground(Color.white);
			}
		}
	}

	/**
	 * Inner class for the exit button This class contains code that will act on
	 * the exit button when it is pressed.
	 */
	class ExitButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			System.exit(0);
		}

	}

	/**
	 * Inner class for the new game button This class contains code that will
	 * act on the new game button when it is pressed.
	 */
	class NewGameButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			// TODO: You need to put your code here for the new game button
			Random rand=new Random();
			num=rand.nextInt(1000)+1;
			results.setText("");
			guessField.setText("");
			count=0;
		}
	}

	/**
	 * Class' main method.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		GuessGame g=new GuessGame();
		g.setVisible(true);
	}
}


Here is what is required, I have no idea how to start it or where I need to place it.

To turn the text field red or blue I have two variables in my program: lastDistance and currentDistance. You set last distance and current distance when you make the first guess. For example if you guess 500 and the actual number is 700, the distance is 200. Let's say the next time you guess 300 so the current distance is 400. Compare that to the last distance. It is greater so you turn the text field blue (you are getting colder). Now put the current distance in last distance. Next time you guess 600. The currect distance is 100 which is less than the last distance so turn the text field red (you are getting warmer).

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>