• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

https://codingbat.com/prob/p187868 - boolean default values

 
Greenhorn
Posts: 25
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm starting the coding bat problem series on Java

You can find the first exercise in this link https://codingbat.com/prob/p187868.
The problem reads:
"The parameter weekday is true if it is a weekday, and the parameter vacation is true if we are on vacation. We sleep in if it is not a weekday or we're on vacation. Return true if we sleep in.

sleepIn(false, false) → true
sleepIn(true, false) → false
sleepIn(false, true) → true
"

My code:


Issue: My code is incorrect (it tells me it is correct for 75% of the cases, not the 100% of the cases). It only becomes correct once I take away the assignments:
 

Why? Don't we need to initialize these booleans to true? (isn't the default value of a boolean false, otherwise?). And why does my solution not fit 100% of cases?

Thanks
 
author & internet detective
Posts: 39399
763
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pedro,
By setting those to true, you are replacing the values passed into the method. Suppose codingbat tests your method with
sleepIn(true, false)

Your code sets vacation to true. So the method returns true instead of false.
 
Rancher
Posts: 137
7
IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pedro,

The variables weekday and vacation do not need to be initialized, they represent the parameters of your method. These are the values that are passed to the method.

 
Pedro Esgueira
Greenhorn
Posts: 25
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeanne, Brecht

I see now the confusion I was making in my mind. Of course, they are just parameters, I shouldn't want to limit them by "overwritting" their argument value to a specific value.
 
Saloon Keeper
Posts: 3416
149
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Pedro,

just a quick remark. Suppose that A is a boolean expression that is either true or false (for instance: A is the expression: x == 5 || x == 6), and that you have this code:

In other words: when A is true, return true, otherwise, when A is false, return false. In both cases you return the value of A, so you might as well write:

And that can be shortened to the short and simple:

Do you see a way to apply this in your code?
 
Pedro Esgueira
Greenhorn
Posts: 25
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piet, yes

return (!weekday || vacation)

thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!