0

In my class I have set the member variable honey as follows:

int honey = 10;

What I want the following method to do is take as a parameter the amount of hooney required and either return that amount if it is available and remove the specified amount from the stores or return 0 if there is no honey to be had. My method is as follows:

public int takeHoney(int h2){
    if(h2 <= honey){
        honey = honey - h2;
        return h2;
    }else{
        return 0;
    }
}

When my test harness does this:

h.takeHoney(Integer.MAX_VALUE);

it does not go down the if branch as I wanted it to but down the else branch. Why is this? Surely as it is using Integer.MAX_VALUE it is removing the exact value that is in the hive and therefore h2<= honey as it equals honey?

user2973447
  • 339
  • 1
  • 3
  • 13
  • Who is Honey and who is h ? Also your method is returning a value not setting in anywhere. – Jorge Campos Dec 07 '13 at 01:59
  • Agree with @JorgeCampos. Do you store value of this function anywhere? What is the value of Honey? – e.doroskevic Dec 07 '13 at 02:07
  • 1
    Because MAX_VALUE is larger than any `Honey` value you're apt to have. – Hot Licks Dec 07 '13 at 02:08
  • @HotLicks I would say, at least equal not larger. If the Honey is an integer an has the value of MAX_VALUE it would attend the if. But you are rigth. Most likely the Honey value is less than MAX_VALUE. – Jorge Campos Dec 07 '13 at 02:09
  • BTW, standard Java coding convention is that symbols with Leading Caps are reserved for class names, and variables and methods start with a lower case letter. – Hot Licks Dec 07 '13 at 02:09
  • changed my code to make honey lowercase to match convention. – user2973447 Dec 07 '13 at 02:22

4 Answers4

1

Is "Honey" a global variable? if yes what is its value?

"If" block will only work if value of variable Honey is greater than or equal to variable h2. I think value of Honey is lesser than h2. So that is why else block is executed.

[EDIT] You updated the question and clearly mention honey=10.

condition in if block (h2 <= honey)

value of honey=10;

h2= Integer.MAX_VALUE= 2147483647 . Clearly h2>honey .

So your if block will never be executed in this scenario.

change method call as follows

h.takeHoney(10);

or

h.takeHoney(9);

(your if block will be executed for any value lesser than 11)

vivek
  • 198
  • 4
  • 10
0

Honey is not equal to anything. Because Honey is beginning with a capital it seems it is a class. If the if is always being executed then the passed value must be less than Honey.

vivek
  • 198
  • 4
  • 10
yatesrates
  • 11
  • 3
0

Yes, your return appears to be wrong (returning the input isn't what you want) - Also, you really should use lower case variable names.

public int takeHoney(int h2){
  // Honey changed to honey, use Java naming conventions (please).
  if(h2 <= honey){
    honey = honey - h2;
  } else {
    // Not enough honey... log it? throw an exception? ... return -1...
    System.err.println("Oh bother - Insufficient Honey Encountered");
  }
  return honey;
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I think he wants to return the amount of Honey taken. – Hot Licks Dec 07 '13 at 02:08
  • @HotLicks I think he wants to take **ALL OF THE HONEY**. Maybe I am reading to much into `takeHoney(Integer.MAX_VALUE)`. – Elliott Frisch Dec 07 '13 at 02:14
  • You are both right- I want to return the amount of honey taken when this method is run but in my test harness I want to remove all of the honey from the hive at certain points to make sure that certain methods work but I do not wish to return what's left in the hive but what was taken so I can use the method to "feed" my bees later – user2973447 Dec 07 '13 at 02:21
  • @user2973447 You need a method that returns the current amount of honey. Then pass the result of that into takeHoney. – Hot Licks Dec 07 '13 at 02:23
  • @user2973447 Presumably, you should have a single decrement then (assuming each bee will take "1" honey). – Elliott Frisch Dec 07 '13 at 02:25
0

The only reason why else statement is executed instead of if statement is because the condition you've specified for if is not met.

if(h2 <= Honey) // This does not happen - condition is not met!
e.doroskevic
  • 2,129
  • 18
  • 25