• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hopefully my last question for today...

 
Lisa Beglaw
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I wrote this incorrectly so that when someone invokes the rent out method, it will still add one to the times rented field even if the movie is rented out.

So I need to change it so that if currentlyRented already = true, it won't do anything. But I am not sure how I would make this happen.

I would like it to do nothing if it is already true, and if it is false, chget it to true and add one to times rented.

But how do I set something in a method to do nothing?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34071
331
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lisa,
Note that "if (currentlyRented = true") sets it to true, while
"if (currentlyRented == true)" actually compares the two values.
"if (currentyRented)" does the same.

Now on to your question: There are two ways of doing nothing. Option 1 is to just add a comment. You could just have an empty if block, but that is confusing.


Option 2 is to rewrite the if statement to avoid the "do nothing part":
 
M Jay
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lisa,

To do this, you should replace:

???do nothing;

in your code with:

return;

This way, if the movie is currently rented, then the method will just return without going any further.

Hope that helped. If anything above was unclear to you just give me a shout.

Cheers
 
Lisa Beglaw
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne - Again with the = vs ==. This is the second time today I have been caught on that one! Thank you!!!

As for the rest, thank both.

Jeanne - I really like your shortened version and will give that a try.

M Jay - if I have the word 'return' at the end...will it actually return something?

I am off to test both! Thank again!
 
M Jay
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dearest Lisa,

Because your method has a return type of VOID, it cannot return anything. So return in such situation means "do nothing and finish executing this method".

Hope that was helpful.

Cheers
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally I would prefer Jeanne's version. In some circles using a return statement other than at the end of a method (which isn't even needed in a void method) is considered bad practise because it can make it confusing to understand what the method is doing.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic