Java Reference
In-Depth Information
3.21
Assume that x and y are int type. Which of the following are legal Java expressions?
x > y > 0
x = y && y
x /= y
x or y
x and y
(x != 0 ) || (x = 0 )
3.22
Suppose that x is 1 . What is x after the evaluation of the following expression?
a. (x >= 1 ) && (x++ > 1 )
b. (x > 1 ) && (x++ > 1 )
3.23
What is the value of the expression ch >= 'A' && ch <= 'Z' if ch is 'A' , 'p' ,
'E' , or '5' ?
3.24
Suppose, when you run the program, you enter input 2 3 6 from the console. What
is the output?
public class Test {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
double x = input.nextDouble();
double y = input.nextDouble();
double z = input.nextDouble();
System.out.println( "(x < y && y < z) is " + (x < y && y < z));
System.out.println( "(x < y || y < z) is " + (x < y || y < z));
System.out.println( "!(x < y) is " + !(x < y));
System.out.println( "(x + y < z) is " + (x + y < z));
System.out.println( "(x + y < z) is " + (x + y < z));
}
}
3.25
Write a Boolean expression that evaluates true if age is greater than 13 and less
than 18 .
3.26
Write a Boolean expression that evaluates true if weight is greater than 50 pounds
or height is greater than 60 inches.
3.27
Write a Boolean expression that evaluates true if weight is greater than 50 pounds
and height is greater than 60 inches.
3.28
Write a Boolean expression that evaluates true if either weight is greater than 50
pounds or height is greater than 60 inches, but not both.
3.12 Case Study: Determining Leap Year
A year is a leap year if it is divisible by 4 but not by 100 , or if it is divisible by 400 .
You can use the following Boolean expressions to check whether a year is a leap year:
Key
Point
// A leap year is divisible by 4
boolean isLeapYear = (year % 4 == 0 );
// A leap year is divisible by 4 but not by 100
isLeapYear = isLeapYear && (year % 100 != 0 );
// A leap year is divisible by 4 but not by 100 or divisible by 400
isLeapYear = isLeapYear || (year % 400 == 0 );
Or you can combine all these expressions into one like this:
isLeapYear = (year % 4 == 0 && year % 100 != 0 ) || (year % 400 == 0 );
 
 
Search WWH ::




Custom Search