• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

return Boolean

 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17306
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
If a regular clown is funny, then a larger clown would be funnier. Math. Verified by this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic