Complete the following exercises: Assume b1 and b2 are declared Java boolean var
ID: 3803852 • Letter: C
Question
Complete the following exercises:
Assume b1 and b2 are declared Java boolean variables. Write a Java expression for each of the following Boolean statements:
b1 and b2
either b1 or b2
b1 but not b2
not b1 but b2
either b1 or not b2
either not b1 or b2
neither b1 nor b2 (note this is not the same as 1f)
not b1 and not b2
either b1 or b2 but not both b1 and b2 (this is called exclusive-or)
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.
In Boolean (Predicate) Logic, there are some additional operators that are often used:
The implication () operator is defined as:
Implement the implication operator as a Java method
The equivalence () operator is defined as:
Implement the equivalence operator as a Java method
A B A B true true true true false false false true true false false trueExplanation / Answer
Hi,
Please see below the tester class which includes all the expressions and methods.
Please comment for any queries/feedbacks.
Thanks.
BooleanTester.java
public class BooleanTester {
public static void main(String []args){
//declaring boolean variables
boolean b1 = true;
boolean b2 = false;
//b1 and b2
if(b1 && b2 ){
System.out.println("b1 and b2 is TRUE " );
}
else{
System.out.println("b1 and b2 is FALSE " );
}
//either b1 or b2
if(b1 || b2){
System.out.println("either b1 or b2 is TRUE " );
}
else{
System.out.println("either b1 or b2 is FALSE " );
}
//b1 but not b2
if(b1 && !b2){
System.out.println("b1 but not b2 is TRUE ");
}
else{
System.out.println("b1 but not b2 is FALSE ");
}
//not b1 but b2
if(!b1 && b2){
System.out.println("not b1 but b2 is TRUE ");
}
else{
System.out.println("not b1 but b2 is FALSE ");
}
//either b1 or not b2
if(b1 || !b2){
System.out.println("either b1 or not b2 is TRUE ");
}
else{
System.out.println("either b1 or not b2 is FALSE ");
}
//either not b1 or b2
if(!b1 || b2){
System.out.println("either not b1 or b2 is TRUE");
}
else{
System.out.println("either not b1 or b2 is FALSE");
}
//neither b1 nor b2
if(!b1 || !b2){
System.out.println("neither b1 nor b2 is TRUE ");
}
else{
System.out.println("neither b1 nor b2 is FALSE ");
}
//not b1 and not b2
if(!b1 && !b2){
System.out.println("not b1 and not b2 is TRUE ");
}
else{
System.out.println("not b1 and not b2 is FALSE ");
}
//either b1 or b2 but not both b1 and b2
if((b1 || b2) && (!(b1 && b2))){
System.out.println("either b1 or b2 but not both b1 and b2 TRUE ");
}
else{
System.out.println("either b1 or b2 but not both b1 and b2 FALSE ");
}
//Checking implication
System.out.println("Implication: ");
System.out.print("true, true ->");
System.out.print(implication(true,true));
System.out.println();
System.out.print("true, false ->");
System.out.print(implication(true,false));
System.out.println();
System.out.print("false, true ->");
System.out.print(implication(false,true));
System.out.println();
System.out.print("false, false ->");
System.out.print(implication(false,false));
System.out.println();
//Checking equivalence
System.out.println("equivalence : ");
System.out.print("true, true ->");
System.out.print(equivalence (true,true));
System.out.println();
System.out.print("true, false ->");
System.out.print(equivalence (true,false));
System.out.println();
System.out.print("false, true ->");
System.out.print(equivalence (false,true));
System.out.println();
System.out.print("false, false ->");
System.out.print(equivalence (false,false));
System.out.println();
}
//Implement the implication operator as a Java method
public static boolean implication(boolean a,boolean b){
boolean retVal = false;
if(a && b){
retVal = true;
}
else if(a && !b){
retVal = false;
}
else if(!a && b){
retVal = true;
}
else if(!a && !b){
retVal = true;
}
return retVal;
}
//Implement the equivalence operator as a Java method
public static boolean equivalence (boolean a,boolean b){
boolean retVal = false;
if(a && b){
retVal = true;
}
else if(a && !b){
retVal = false;
}
else if(!a && b){
retVal = false;
}
else if(!a && !b){
retVal = true;
}
return retVal;
}
}
Sample output:
b1 and b2 is FALSE
either b1 or b2 is TRUE
b1 but not b2 is TRUE
not b1 but b2 is FALSE
either b1 or not b2 is TRUE
either not b1 or b2 is FALSE
neither b1 nor b2 is TRUE
not b1 and not b2 is FALSE
either b1 or b2 but not both b1 and b2 TRUE
Implication:
true, true ->true
true, false ->false
false, true ->true
false, false ->true
equivalence :
true, true ->true
true, false ->false
false, true ->false
false, false ->true
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.