This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

return Boolean

 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following method in my Current class:



This method is getting the results I expect but I am a little confused with the Boolean return statement as it is not actually being used anywhere?

The third line from the bottom:
this.newBalance = newBalance - amount;

is returning what I require i.e. the newBalance which is going into the method:



What I don't therefore understand is why is the method withdrawal returning a Boolean for allowed?

Is it because I should NOT be putting; in the method withdrawal:
The third line from the bottom:
this.newBalance = newBalance - amount;

instead, should I be putting something in the setBalance method like:




When I tried this though, I didn't get the expected results. If fact the newBalance didn't take account of the withdrawal amount.???

Thanking you in advance!
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What I don't therefore understand is why is the method withdrawal returning a Boolean for allowed?

What would happen if I have a balance of $100, and try to withdraw $1,000? Does the withdraw happen? And Should it happen? Read through (i.e. trace through it) the first withdraw method you posted and see what happens. Does the withdraw occur? And what is returned as a boolean value?
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, be careful when referring to the boolean return. You stated in both your subject line and in your question, that I quoted above, the method is returning a Boolean (capital 'B'). It is however returning a boolean (lower case 'b'). A boolean is a primitive type, whereas a Boolean is an class; a wrapper class for the boolean primitive. All the primitive types in Java have such wrapper classes, and the wrapper class' name is usually the same as the primitive, except they start with a capital letter. The one exception is int has a wrapper class of Integer. If you have not learned about these wrapper classes, you will. In the meantime you can read about them in the api docs.
 
Junilu Lacar
Bartender
Pie
Posts: 7794
62
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems to me that the intended use would be something like this:




The "allowed" variable was not meant to be used inside the withdraw method; it was meant to indicate a status, i.e. whether the withdrawal was allowed or not.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maureen Charlton:

(snip)

What I don't therefore understand is why is the method withdrawal returning a Boolean for allowed?

Is it because I should NOT be putting; in the method withdrawal:
The third line from the bottom:
this.newBalance = newBalance - amount;



I think the confusion is with terminology. This line of code does NOT return anything. It sets a member variable named "newBalance" which can be later retrieved with the getBalance() method. To return a value, you HAVE TO use the "return" keyword. So look through the withdraw() method and try to find where you use the word "return". This will show you what the method is returning directly. It should also indicate why this methead returns boolean (note the lower case "b"), rather than double.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic