CS 117 Winter 2006 Lab 5
Midterm Exam Post Mortem

This lab is to be done individually. Create a directory lab5, and put your answers there. Submit answers to all of these questions before class on Friday, 10 February 2006.

Tasks for the day:

  1. Examine the following code:
    int x = 117;
    while (x >= 0 && (x % 3) != 2) {
        while (x % 2 == 1) {
    	x = (x - 1)/2;
        if (x == 6) {
    	x = 50;
        x = x - 1;
        System.out.println("x = " + x);
    What will it output? Trace through this code. In a file loop.txt, describe what output will be produced. (You may also do this on paper if you prefer.) You may retype this code into a Java program and run it, but only after you have traced it by hand. If your prediction was not correct, explain what was wrong. Also in loop.txt, explain what the output would be if we set x=127 instead.

  2. Code up the LibraryBook class from the exam. If you can't figure out how to get started, you may look at the classes written on the web page, but do not copy and paste existing code; you must type everything from scratch. Here is a tester class. When you compile your program and this tester and run the tester, you should get the following output:
    The current borrower of Taxi:  The Harry Chapin Story is David Liben-Nowell
    The current borrower of The Chicago Manual of Style is None
    When you're done, modify LibraryBookTester class to create an array of 40 library books. Name them "Encyclopedia Brittanica Volume 1", "Encyclopedia Brittanica Volume 2", etc. Borrow them all yourself. Print out their titles and current borrowers.

  3. Look at Broken.java and explain in broken.txt what's wrong with it (i.e., why it doesn't compile).

  4. When you're done with the above, think about how you would create a color histogram for an EzImage. Start to write one. (Start out with grayscale images; it's easier to think about.) Then start to write it. Clarification: you don't have to hand anything in for this question; this is meant to be something to think about if you want something to think about. Ask if you have questions!

By David Liben-Nowell.