Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java logical test questions

I've been solving a few problems about logic tests for AP Computer Science but I happened to get stuck on a few questions.

Here are the directions from the website: Translate each of the following English statements into logical tests that could be used in an if/else statement. Write the appropriate logical test for each statement below. Assume that three int variables, x, y, and z, have already been declared.

These are the 2 questions I have problems with:

Either x or y is even, and the other is odd.

x and z are of opposite signs.

I've been trying to find these answers out for a couple of hours and I still have no clue. I would appreciate it if someone could guide me in the right direction. I understand this is "homework" but some definitive help would be very helpful.

like image 933
user1287412 Avatar asked Nov 23 '25 03:11

user1287412


2 Answers

For the first question: x % 2 != y % 2 Second question: x * z < 0

like image 72
Rico Avatar answered Nov 24 '25 18:11

Rico


You'll need to use and (&&) and or (||) to make a logic formula. I'm not going to do yours, but here's another one:

x is bigger than both y and z or x is less than both y and z.

Translates to:

((x > y) && (x > z)) || ((x < y) && (x < z))

You just need to figure out a formula for odd/even (hint - the low order bit) and for positive/negative (hint - compare with 0), and combine those with and/or.

like image 24
Keith Randall Avatar answered Nov 24 '25 18:11

Keith Randall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!