In: Computer Science
Lab 7. Boolean Expressions a) Write a program that evaluates the
following expressions. Assign reasonable values to the variables.
Print the results. a<b≥c , √a−7 b2 ≠c , d∨e∧f , a<b∨¬d ∧means
and, ∨means inclusive or, ¬ means not. b) Write a program that asks
a user whether he or she wants to become a Java programmer and
determines if the user typed “yes” (Print true if yes and false
otherwise.) Don't use the if
statement here
//Test java program for the expressions
//Lab7.java
public class Lab7
{
public static void main(String[] args)
{
boolean result;
int a=10;
int b=20;
int c=5;
//Expression : a< b>=c
//The above expression can be
written as b>a && b>=c
result= b>a &&
b>=c;
System.out.println("a = "+a);
System.out.println("b = "+b);
System.out.println("c = "+c);
System.out.println(" a< b>=c
= "+result);
//Expression : √a−7
System.out.println("Sqrt of ( a−7)
= "+Math.sqrt(a-7));
//Expression :b2 ≠c
result=!(b*b==c);
System.out.println("b2 ≠c =
"+result);
boolean d=true;
boolean e=true;
boolean f=false;
System.out.println("d = "+d);
System.out.println("e = "+e);
System.out.println("f = "+f);
//Experssion : d∨e∧f
result =d || e && f;
System.out.println("d∨e∧f =
"+result);
//Expression : a<b∨¬d
result =(a<b ) || !(d);
System.out.println("a<b∨¬d =
"+result);
}
}
-----------------------------------------------------------------------------------------------
Sample Output:
a = 10
b = 20
c = 5
a< b>=c = true
Sqrt of ( a−7) = 1.7320508075688772
b2 ≠c = true
d = true
e = true
f = false
d∨e∧f = true
a<b∨¬d = true
----------------------------------------------------------------------------------------------------------------------------------------
//Exercise_1.java
import java.util.Scanner;
public class Exercise_1
{
public static void main(String[] args)
{
Scanner console=new
Scanner(System.in);
//prompt for user input
System.out.println("Do you want to
become a java developer ?");
String
response=console.nextLine();
//Using ternary operator
//result =expression ? statement1 :
statement2
//statement1 is executed if
expression is true
//otherwise statement2 will be
executed.
//checking if response is yes
then
boolean
result=response.equalsIgnoreCase("yes")?true:false;
System.out.println(result);
}
}
----------------------------------------------------------------------------------------------------------------------------
Sample Output:
Do you want to become a java developer?
yes
true