Computer Science 15-110, Lecture 9 (Sections M-Q), Fall 2009
Lab 2
Due:  Mon 7-Sep-2009 at 11:59pm (email copy to your CA)
(no late submissions accepted).


Read these instructions first!



Programming guidelines:

  1. Short Answers
  2. Tracing
  3. Mystery Code
  4. Non-Graphics Methods
    1. nearestBusStop
    2. xIntercept
    3. xInterceptOfParabola
    4. isPerfectSquare
    5. toUpperCase
  5. Painting Flags
    1. Benin
    2. The Central African Republic

  1. Short Answers
    In the written portion of your submission, answer each of the following questions.  Be very brief, but show your work where appropriate.
    Assume that "x" and "y" are non-negative integer values, and ignore any errors (such as dividing by zero):
     
    1. If (x % 2) equals 1, then we know that x is ____________.
       
    2. True or False:   (x % y % y) always equals (x % y)?
       
    3. For this part, assume that "x" and "y" are boolean values (though these are not very good names for boolean values):

      Fill in the blanks with one of “always”, “sometimes”, or “never”.  Be sure to show your work, using truth tables to prove your answer.  The first problem is done for you to demonstrate.
      1. !(x || y)   ___sometimes____  equals (x && y)

            x    y    (x || y)   !(x || y)  (x && y)
            T    T        T           F        T
            T    F        T           F        F   
            F    T        T           F        F   
            F    F        F           T        F


        Note:  The truth table clearly shows that the two expressions are equal when x is true and y is false or when x is false and y is true, but are unequal when both x and y are false or both are true.

        Note:  the column labeled (x || y) is not part of the final answer, but is still important to include.  It is a “helper column”, and it simplifies the computation of the next column.  You should include helper columns as you deem necessary.
      2. (!!x)   ________________  equals x
      3. (x && !y)   ________________  equals (!x || !y)
      4. (x && !y)   ________________  equals !(x || !y)
  2. Tracing
    In the written portion of your submission, indicate what each of the following will print or (if it is graphical) draw.  Do not run these programs.  Figure this out by hand.  Remember to show your work.
     
    a)




     
      public static void main(String[] args) {
        int x = 5, y = 2, z = 3;
        System.out.println(x + y * z);
        System.out.println(x % y % z);
        System.out.println(10 + 9 / x * z - y);
      }
    b)



     
      public void paint(Graphics page) {
        int c = getWidth(), d = getHeight();
        page.fillRect(c/2-10, 50, 20, 100);
        page.fillOval(c/4,d*3/4,c*3/4, d/4);
      }

     

  3. Mystery Code
    In the written portion of your submission, answer the following question in general, and in just a few words of plain English.  The first problem is done for you to demonstrate.

    Hint:  while you should definitely trace the code in this problem by hand to make some sense out of it, you may also wish to actually test the code in a compiler with samples of your choosing, at least to verify that it works as you think it does!

    Note:  these methods may seem a bit confusing, and in fact they are!  There are some much easier, clearer, and more sensible ways to do the same thing.  This is to test your understanding of data and expressions, not to teach the best way to write these methods.
     

    1. What does this method do (in general)?

        public static boolean mysteryMethodA(int x, int y) {
          return ((x % 2) * (y % 2) == 0);
        }


      Answer:  The product is zero if either (x % 2) is zero and/or (y % 2) is 0.  (x%2) is 0 if x is even.  (y%2) is zero if y is even.  And so, the method checks if x and/or y are even.

      Note:  you will receive zero credit for simply explaining, step-by-step, what the method does.  For example, this response would receive zero credit:  "The method takes the remainder when x is divided by 2 and multiplies it by the remainder when y is divided by 2, and then the method returns true if this product is zero and false otherwise."  This is all entirely correct, but misses the point of the question, which is to abstract away from the details and determine the essence of the computation.  Your final answer (the underlined portion above), must be in just a few words of plain English.
       
    2. What does this method do (in general)?

        public static boolean mysteryMethodB(int x, int y) {
          return (((x == 0) && (y == 0)) ||
                  ((x != 0) && (y != 0) && ((x/y) + (y/x) == 2)));
        }


      Hint:  there are several cases to consider!  What if x>y?  x==y?  x<y?
       
    3. What does this method do (in general)?

      public static int mysteryMethodC(int x, int y) {
        int a = x - y;
        int b = 1 - (3*a)/(3*a-1);
        return b*x + (1-b)*y;
       }

       
  4. Non-Graphics Methods
    Starting from Lab2.java, write the unfinished methods.  Note that we may include some tests in our autograder besides those included in the provided test methods.
     
  5. Painting Flags
    Paint each of the following flags (one flag per Java file) using only filled rectangles and filled ovals.  You should use custom colors, matching the colors in the flags as closely as possible.  Also, your flags may not be fixed-sized, but rather they must entirely fill the window, even when the window is resized, leaving a 10-pixel gray margin on all sides (you are not responsible for dealing with window sizes below 50x50).  While the window's size may change, you may assume the window will be roughly "flag-shaped" -- you will not be graded on how your flags appear in, say, a tall thin window (which is not at all "flag-shaped").

    Note: All these flag images are from the very informational CIA World Factbook, which includes a flags-of-the-world page

    Note: As we are limited to just rectangles and ovals this week, you should replace any stars in flags with ovals (of the same size, location, and color).

    Note:
      This week, you are not responsible for the "thin white stripe" that occurs between stripes when the screen size is not an even multiple of the stripe size.  Ask your CA for details.

    a. Benin
    (file:  Lab2FlagOfBenin.java)
    (larger image with details)

    b.  The Central African Republic
    (file:  Lab2FlagOfTheCentralAfricanRepublic.java)
    (larger image with details)

Carpe diem!