Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-02-2009, 06:33 AM
Member
 
Join Date: Nov 2008
Posts: 30
Rep Power: 0
iWonder is on a distinguished road
Default Methods - give hint
i have a program that is basically like guessing a number between 1-20, and if the user gets it wrong, there should be a separate method which gives hint like try a higher number, try a lower, you won.

it should look something along the lines of..
enter a # between 1-20: 15
hint: try a higher number

enter a # between 1-20: 17
hint: try a lower number

enter a # between 1-20: 16
you won!

so far this is what i have
Code:
import java.util.Scanner;
public class Ch7Ex10 {
   public static void giveHint() {

     
    if  (userNum < compNum) {
      System.out.println("Try a lower number");
    } else {
      System.out.println("Try a higher number");
    }
  }
  public void main (String[]args) {
    int  compNum = 16, userNum;
    Scanner input = new Scanner (System.in);
    
    System.out.println("Enter a number between 1 and 20: ");
    userNum = input.nextInt();
    
    if (userNum == compNum) {
      System.out.println("You won!");
    }else {
      giveHint();
    }
  }
}
help will be greatly appreciated!
tips, solutions, anything will be good..
by the way if you do give a solution, pls explain?
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 01-02-2009, 03:49 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default Guess a number
You really didn't ask a question or state a problem. In the future , try to specific exactly what the problem is and that way get better help.
Back to your code... it won't compile. You have a scope problem. Variables "userNum" and "compNum" are defined in the "main" method, and are therefore not available in the "giveHint" method. There are two ways to get around this:
  • Pass these variables to the getHint method as arguments of the method (suggested way... best way)
  • Place these variables declarations in the class scope and that way they will be available to the getHint method.
Helpful link:
Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-02-2009, 09:01 PM
Member
 
Join Date: Nov 2008
Posts: 30
Rep Power: 0
iWonder is on a distinguished road
Default
yes thats my problem, it wont compile
sorry for not specifying.
anyways, so to pass the variables to the getHint method as args of the method, what do you mean by that??
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-02-2009, 09:36 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default Ops.. misspell
Quote:
anyways, so to pass the variables to the getHint method as args of the method, what do you mean by that??
Sorry about the misspell... I meant the "giveHint" method. Since I'm not sure your level of programming knowledge, I'll assume that you don't know what method arguments/parameters are.
When you call a method, you can pass information to the method in the form of arguments/parameters. For example:
Code:
class myClass ()
 {
   public void main (String[]args) 
     {
      int myFirstArg = 5;
      int mySecoundArg = 4;
      int answer = mySumMethod(myFirstArg, mySecondArg);
     }

public static int mySumMethod(int myArg1, int myArg2) 
     {
        int result = myArg1 + myArg2;
        return result;
     }
 }
For more info, consult the following link:
Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-02-2009, 09:46 PM
Member
 
Join Date: Nov 2008
Posts: 30
Rep Power: 0
iWonder is on a distinguished road
Default
so how would you go about this in my case? :s
i know you dont like giving away solutions, but i went on the links and read them and im still lost..
sorry for the inconvenience :[
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-02-2009, 10:13 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default
It's not so much about giving away the solution, it's about learning. It's obvious that even with the highlighted example that I posted, you have no clue want's going on. If I implement the solution for you, you will be exactly in the same situation as before and will have learned nothing.
I'll try to explain further:
  • userNum and compNum are variables in your main program, ok?
  • You want to pass those variables to your giveHint method so you can compare them, right?
  • You can do that by passing these variable as arguments/parameters to the giveHint method (when you call the method):
    Code:
    public static void giveHint(compNum, userNum)
  • In the giveHint method declaration, you can declare the arguments with the same name or different name as the original variable name (I declared them different in my posted example).
Did this help?

Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-03-2009, 05:04 AM
Member
 
Join Date: Nov 2008
Posts: 30
Rep Power: 0
iWonder is on a distinguished road
Default
Code:
 public static void giveHint(compNum, userNum) {

     
    if  (userNum < compNum) {
      System.out.println("Try a lower number");
    } else {
      System.out.println("Try a higher number");
    }
  }
  public void main (String[]args) {
    int  compNum = 16, userNum;
    Scanner input = new Scanner (System.in);
    
    System.out.println("Enter a number between 1 and 20: ");
    userNum = input.nextInt();
    
    if (userNum == compNum) {
      System.out.println("You won!");
    }else {
      giveHint();
    }
  }
something like that? Yes, that does clear things up
but I'm still wondering what it is I have to do to the code
I mean, I know what's going on and what's happening
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-03-2009, 01:39 PM
Senior Member
 
Join Date: Nov 2007
Posts: 155
Rep Power: 3
carderne is on a distinguished road
Default
You're close, but still not quite there.

In the method declaration for giveHint, you put the following:
Code:
public static void giveHint(compNum, userNum)
That's close, but due to our obvious lack of experience, not quite right. Those two variables need to be created in the method declaration, thus:
Code:
public static void giveHint(int compNum, int userNum)
Now those two variables can be used in the giveHint method, almost. First you have to pass the required variables to giveHint in the main method:
Code:
giveHint(firstVariable, secondVariable)
I'll leave that to you to figure out, based on my hint above...

Alternatively, and I think this is more simple in this case, you could declare the two variables (as CJLS suggested) in the class scope. This means that instead of
Code:
int compNum = 16, userNum;
in the main method, you put it jsut beneath your class declaration:
Code:
class NumberGame {
    int compNum = 16, userNum;
    ...
If you do this, you don't need to do any of the other stuff with the methods, and can leave the rest of your code as it was. I still recommend that you read up about methods, as it is quite crucial to start learning some Java...
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 01-04-2009, 05:52 AM
Member
 
Join Date: Nov 2008
Posts: 30
Rep Power: 0
iWonder is on a distinguished road
Default
Code:
public class Ch7Ex10 {
public void main (String[]args) {
    int  compNum = 16;
    userNum;
    Scanner input = new Scanner (System.in);
    
    System.out.println("Enter a number between 1 and 20: ");
    userNum = input.nextInt();
    
    if (userNum == compNum) {
      System.out.println("You won!");
    }else {
      giveHint();
    }
  }
public static void giveHint(int compNum, int userNum) {

     
    if  (userNum < compNum) {
      System.out.println("Try a lower number");
    } else {
      System.out.println("Try a higher number");
    }
  }
 
}
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 01-04-2009, 12:55 PM
Senior Member
 
Join Date: Nov 2007
Posts: 155
Rep Power: 3
carderne is on a distinguished road
Default
I take it you neglected to read my post in it's entirety?

Last edited by carderne; 01-04-2009 at 01:07 PM.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 01-04-2009, 04:03 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default oh boy...
iWonder... before we go on, I need you to answer a question:
  • Do have any Java or programming experience/knowledge at all?
Because if the answer is no, then we cannot go any further with this method stuff. We can not help you to walk if you can't crawl. It's obvious that you don't have a clue of what is going on. Two people have tried to show you how and have not had success.
I suggest that you go through the basics of Java programming and work on it until you come to the method declarations. Here's a good link to start:

The Java™ Tutorials

Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 01-04-2009, 09:32 PM
Member
 
Join Date: Nov 2008
Posts: 30
Rep Power: 0
iWonder is on a distinguished road
Default
I do have knowledge, and we have learned methods in class.
I just dont understand it the way my teacher explains it to me.

And carderne: I did read your post
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 01-04-2009, 10:25 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default One last time....
I'll try to be as clear as possible...
In your code you have the following:
Code:
giveHint();
This code is calling the giveHint method. It is missing something (hint: missing exactly two things):
  • What two things does your giveHint method need so it will work?
  • What two things (hint: variables?) do you need to compare?

If after this, you still don't know how to pass variables to a method, you still have a couple of options left:
  • have your teacher explain it to you or...
  • Using your original code, place the variable declarations:
Code:
int  compNum = 16, userNum;
within the class scope, which would be under the class statement:
Code:
public class Ch7Ex10 {
int  compNum = 16, userNum;
public void main (String[]args) {...
Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 01-05-2009, 01:05 AM
Member
 
Join Date: Nov 2008
Posts: 30
Rep Power: 0
iWonder is on a distinguished road
Default
Code:
public class Ch7Ex10 {
int compNum = 16, userNum;
public void main (String[]args) {

    Scanner input = new Scanner (System.in);
    
    System.out.println("Enter a number between 1 and 20: ");
    userNum = input.nextInt();
    
    if (userNum == compNum) {
      System.out.println("You won!");
    }else {
      giveHint();
    }
  }
public static void giveHint(int compNum, int userNum) {

     
    if  (userNum < compNum) {
      System.out.println("Try a lower number");
    } else {
      System.out.println("Try a higher number");
    }
  }
}
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 01-05-2009, 01:14 AM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default
Since your not passing the variables to the method, your method can't have arguments. Change this:
Code:
public static void giveHint(int compNum, int userNum)
to this:
Code:
public static void giveHint()
and your set.

CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 01-05-2009, 01:36 AM
Member
 
Join Date: Nov 2008
Posts: 30
Rep Power: 0
iWonder is on a distinguished road
Default
thank you so much.
I appreciate you putting up with my lack of knowledge with methods.

Sorry for my inconvenience.. but thanks again!
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 01-05-2009, 01:44 AM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default
Your welcome, but I do strongly suggest you get a handle on methods. If your going to be programming (in any language), it's something that is used day in and day out.

Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 01-05-2009, 05:20 PM
Senior Member
 
Join Date: Nov 2007
Posts: 155
Rep Power: 3
carderne is on a distinguished road
Default
Congratulations. Yes, what you need to understand is that your method call:
Code:
giveHint();
must correspond to your method declaration:
Code:
public static void giveHint()
So if your method declaration has two variables in it:
Code:
public static void giveHint(int var1, int var2)
Then your method call must also have two variables, of the same type:
Code:
giveHint(myNum, otherNum);
Where myNum and otherNum are obviously of type int.

Good luck.
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 01-05-2009, 11:39 PM
Member
 
Join Date: Nov 2008
Posts: 30
Rep Power: 0
iWonder is on a distinguished road
Default
Thanks, carderne
i did it this morning without seeing your post and i got an error and i was wondering if your suggestion would fix this error since i dont have dr.java at home...
http://i39.tinypic.com/2e4vtwy.gif the grey highlight is the error.. its usually yellow but i dont know why it turned grey :/

if it doesnt, suggestions please?
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 01-06-2009, 05:19 AM
Senior Member
 
Join Date: Dec 2008
Location: Hong Kong
Posts: 473
Rep Power: 2
mtyoung is on a distinguished road
Default
iWonder, do you know what static means?
inside static method, no non-static global variable can be used

and your program fix the value of compNum which is not really a guessing game

Last edited by mtyoung; 01-06-2009 at 05:24 AM.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to give delay? shaluchandran New To Java 10 12-17-2008 06:57 PM
Please give me a solution ivvgangadhar AWT / Swing 5 11-14-2008 02:43 PM
Trouble with static methods and boolean equals() methods with classes dreamingofgreen New To Java 5 07-22-2008 07:34 AM
code hint scroll bar ? tryit Eclipse 3 04-28-2008 08:07 AM
give me a advise sireesha New To Java 3 12-11-2007 02:38 AM


All times are GMT +2. The time now is 01:47 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org