High Low game

Having trouble setting the if statments

Page 1 of 1

14 Replies - 5911 Views - Last Post: 10 April 2010 - 10:20 PM Rate Topic: -----

#1 ryan_pin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-April 10

High Low game

Posted 10 April 2010 - 03:01 PM

Ok so I am having a few problems, first is when I try to compile I get:

HighLow2working is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener
public class HighLow2working extends Applet implements ActionListener

Another issue I am having is actually getting this to work, I really am at a loss and have no clue. If anyone can help it would be greatly appreciated.

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.util.Scanner;
import java.util.Random;

public class HighLow2working extends Applet implements ActionListener
{

  //declare variables
  Panel nPanel, sPanel, tcPanel, bcPanel, cPanel;
  Button one;
  Label top,lbl1, lbl2, lbl3, lbl4, bottom, output;
  TextField txt;
  int number, cnumber;
  final int MAX = 25;

 public void init()
  {
  //set north panel
  nPanel = new Panel();
  nPanel.setBackground(Color.pink);
  top = new Label("HighLow Game");

  //set south panel
  sPanel = new Panel();
  sPanel.setBackground(Color.green);
  Label outputLabel = new Label("Pick a number between 1-25");

  //set center panel
  cPanel = new Panel();
  tcPanel = new Panel();
  bcPanel = new Panel();

  one = new Button("Submit your Guess");
  one.addActionListener(this);
  lbl1 = new Label();
  lbl2 = new Label();
  lbl3 = new Label("WOW, That guess was high");
  lbl3.setBackground(Color.red);
  txt = new TextField();
  lbl4 = new Label("Whoa, you hit the bottom, try a higher number");
  lbl4.setBackground(Color.blue);

  //set topcenter GridLayout
  tcPanel.setLayout (new GridLayout(3,1,5,5));
  tcPanel.add(lbl1);
  tcPanel.add(one);
  one.addActionListener(this);
  tcPanel.add(lbl2);

  //set bottomcenter GridLayout
  bcPanel.setLayout (new GridLayout(3,1,5,5));
  bcPanel.add(lbl3);
  bcPanel.add(txt);
  bcPanel.add(lbl4);

  //combine tc and bc panels
  cPanel.setLayout(new BorderLayout());
  cPanel.add(tcPanel, BorderLayout.WEST);
  cPanel.add(bcPanel, BorderLayout.EAST);

  //set layout and label for top
  nPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
  nPanel.add(outputLabel);

  //set layout and label for bottom
  sPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
  sPanel.add(bottom);

  //set layout for the Applet
  this.setLayout(new BorderLayout());
  add(nPanel, BorderLayout.NORTH);
  add(cPanel, BorderLayout.CENTER);
  add(sPanel, BorderLayout.SOUTH);
 }

public void actionPreformed(java.awt.event.ActionEvent e)
{
	number = Integer.parseInt(txt.getText());
	Random generator = new Random();
		cnumber = generator.nextInt(MAX) + 1;
	
	}


 }



Is This A Good Question/Topic? 0
  • +

Replies To: High Low game

#2 immeraufdemhund  Icon User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: High Low game

Posted 10 April 2010 - 03:36 PM

...
  public void actionPerformed(java.awt.event.ActionEvent e) //you put PREformed not PERformed
  {
    number = Integer.parseInt(txt.getText());
    Random generator = new Random();
    cnumber = generator.nextInt(MAX) + 1;
  }
}



when it says that it's abstract blah blah blah.. it means that you didn't use all the methods for said call. usually it means one of two things. 1st obviously you didn't use all of them.. 2nd is if you used it and it says you didn't, chances are you didn't spell it right or use it right. spelling is critical in programming, but so far your code looks good.
Was This Post Helpful? 0
  • +
  • -

#3 ryan_pin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-April 10

Re: High Low game

Posted 10 April 2010 - 07:42 PM

Thank you, I was beating my head against the keyboard wondering why it was producing this error. I guess it just takes a fresh set of eyes.
Was This Post Helpful? 0
  • +
  • -

#4 ryan_pin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-April 10

Re: High Low game

Posted 10 April 2010 - 08:03 PM

I got it working... somewhat, the problem I am having now is that it seems to only allow me to have 2 turns, is there a way to clear out the previous statment once you enter a new guess? Here is my updated code.
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.util.Scanner;
import java.util.Random;

public class HighLow2working extends Applet implements ActionListener
{

