This program asks the user to enter two values for boolean variables x and y, fo
ID: 3730129 • Letter: T
Question
This program asks the user to enter two values for boolean variables x and y, followed by a logical operation represented by a character according to the following description:
1) AND: Represented by characers a or A;
2) OR: Represented by characters o or O;
3) XOR: Represented by characters x or X;
notice that the XOR of two boolean variables is true if one and only one of the two variables is true.
The program then assigns a value to a boolean variable z to the following rules:
1) z is (x AND y) if the selected operation is AND;
2) z is (x OR y) if the selected operation is OR;
3) z is ( x XOR y) if the selected operation is XOR;
4) z is (NOT x) otherwise.
For example, if the user enters 0 for x, 1 for y, and a for operation, then z must be assigned the value of 0.
Explanation / Answer
Hi, you have not mentioned about programming language.
Please try to mention all details.
I have implemented in Java.
import java.util.Scanner;
public class LogicalOperation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter value of x: ");
int x = sc.nextInt();
System.out.print("Enter value of y: ");
int y = sc.nextInt();
System.out.print("Enter operation(a/A, o/O, x/X : ");
char c = sc.next().charAt(0);
c = Character.toLowerCase(c);
int z = 0;
switch(c){
case 'a':
if(x==1 && y==1)
z = 1;
else
z = 0;
break;
case 'o':
if(x==1 || y==1)
z = 1;
else
z = 0;
break;
case 'x':
if(x==y)
z = 0;
else
z = 1;
break;
default:
z = x==0?1:0;
}
System.out.println(z);
}
}
/*
Sample run:
Enter value of x: 1
Enter value of y: 1
Enter operation(a/A, o/O, x/X : x
0
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.