Java Reference
In-Depth Information
}
return x + y;
}
For each of the following calls, indicate the value that is returned:
mystery(3, 3)
mystery(5, 3)
mystery(2, 6)
mystery(12, 18)
mystery(30, 75)
21. The following code is a slightly modified version of actual code that was in the Microsoft Zune music player in 2008.
The code attempts to calculate today's date by determining how many years and days have passed since 1980.
Assume the existence of methods for getting the total number of days since 1980 and for determining whether a
given year is a leap year:
int days = getTotalDaysSince1980();
year = 1980;
while (days > 365) { // subtract out years
if (isLeapYear(year)) {
if (days > 366) {
days -= 366;
year += 1;
}
} else {
days -= 365;
year += 1;
}
}
Thousands of Zune players locked up on January 1, 2009, the first day after the end of a leap year since the Zune
was released. (Microsoft quickly released a patch to fix the problem.) What is the problem with the preceding code,
and in what cases will it exhibit incorrect behavior? How can it be fixed?
22. Consider the following variable declarations:
int x = 27;
int y = -1;
int z = 32;
boolean b = false;
Write a new Boolean expression that is the negation of each of the following Boolean expressions. Use De Morgan's
laws rather than simply writing a ! at the beginning of each entire expression.
a. b
b. (x > y) && (y > z)
c. (x == y) || (x <= z)
 
Search WWH ::




Custom Search