issues getting this code to work

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chloe03
    New Member
    • Apr 2014
    • 1

    issues getting this code to work

    I am stuck trying to get this code work, it's suppose to return true or false if the FitnessFrog is on a black stone, numbered 1 - 11 and if it's on a stone 0 or 12 or higher, neither FitnessFrog is suppose to move. I want to know if I am on the right track with this or not. Thank you.

    Code:
     /**
        * 
        */
       private boolean isOnBlackStones(FitnessFrog AFitnessFrog)
       {
     
        if (AFitnessFrog.getPosition() < 1)
        {
           
          }
         if (AFitnessFrog.getPosition() > 11)
          {
              
          }
          return false;
       }
    }
    Last edited by Rabbit; Apr 4 '14, 05:43 AM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Hi Chloe03 and welcome to bytes.com!

    Just to make sure I understand this correctly, the above function should return true if the stone passed has a number between 1 and 11 inclusive and false otherwise? If so, you've switched the logic here as if it's neither smaller than 1 nor larger than 11 (which would mean it's not a black stone) it will return false when I would expect it to return true. Alternatively you could check whether it is at least 1 and no larger than 11 and if so return true and false otherwise.

    Comment

    • chaarmann
      Recognized Expert Contributor
      • Nov 2007
      • 785

      #3
      The two if-statements are empty (despite its indentation), they are doing nothing. It always returns false. Just put "return false" in line 9 and 13 and change line 15 to "return true".

      One more tip for expert:
      Don't use copy-and-paste programming style!
      So call AFitnessFrog.ge tPosition() only once and store it in a local variable.
      Frogs can jump quickly and uncontrolled (whenever they want). So when you call AFitnessFrog.ge tPosition() first time, you may get back 1, but when the code moves on executing and you call the same AFitnessFrog.ge tPosition() again a few lines below, you may get back 0, because the frog jumped from stone 1 to stone 0 in the meantime.
      That means now your method returns true which is wrong.

      To avoid this, you can also use synchronization instead. But then issue remains that calling AFitnessFrog.ge tPosition() twice leads to bad performance if this method getPosition() does anything now (or will be changed in the future) that takes a long time.

      Comment

      Working...