Method repOk() | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Method repOk()

My question is is this work the same ? Code1: Public boolean repOk() { Return validateId(id) && validateName(name) && validateDob(dob); } Code2: Public boolean repOk() { If (!(validateId(id)) || !(validateName(name)) || !(validateDob(dob))) { return false; } else { return true; } *)Note that method validate check is id, name or dob valid. I think it work the same but i'm not sure

18th Apr 2019, 1:41 AM
Ha Nguyen
Ha Nguyen - avatar
4 Answers
+ 1
First code returns true if everything is valid. Second code returns false if one thing isn't valid. Since you have only 3 booleans in there and you are not sure, you can check if it outputs the same thing on paper. You can also check de Morgan's laws.
18th Apr 2019, 7:47 AM
Adam Priesnitz
Adam Priesnitz - avatar
+ 1
Adam Priesnitz so first code will return false if one thing is not valid, right? So code 1 and code 2 are the same right ?
18th Apr 2019, 7:58 AM
Ha Nguyen
Ha Nguyen - avatar
+ 1
Yes, they both return the same output. In first code you check if everything is true and in the second code if something is false.
18th Apr 2019, 8:04 AM
Adam Priesnitz
Adam Priesnitz - avatar
0
Adam Priesnitz many thanks
18th Apr 2019, 12:25 PM
Ha Nguyen
Ha Nguyen - avatar