  //declare variables
  Panel nPanel, sPanel, tcPanel, bcPanel, cPanel;
  Button one;
  Label top,lbl1, lbl2, lbl3, lbl4, bottom, output;
  TextField txt;
  int number, cnumber;
  final int MAX = 25;

 public void init()
  {
  //set north panel
  nPanel = new Panel();
  nPanel.setBackground(Color.pink);
  top = new Label("HighLow Game");

  //set south panel
  sPanel = new Panel();
  sPanel.setBackground(Color.green);
  Label outputLabel = new Label("Pick a number between 1-25");

  //set center panel
  cPanel = new Panel();
  tcPanel = new Panel();
  bcPanel = new Panel();

  one = new Button("Submit your Guess");
  one.addActionListener(this);
  lbl1 = new Label();
  lbl2 = new Label();
  lbl3 = new Label();
  txt = new TextField();
  lbl4 = new Label();

  //set topcenter GridLayout
  tcPanel.setLayout (new GridLayout(3,1,5,5));
  tcPanel.add(lbl1);
  tcPanel.add(one);
  one.addActionListener(this);
  tcPanel.add(lbl2);

  //set bottomcenter GridLayout
  bcPanel.setLayout (new GridLayout(3,1,5,5));
  bcPanel.add(lbl3);
  bcPanel.add(txt);
  bcPanel.add(lbl4);

  //combine tc and bc panels
  cPanel.setLayout(new BorderLayout());
  cPanel.add(tcPanel, BorderLayout.WEST);
  cPanel.add(bcPanel, BorderLayout.EAST);

  //set layout and label for top
  nPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
  nPanel.add(top);

  //set layout and label for bottom
  sPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
  sPanel.add(outputLabel);

  //set layout for the Applet
  this.setLayout(new BorderLayout());
  add(nPanel, BorderLayout.NORTH);
  add(cPanel, BorderLayout.CENTER);
  add(sPanel, BorderLayout.SOUTH);
 }

public void actionPerformed(java.awt.event.ActionEvent e)
{
	number = Integer.parseInt(txt.getText());
	Random generator = new Random();
		cnumber = generator.nextInt(MAX) + 1;
	if (number > cnumber)
	{
		lbl3.setText("Number is too high");
	}
	else if (number < cnumber)
	{
		lbl4.setText("Number is too low");
	}
	else if (number == cnumber)
	{
		output.setText("You got it!!!");
	}

	}


 }


Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10390
  • View blog
  • Posts: 38,450
  • Joined: 27-December 08

Re: High Low game

Posted 10 April 2010 - 08:11 PM

You can use the setText() method for the TextField to clear the text. Also, in your actionPerformed() method, you are generating a new random number each time the user enters a guess and hits the submit button. So when I enter my first guess, the correct answer will be different than on my second guess. You may want to include a reset or new game feature to handle random number generating.
Was This Post Helpful? 0
  • +
  • -

#6 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: High Low game

Posted 10 April 2010 - 08:15 PM

This doesn't really affect anything, but you add the same ActionListener to your 'one' Button twice:
one.addActionListener(this);
...
one.addActionListener(this);


Was This Post Helpful? 1
  • +
  • -

#7 ryan_pin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-April 10

Re: High Low game

Posted 10 April 2010 - 08:47 PM

View Postmacosxnerd101, on 10 April 2010 - 07:11 PM, said:

You can use the setText() method for the TextField to clear the text. Also, in your actionPerformed() method, you are generating a new random number each time the user enters a guess and hits the submit button. So when I enter my first guess, the correct answer will be different than on my second guess. You may want to include a reset or new game feature to handle random number generating.


By adding the setText() method for the TextField do you mean something like this?
if (number > cnumber) 
        { 
                lbl3.setText("Number is too high"); 
                lbl4.setText();
        } 
        else if (number < cnumber) 
        { 
                lbl4.setText("Number is too low");
                lbl3.setText();
        } 




I tried this out with no joy.

About the number being regenerated all of the time, I really have no idea how to stop this.

Thank you for your help.

PS.
one.addActionListener(this); 
... 
one.addActionListener(this);



fixed Thank you for the sharp eyes.
Was This Post Helpful? 0
  • +
  • -

#8 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: High Low game

Posted 10 April 2010 - 08:50 PM

Quote

About the number being regenerated all of the time, I really have no idea how to stop this.


