Corey Smith wrote:@Junilu, I haven't learned how to implement that in Java yet...But I assume it's like a table of boolean logical operators? (I might be wrong)
Thank you. If they are either positive or negative, then a 0 input should be banned.Corey Smith wrote:. . . it did not display any value of 0.
Thank you. Have tried the exercise.Here's the link . . .
Yes it is; it is called a truth table. You can implement it as Junilu showed you: a ^ b. I also suggest you remind yourself about the different precedences of operators, and learn about the ?: operator.. . . table of boolean logical operators? . . .
Junilu Lacar wrote:When you fix the logic error in your boolean expression, you could still simplify it further using the fact that a negative number multiplied by a positive number will be negative.
Corey Smith wrote:@Mike, the first one worked when using ternary operator, meanwhile, the second one was trying to simplify the whole thing and failed in the end. As you can see in the screenshot, I managed to get most of them correct except for the red ones.
Mike Simmons wrote:Well, it should be. Unless you overflow...
Corey Smith wrote:I guess some exercise won't be possible to solve with just only return (exclude ternary operator), I thought it is always better to shorten the expression in if-else statement by writing only "return (expression);".
Junilu Lacar wrote:
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:the part: "a < 0 ^ b < 0" is incorrect. See OP's specification in his/her opening post.
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:Not good enough for me.
Sorry for the delay in replying; I was all busy with other things yesterday.Junilu Lacar wrote:. . . The specs are unclear as to what to do with 0 and the tests given don't include data with 0.
That is what I tried and got green throughout from codingbat. But I was quite disappointed by the quality of their suggested solution.As far as that goes then, the code is correct. Plus, the site accepts that code, too, so it's good enough for me.
That is something you shoule probably familiarise yourself with. The return in the if causes that block to “terminate abruptly” and the flow of control cannot continue to whatever follows the if.Corey Smith wrote:. . . how did the return keyword get there without using else . . .
Nice, but I have a allergy to the >= and <= operators. I believe they are harder to read than > and <. I have a concept of the limiting value, which might end 0 or 00. Is that in the lower category in which case you use >, or is it in the upper category so you use <? In the case of grades, the mark ending 0 is usually in the upper category; you get the same grade for 60 as for 69 but more than for 59. In the case of parcels, the weight ending 0 is in the lower category; a 1000g parcel costs the same as 501g, and less than 1000.1g, if the scales actually read such small divisions.Note additional spaces make operators and numbers align vertically.Note the indentation of the operators is different in each block; you will have to work out an indentation convention for nested ?:a and stick to it. Use whatever is the easiest to read. Round here, if you get <40 in only one module, and that mark is ≥30, you can usually get away without having to resit that one module, hence F_C. Marks work better as enum elements than as literal Strings.
No, you should rewire the code to make it easier for your brain to understand. Formatting the code shown so all the colons are aligned vertically is a much better solution. The days of restricted memory, when it cost a significant memory overhead to write spaces or line ends in the code, are long gone. Memory is cheap; use it. Brainpower is expensive; conserve it. The days of restricted brainpower, when it cost a significant brainpower overhead to read programs without spaces or line ends in the code, are still with us. Us mods are expensive; you are lucky to get experience free of charge, but if somebody shows us unformatted code, we are liable simply to fling it back with a shout of, “Format it!” You try it with your boss at work. Your unformatted code will come back about 2sec later with, “Don't waste my time with illegible rubbish like that.”. . . my brain is rewired to easily read those long one line codes.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |