RU beehive logo ITEC dept promo banner
ITEC 120
2010fall
ibarland

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs

lab11b

Today we will review hw08-soln.html.

Use the remaining time to solve any of the suggested problems on recent lecture, or recent lab and write an interactive main method as in recent lecture, which calls it repeatedly until the user indicates end-of-input (by typing ^C (Windows) or ^D (Unix,Mac)).

Find the Zune bug

On midnight of New Year's Eve 2008, everybody's Zune crashed — they froze up as soon as any application tried to access the system date. The culprit turned out to be in the following code:

class Calendar {
    int year;
    int day;  // #days into the current year (0 means Jan.01).

    /** Constructor.
     * @param daysSinceEpoch The number of days since ORIGIN_YEAR started.
     * From this, we calculate the year and the #days into the current year.
     */
    Calendar( int daysSinceEpoch ) {
       int daysSoFar = daysSinceEpoch;
       int yearSoFar = ORIGIN_YEAR; /* = 1980 */
   
       while (daysSoFar > 365) {
           if (Calendar.isLeapYear(yearSoFar)) {
               if (daysSoFar > 366) {
                   daysSoFar -= 366;
                   yearSoFar += 1;
               }
           }
           else {
               daysSoFar -= 365;
               yearSoFar += 1;
           }
       }
   
       this.year = yearSoFar;
       this.day  = daysSoFar;
   }
} 
Adapted (put into a class context) from here.

Exercise: Can you step through this program and find the real-life bug somewhere in the while-loop?

hint: The error occured on Jan.01 2009. Suppose we get near the end of the loop — say, yearSoFar reaches 2007. At that moment, daysSoFar would be 366+365=731.

What 120 principle was violated, that led to the bug? (It's one which we adhered to at first, but then relaxed once we had mutation and assigning-to-fields.)

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs


©2010, Ian Barland, Radford University
Last modified 2010.Nov.16 (Tue)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme