CSE 201 - Homework 3
Read Section 2.14 in Chapter 2 and Sections 3.1-3.5 in Chapter 3 in
the textbook, and then answer the following questions.
- Given the following declarations, evaluate each of the Boolean
expressions below.
boolean x = false, y = true, z = false;
x && y
y || z
x || y || !z
(x || y) && (y || z)
(!(y || z)) || x
z && x && y
(!y) || (z || !x)
z || (x && (y || z))
- Given the following declarations, write a Boolean expression
for each of the problems listed below.
boolean x, y, z;
int i, j, k;
- Write a Boolean expression that is true if and only
if either x or y is true but not both
- Write a Boolean expression that is true if and only
if x is true and y is false
- Write a Boolean expression that is true if and only
if i has a value between -3 and 7 (excluding -3 and 7)
- Write a Boolean expression that is true if and only
if i is an odd number
- Write a Boolean expression that is true if and only
if the sum of i and j is equal to twice the value of k
- Write a Boolean expression that is true if and only
if i, j, and k all have distinct values
- For each of the following program segments, write the output
produced.
-
int x = 12, y = -7, z = 0, a;
if (x > y) {
if (x > z) {
a = x;
}
else {
a = z;
}
}
else {
if (y > z) {
a = y;
}
else {
a = z;
}
}
System.out.println(a);
-
int number = 45;
if (number < 10) {
System.out.println("what");
}
else if (number < 50) {
System.out.println("will");
}
else if (number < 100) {
System.out.println("this");
}
else {
System.out.println("print");
}
-
int x = 12, y = -7, z = 0;
if (x > y) {
int t = x;
x = y;
y = t;
}
if (x > z) {
int t = x;
x = z;
z = t;
}
if (y > z) {
int t = y;
y = z;
z = t;
}
System.out.println(x + "," + y + "," + z);
- For each of the following questions, write a code segment that
solves the given problem.
- Write a program segment that translates a letter grade
into a number grade. Letter grades are A, B, C, D, E. Their
numeric values are 4, 3, 2, 1, and 0, respectively. Assume
that the letter grade is in a variable of type char
called letterGrade. The program segment should
assign the corresponding number grade to a variable of type
int called numberGrade.
- Write a program segment that decides whether a given year
is a leap year or not. A year is a leap year if it is
divisible by 4, except for the case when it is also
divisible by 100 and not by 400 hundred. So, for example, 1999
is not a leap year because it is not divisible by 4; 2000 is
a leap year because it is divisible by 4 and although it is
divisible by 100, it is also divisible by 400; 1900 however,
is not a leap year because it is divisible by 100 and not by
400. Finally, 2004 is a leap year because it is divisible
by 4 and not by 100.
Assume that the year is stored in a variable
of type int called year. The program segment
should print to the screen "leap year" if the year stored
in year is a leap year, or print "not leap year" if
it is not a leap year.