Only generate the random number once, inside your init method, so it'll stay the same no matter how many times the button is clicked.
Was This Post Helpful? 1
  • +
  • -

#9 carden2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 25-March 10

Re: High Low game

Posted 10 April 2010 - 09:10 PM

Scanner Scanner = new Scanner(System.in);
    int secretNumber= you'll use math.random to generate your secret number before the loop
    int guess = -1;  // initialize guess so it can't be equal to secretNumber
    int count=1;
    while (count <= 10 && guess != secretnumber)
    {
			 
	 
      System.out.println("Make guess # " + count + ":");  
      guess=Scanner.nextInt ();        

      if(guess < secretnumber)                
         System.out.println ("No your guess was too low.");
      else
      if (guess > secretnumber)
			System.out.println ("No your guess was too high.");
 
       count ++;
    }



This is the code I used in my program that is similar except mine was 10 guesses and number was from 1 to 1000. I had a library with a method saved that picked a random number so mine was secretNumber= gamble.pickNumber(1000) then I used my loop after there 10 guesses I had my print statements to end the game or play again. hope that helps.

This post has been edited by carden2: 10 April 2010 - 09:14 PM

Was This Post Helpful? 0
  • +
  • -

#10 ryan_pin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-April 10

Re: High Low game

Posted 10 April 2010 - 09:14 PM

Could you go into a little more detail? Currently, I am having 2 problems.
1. Whenever the guess is correct, the "You guessed it!!" wont show up in the SOUTH region.
2. The number will not stay the same.

I guess there is a third problem.
3. I still dont understand how to clear the text, example.
person chooses 3 number is too low "Too low" and Blue background show up
person chooses 7 number is too high "Too high" and red background show up, while clearing the previous blue background

Here is what I have.

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.util.Scanner;
import java.util.Random;

public class HighLow2working extends Applet implements ActionListener
{

  //declare variables
  Panel nPanel, sPanel, tcPanel, bcPanel, cPanel;
  Button one;
  Label top,lbl1, lbl2, lbl3, lbl4, bottom;
  TextField txt;
  int number, cnumber;
  final int MAX = 25;

 public void init()
  {
  //set north panel
  nPanel = new Panel();
  nPanel.setBackground(Color.pink);
  top = new Label("HighLow Game");

  //set south panel
  sPanel = new Panel();
  sPanel.setBackground(Color.green);
  Label bottom = new Label("Pick a number between 1-25");

  //set center panel
  cPanel = new Panel();
  tcPanel = new Panel();
  bcPanel = new Panel();

  one = new Button("Submit your Guess");
  one.addActionListener(this);
  lbl1 = new Label();
  lbl2 = new Label();
  lbl3 = new Label("                                                                 ");
  txt = new TextField();
  lbl4 = new Label("                                                                 ");

  //set topcenter GridLayout
  tcPanel.setLayout (new GridLayout(3,1));
  tcPanel.add(lbl1);
  tcPanel.add(one);
  tcPanel.add(lbl2);

  //set bottomcenter GridLayout
  bcPanel.setLayout (new GridLayout(3,1));
  bcPanel.add(lbl3);
  bcPanel.add(txt);
  bcPanel.add(lbl4);

  //combine tc and bc panels
  cPanel.setLayout(new BorderLayout());
  cPanel.add(tcPanel, BorderLayout.WEST);
  cPanel.add(bcPanel, BorderLayout.EAST);

  //set layout and label for top
  nPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
  nPanel.add(top);

  //set layout and label for bottom
  sPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
  sPanel.add(bottom);

  //set layout for the Applet
  this.setLayout(new BorderLayout());
  add(nPanel, BorderLayout.NORTH);
  add(cPanel, BorderLayout.CENTER);
  add(sPanel, BorderLayout.SOUTH);
 }

public void actionPerformed(ActionEvent e)
{
	number = Integer.parseInt(txt.getText());

	Random generator = new Random();
		cnumber = generator.nextInt(MAX) + 1;
	if (number > cnumber)
	{
		lbl3.setText("Number is too high");
	    lbl3.setBackground(Color.red);
	}
	else if (number < cnumber)
	{
		lbl4.setText("Number is too low");
		lbl4.setBackground(Color.blue);
	}
	else if (number == cnumber)
	{
		bottom.setText("You got it!!!");
	}

	}


 }


Was This Post Helpful? 0
  • +
  • -

