• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using &&

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Neither my text, or the book I just bought, explain to me how to use the && for and except is very simple ways....
a && b is true if both are true and false in all other cases.
That's it!

In my code below I am getting an error message "operator && cannot be applied to int,boolean". What can I use in it's place to make my code work?

 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the code below, please take a look at the text in bold,


here the result of pickAYear = released will not result in a boolean, whereas the short-circuit && requires a boolean on either side that is <boolean>&&<boolean>.

What you are doing here pickAYear = released is an assignement operation.
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to the comment above, I would refactor the negative logic:


as well as the int var names that should typically be nouns. Combined everything together, it would look like this:

 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John , surely you have refactored the code,

to


But what if the year was equal to 1930 or 1969, would'nt it be different from the logic Lisa has for pickAYear. I think the refactored code has to look like,
 
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John is right I believe.

The problem in the first version is that although the code is designed to produce an invalid year error message, it is actually testing for a valid year - and then negating it. Thats why the refactor was needed - because the original was so confusing
 
Lisa Beglaw
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Man...you guys are AWESOME!!!

Once again, it was a silly beginners mistake on my part using = instead of ==...but I am a beginner so it is still allowed.

But I love the way the date code has been shortened too!!! WAY BETTER than what I have.

The reason I chose to use the ! in the dates is because of the wording of the teachers questions...
Add a method called "specialReport". This method takes a year as its parameter. If the parameter is not a year between 1930 and 1969 inclusive...blah blah blah...

But I think the samples here are WAY better and I used the ! in my last statement that finished the code.

Again, you guys have been fantastic!
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Stuart John is right, I was wrong on that one. Lisa please use John's code.
 
Lisa Beglaw
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Abdulla!
 
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 Lisa Beglaw:
Man...you guys are AWESOME!!!

Once again, it was a silly beginners mistake on my part using = instead of ==...but I am a beginner so it is still allowed.

But I love the way the date code has been shortened too!!! WAY BETTER than what I have.

The reason I chose to use the ! in the dates is because of the wording of the teachers questions...
Add a method called "specialReport". This method takes a year as its parameter. If the parameter is not a year between 1930 and 1969 inclusive...blah blah blah...

But I think the samples here are WAY better and I used the ! in my last statement that finished the code.

Again, you guys have been fantastic!



Boolean logic can be a little tricky when you first encounter it. If you plan on continuing programming, and assuming you are going to a university at the moment, I suggest that you take classes in Discrete Math and Logic. If your math department doesn't have a course in Logic, then check the philosophy department since it can sometimes show up there.

In short, there are many logically equivalent ways to represent the statement "the year is not a between 1930 and 1969 inclusive".

Layne
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be honest, what did (and always does) throw me was the double use of negation. Though John's version is my prefered one, I think even a version using just one negation would be OK (though we would probably drown in parentheses).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic