solving java excersice
Complete the following exercises:
1. Assume b1 and b2 are declared Java boolean variables. Write a Java expression for each of the following Boolean statements:
a. b1 and b2
b. either b1 or b2
c. b1 but not b2
d. not b1 but b2
e. either b1 or not b2
f. either not b1 or b2
g. neither b1 nor b2 (note this is not the same as 1f)
h. not b1 and not b2
i. either b1 or b2 but not both b1 and b2 (this is called exclusive-or)
2. Assume the boolean variables b1 from b2 from question 1 have been assigned values: b1 = true and b2 = false. What is the resulting (boolean) value of evaluating each of the expressions from question 1? Remember the order of operations of boolean operators: not is evaluated before and, which is evaluated before or.
3. In Boolean (Predicate) Logic, there are some additional operators that are often used:
a. The implication (→) operator is defined as:
|
A |
B |
A → B |
|
true |
true |
true |
|
true |
false |
false |
|
false |
true |
true |
|
false |
false |
true |
b. Implement the implication operator as a Java method
c. The equivalence (↔) operator is defined as:
|
A |
B |
A ↔ B |
|
true |
true |
true |
|
true |
false |
false |
|
false |
true |
false |
|
false |
false |
true |
d. Implement the equivalence operator as a Java method
Extra exercise:
26. Complete the following truth table by finding the truth values of the Boolean expressions for all combinations of the Boolean inputs p, q, and r.
27. State whether the following is true or false. If false, explain:
A && B is the same as B && A for any Boolean conditions A and B.
28. The "advanced search" feature of many search engines allows you to use Boolean operators for complex queries, such as (cats OR dogs) AND NOT pets. Contrast these search operators with the Boolean operators in Java.
HELPFUL EXAMPLES:
------------------------------------------------
! (NOT)
|
A |
! A |
|
T |
F |
|
F |
T |
&& (AND)
|
A |
B |
A&&B |
|
T |
T |
T |
|
T |
F |
F |
|
F |
T |
F |
|
F |
F |
T |
|| (MOLUSIVE OR)
|
A |
B |
A||B |
|
T |
T |
T |
|
T |
F |
T |
|
F |
T |
T |
|
F |
F |
F |
|
A |
B |
dmnb (A,B) |
|
T |
T |
F |
|
T |
F |
T |
|
F |
T |
T |
|
T |
F |
F |
(d || m) && ! (d&&m)
|
d |
m |
d || m |
d && m |
! (d && m) |
(d || m) && ! ( d && m ) |
|
T |
T |
T |
T |
F |
F |
|
T |
F |
T |
F |
T |
T |
|
F |
T |
T |
F |
T |
T |
|
F |
F |
F |
F |
T |
F |