#11 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: High Low game

Posted 10 April 2010 - 09:17 PM

Move these lines into init to fix the error of the number not staying the same:
public void init(){
   Random generator = new Random();
   cnumber = generator.nextInt(MAX) + 1;
   ...
}


Was This Post Helpful? 1
  • +
  • -

#12 ryan_pin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-April 10

Re: High Low game

Posted 10 April 2010 - 09:53 PM

Ok with Erik's insperation I got the random number working. I was always able to figure out the issue with the win message in the South. Any ideas on how to clear the previous guesses?
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10390
  • View blog
  • Posts: 38,450
  • Joined: 27-December 08

Re: High Low game

Posted 10 April 2010 - 10:05 PM

View Postmacosxnerd101, on 10 April 2010 - 11:11 PM, said:

You can use the setText() method for the TextField to clear the text. Also, in your actionPerformed() method, you are generating a new random number each time the user enters a guess and hits the submit button. So when I enter my first guess, the correct answer will be different than on my second guess. You may want to include a reset or new game feature to handle random number generating.


You can use the setText() method on the TextField. So if your TextField is named txt, you can use txt.setText("");. This will clear your TextField of all its contents.
Was This Post Helpful? 1
  • +
  • -

#14 ryan_pin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-April 10

Re: High Low game

Posted 10 April 2010 - 10:13 PM

Oh geez I feel dumb now, when I was taking you advice the first time you posted I only put:
lbl4.setText();
Not even thinking about the "", should have know better.

Anyways I added a counter to the finished product. Thank you all for your help.

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.util.Scanner;
import java.util.Random;

public class HighLow2working extends Applet implements ActionListener
{

  //declare variables
  Panel nPanel, sPanel, tcPanel, bcPanel, cPanel;
  Button one;
  Label top,lbl1, lbl2, lbl3, lbl4, bottom;
  TextField txt;
  int number, cnumber,count;
  final int MAX = 25;

 public void init()
  {

  //create random number
  Random generator = new Random();
		cnumber = generator.nextInt(MAX) + 1;

  //set north panel
  nPanel = new Panel();
  nPanel.setBackground(Color.pink);
  top = new Label("HighLow Game");

  //set south panel
  sPanel = new Panel();
  sPanel.setBackground(Color.green);
  bottom = new Label("                                               ");

  //set center panel
  cPanel = new Panel();
  tcPanel = new Panel();
  bcPanel = new Panel();

  one = new Button("Submit your Guess");
  one.addActionListener(this);
  lbl1 = new Label();
  lbl2 = new Label();
  lbl3 = new Label("                                                                 ");
  txt = new TextField();
  lbl4 = new Label("                                                                 ");

  //set topcenter GridLayout
  tcPanel.setLayout (new GridLayout(3,1));
  tcPanel.add(lbl1);
  tcPanel.add(one);
  tcPanel.add(lbl2);

  //set bottomcenter GridLayout
  bcPanel.setLayout (new GridLayout(3,1));
  bcPanel.add(lbl3);
  bcPanel.add(txt);
  bcPanel.add(lbl4);

  //combine tc and bc panels
  cPanel.setLayout(new BorderLayout());
  cPanel.add(tcPanel, BorderLayout.WEST);
  cPanel.add(bcPanel, BorderLayout.EAST);

  //set layout and label for top
  nPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
  nPanel.add(top);

  //set layout and label for bottom
  sPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
  sPanel.add(bottom);

  //set layout for the Applet
  this.setLayout(new BorderLayout());
  add(nPanel, BorderLayout.NORTH);
  add(cPanel, BorderLayout.CENTER);
  add(sPanel, BorderLayout.SOUTH);
 }

public void actionPerformed(ActionEvent e)
{
	number = Integer.parseInt(txt.getText());

	count++;

	if (number > cnumber)
	{
		lbl3.setText("Number is too high");
	    lbl3.setBackground(Color.red);
	    lbl4.setText("");
	}
	else if (number < cnumber)
	{
		lbl4.setText("Number is too low");
		lbl4.setBackground(Color.blue);
		lbl3.setText("");
	}
	else if (number == cnumber)
	{
		bottom.setText("It only took you " + count + " guesses!!!");
	}
}
}


Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10390
  • View blog
  • Posts: 38,450
  • Joined: 27-December 08

Re: High Low game

Posted 10 April 2010 - 10:20 PM

Not a problem. Glad we could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1