#question about the first boolean assignment in Mc Cue,s e-book

66 messages · Page 1 of 1 (latest)

hollow thorn
#

I,m doing the e-book modern java from mr. Mc Cue and I don,t understand the boolean
assignment no. 1:

I solved it correctly, but it was a guess.
I noted it written out to understand it.
Did I interpreted it correctly or not?

public void main(){

boolean a = true;
boolean b = false;
boolean c = true;
boolean d = false;

boolean result = a || b && c || !d;
// true || false && true || NOT false = true
// true OR  false AND true = false) OR true
// true OR false OR true = true??


IO.println(result); // true

}

lusty muralBOT
#

<@&987246399047479336> please have a look, thanks.

drowsy halo
#

best to put it into a codeblock to render properly:

void main() {
    boolean a = true;
    boolean b = false;
    boolean c = true;
    boolean d = false;

    boolean result = a || b && c || !d;

    IO.println(result);
}
hollow thorn
#

sorry, I see discord does something weird to my text.

drowsy halo
#

yeah markdown uses || to hide spoiler text

#

anyways, how would you go about this task
you would go from left to right with proper precedence

#

so a || b first, what does this evaluate to?

hollow thorn
#

here,s my full code block:


    boolean a = true;
    boolean b = false;
    boolean c = true;
    boolean d = false;

    boolean result = a  b && c  !d;
    // true  false && true  NOT false = true
    // true OR  false AND true = false) OR true
    // true OR false OR true = true??


    IO.println(result); // true
}```
lusty muralBOT
hollow thorn
drowsy halo
#

that means it results into?

hollow thorn
#

False

drowsy halo
#

its actually true

hollow thorn
#

loool see

drowsy halo
#

a or b is true if at least one of them is true

hollow thorn
#

ooo yes because it,s the OR and not the AND

drowsy halo
#

ok so we have true && c which is?

#

well I think I am doing it wrong now myself 💀

#

have to keep in mind operator precedence

#

so b && c gets executed first 😅

hollow thorn
drowsy halo
hollow thorn
#

true OR false OR true

#

but couldn,t the answer then not also be false?
because it,s OR?

hollow thorn
drowsy halo
#

wdym "it could"
true OR false OR true

evaluates from left to right, true || false is true, then we got true || false again which is also true

#

kinda hard to follow lol

hollow thorn
#

I thought true || false could both run because of the OR
But you,re saying it always is true in that case

#

ooo right, because then the if statement (for example) will run

wise crow
#

a || b && c || !d;
if a is true, it stop and is true
if a is false, it check if b and c are true for the 2,
if b and c are somehow false, it check if d is false

#

|| can be read as if everything before it is false, try the next

prime sigil
lusty muralBOT
#

@hollow thorn

Your question has been closed due to inactivity.

If it was not resolved yet, click the button below to keep it
open, or feel free to create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure 👍

hollow thorn
hollow thorn
lilac torrent
hollow thorn
lilac torrent
#

the point is to force you to find a way to

#

once you've done that i can show you some short ones

hollow thorn
#

okay, I will share my solution once I tested it! thnx!

hollow thorn
#
   // Change these two variables to test your solution
    boolean hasIceCream = true;
    boolean hasCookie = false;

    boolean validChoice = true;

    if (hasIceCream == true && hasCookie == true) {
        validChoice = false;
    } else if (hasIceCream == false && hasCookie == false) {
        validChoice = false;
    }

    IO.println(validChoice);      
    
}```
lusty muralBOT
lilac torrent
#
public void main(){
   // Change these two variables to test your solution
    boolean hasIceCream = true;
    boolean hasCookie = false;

    boolean validChoice = !(hasIceCream && hasCookie) && (hasIceCream || hasCookie);
}
#

so "not both, but at least one"

#
public void main(){
   // Change these two variables to test your solution
    boolean hasIceCream = true;
    boolean hasCookie = false;

    boolean validChoice = !((hasIceCream && hasCookie) || (!hasIceCream && !hasCookie));
}
#

this is what you wrote, more or less

#

NOT (both or none)

#

also works

hollow thorn
#

Would one solution be preferred and why?

#

Syntax is shorter, so it saves time in readability?

prime sigil
#

shorter doesnt always mean easier to read/understand

smoky lodge
prime sigil
#

or u make intermediate bools: hasBoth, hasNone

#

well named variables help a lot

smoky lodge
#

Also not mentioned in the book,
https://en.wikipedia.org/wiki/De_Morgan's_laws

In propositional logic and Boolean algebra, De Morgan's laws, also known as De Morgan's theorem, are a pair of transformation rules that are both valid rules of inference. They are named after Augustus De Morgan, a 19th-century British mathematician. The rules allow the expression of conjunctions and disjunctions purely in terms of each other vi...

hollow thorn
hollow thorn
lilac torrent
#
MIT OpenCourseWare

6.111 is reputed to be one of the most demanding classes at MIT, exhausting many students' time and creativity. The course covers digital design topics such as digital logic, sequential building blocks, finite-state machines, FPGAs, timing and synchronization. The semester begins with lectures and problem sets, to introduce fundamental topics be...

#

here is a good pdf

smoky lodge
#

Sorry, Wikipedia math is the worst kind of math (assumes PhD level.) Anyway, the demorgan's law is the most recognized transformation for boolean algebra. If you have an AND you can turn it into a OR (or vice versa) and possibly make your statement more readable. Generally, there's little use for these types of boolean statements in the wild since anything over two conditions is a code smell in my book. For more complicated scenarios there's Karnaugh Maps as a tool to reduce boolean conditions, but that usually more in the circuit design realm and when you have conditions that you 'don't care' about.