Results 1 to 13 of 13
  1. #1
    Mirix is offline Member
    Join Date
    May 2010
    Location
    Alexandria, VA
    Posts
    14
    Rep Power
    0

    Default Returning Value from a method

    How would you return a value from a method and using it for an if-then statement?
    For example, I have my robot, and I want him to count the numbers of beepers in a pile using:

    Java Code:
    public int numOfBeepersInPile()
    {
       return numOfBeepersInPile();
    }
    then if there is:
    • 1 beeper in the pile, turn left
    • 2 beepers in pile, turn around
    • three beepers, turn right


    How would I take the value of "numOfBeepersInPile" and apply an if-else statement to it?

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Unless the method numOfBeepersInPile() was a typo, you're going to get a StackOverflowError error.

    To apply a method in an if statement, do what your intuition tells u:
    If numOfBeepersInPile is a field (a variable), then u would do this:
    Java Code:
    if (numOfBeepersInPile == 1)
         turnLeft;
    if (numOfBeepersInPile == 2)
         turnAround;
    if (numOfBeepersInPile == 3)
         turnRight;
    If numOfBeepersInPile is a method, then u would do this:
    Java Code:
    if (numOfBeepersInPile() == 1)
         turnLeft;
    if (numOfBeepersInPile() == 2)
         turnAround;
    if (numOfBeepersInPile() == 3)
         turnRight;
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  3. #3
    Mirix is offline Member
    Join Date
    May 2010
    Location
    Alexandria, VA
    Posts
    14
    Rep Power
    0

    Default

    Well they're giving me two methods to fill in for it:

    Java Code:
    public int numOfBeepersInPile()
    {
       return numOfBeepersInPile();  //So it can display the amount of beepers
    }
    and

    Java Code:
    public void turn(int beepers)
    {
       //not sure what to go here... how would I connect beepers and numOfBeepersInPile together?
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Mirix View Post
    Well they're giving me two methods to fill in for it:

    Java Code:
    public int numOfBeepersInPile()
    {
       return numOfBeepersInPile();  //So it can display the amount of beepers
    }
    Please re-read what lil' just stated. This code above will call recursively -- will call itself repeatedly, and will cause your program to crash. So don't do it.

  5. #5
    Mirix is offline Member
    Join Date
    May 2010
    Location
    Alexandria, VA
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Please re-read what lil' just stated. This code above will call recursively -- will call itself repeatedly, and will cause your program to crash. So don't do it.
    In that case, how would you suggest I return the value? :)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    [QUOTE=Mirix;123796]
    Quote Originally Posted by Mirix View Post
    In that case, how would you suggest I return the value? :)
    By doing just that, returning the value not making another method call. You do understand that the parenthesis at the end makes it a method call, right?

  7. #7
    Mirix is offline Member
    Join Date
    May 2010
    Location
    Alexandria, VA
    Posts
    14
    Rep Power
    0

    Default

    By doing just that, returning the value not making another method call. You do understand that the parenthesis at the end makes it a method call, right?[/QUOTE]

    Yes, so would it be

    Java Code:
    return numOfBeepersInPile;
    ? But that still gives me an error that there's no such variable.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Mirix View Post
    Java Code:
    return numOfBeepersInPile;
    ? But that still gives me an error that there's no such variable.
    Well, does your program have such a field?

    I feel that without seeing your code, we're groping about blindly.

  9. #9
    Mirix is offline Member
    Join Date
    May 2010
    Location
    Alexandria, VA
    Posts
    14
    Rep Power
    0

    Default

    The purpose of this lab is for the robot to "read" a map by walking forward until it sees a pile of beepers, the amount of beepers in that pile will determine if it'll turn left/right/around, ect. It picks up all the beepers in the pile and heads in the direction mentioned until it reaches another pile of beepers and so on until it reaches a pile with 5 beepers, which marks as the "treasure."

    I have to create an algorithm for this using three methods: approachPile(), numOfBeepersInPile(), and turnAppropriately().
    (This is a resource class.)

    Java Code:
     import edu.fcps.karel2.Display;
        public class Pirate extends Athlete
       {
           public Pirate()
          {
            super(1, 1, Display.EAST, 0);
          }
          
           public void approachPile()
          {
                while(!nextToABeeper())
    		  {
    		  	move();
    		  }
          }
           
           public int numOfBeepersInPile()
          {
          	    return numOfBeepersInPile;  //? store value in here?
          }
           
          public void turnAppropriately(int beepers)
          {
            // If 1, turn Left
            // If 2, turn Around
            // If 3, turnRight
          }
       }

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm not sure that this class should even have a numOfBeepersInPile() method as this is more of a property of the map itself then the robot.

  11. #11
    Mirix is offline Member
    Join Date
    May 2010
    Location
    Alexandria, VA
    Posts
    14
    Rep Power
    0

    Default

    Well, the specification of this lab said:

    "Load Unit1\Pirate.java. Pirate extends Athlete and has only a no-arg constructor starting each pirate at (1, 1) facing east with no beepers. There are three methods in Pirate that you must implement so that Lab18 will work. The method approachPile() should give you no trouble. The method

    public int numOfBeepersInPile()

    has to count and return the number of beepers in that pile. Ask your teacher how to return a value from a method. The method turnAppropriately(int beepers) can be implemented with an if-else ladder, a for-loop, or a switch statement. If you want to use the switch statement, ask your teacher about its syntax.

    Load Unit1\Lab18.java at size 8x8. Use maps “map1”, “map2”, and “map3”. Compile and run."

    hmm

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  13. #13
    Mirix is offline Member
    Join Date
    May 2010
    Location
    Alexandria, VA
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Well shut my mouth and call me an idiot. I'm obviously wrong -- sorry.
    lolz! No, of course I won't call you that, everyone has a different view on a problem :D. Thank you for your input :]

Similar Threads

  1. Inherited method returning bad value
    By viking90 in forum New To Java
    Replies: 11
    Last Post: 04-07-2010, 04:53 PM
  2. returning an object from a method
    By bigj in forum New To Java
    Replies: 7
    Last Post: 01-08-2010, 01:39 PM
  3. Returning complex data types from a web method
    By Tshegofatsom in forum Advanced Java
    Replies: 6
    Last Post: 05-15-2009, 04:30 PM
  4. Replies: 3
    Last Post: 12-10-2008, 02:38 AM
  5. Need help. Method won't returning proper value..
    By zlwilly in forum New To Java
    Replies: 2
    Last Post: 12-02-2008, 10:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •