Computer Science 15-100 (Lecture 18), Spring 2009
Homework 1 Practice
Due:  Never.  This is not assigned work.


  1. Short Answers
  2. Tracing
  3. Mystery Code
  4. Some Useful Arithmetic
  5. Painting Flags

  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. True or False:  If x and y are not equal, then one of (x/y) or (y/x) must equal zero?
       
  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.

    What does this program do (in general)?

    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!
    // Mystery code
    import java.util.Scanner;
    class MyCode {
      public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // You may assume the user enters two positive integers.
        System.out.print("Enter a positive integer: ");
        int x = scanner.nextInt(); 
        System.out.print("Enter another positive integer: ");
        int y = scanner.nextInt();
        int mystery = (x % 2) * (y % 2);
        System.out.println(mystery);
      }
    }
  4. Some Useful Arithmetic
    [ Hint:  While these may seem odd (and they are!), they may be of some use in solving the DuplicateDetector problem in hw1...  Also, note that in the near future, we can solve these more naturally using "conditional" statements or using the Math.signum method, among other approaches. ]
    Explain how each of the following accomplishes the stated goal.  Assume x is a possibly-negative integer.
     
    1. // goal:  set a to 1 if x is 0, or to a value >1 otherwise (if x>0 or x<0).
      int a = x*x + 1;
       
    2. // goal:  set b to 0 if x is 0 or 1, or to a value >1 otherwise (if x>1 or x<0).
      int b = x*x - x;
       
    3. // goal:  set c to 1 if x>=1, or to 0 if x<=0.
      int c = (3*x)/(3*x-1);
       
  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.  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 must deal with the "thin white stripe" that occurs between stripes when the screen size is not an even multiple of the stripe size.

    a. Belgium
      (without the "thin white stripe")
    (file:  Hw1PracticeFlagOfBelgium.java)
    (larger image with details)

    b.  Burkina Faso
    (file:  Hw1PracticeFlagOfBurkinaFaso.java)
    (larger image with details)

Carpe diem!