Computer Science 15-100 (Lecture 18), Spring 2009
Homework 1
Due:  Thu 22-Jan-2009 at 11:59pm (email copy) and at Friday's class/recitation (identical physical copy)
(no late submissions accepted).


Read these instructions first!


  1. Short Answers
  2. Tracing
  3. Mystery Code
  4. Duplicate Detector
  5. Painting Flags
  6. Bonus/Optional:  Short Answers

  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. (x % 1) always equals ____________.
       
    2. (x % x) always equals ____________.
       
    3. If x > 1, then (1 % x) always equals __________.
       
    4. If x < y, then (x / y) always equals ________.
       
    5. If x < y, then (x % y) always equals ________.
       
  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)





     

     
    b)





     

     
    c)




     

     
  3. Mystery Code
    In the written portion of your submission, state what the following program does in general, and in just a few words of plain English.

    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);
        System.out.print("Enter a 3-digit integer (between 100 and 999): ");
        // You may assume the user enters a number in the desired range.
        int x = scanner.nextInt();
        int a = x%10;
        int b = (x/10)%10;
        int c = x/100;
        int d = 100*a + 10*b + c;
        System.out.println(d);
      }
    }
  4. Duplicate Detector
    In the file Hw1DuplicateDetector.java, write a program that reads in three integer values and prints out 0 if there are any duplicate values (that is, fewer than three unique values were entered) and prints out 1 otherwise (that is, three unique values were entered).  Order should not matter.  So, for example, your program should print out 0 if the user enters 3, 2, 2  or 2, 3, 2, or 2, 2, 3, or 2, 2, 2.
     
  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. Tonga
    (file:  Hw1FlagOfTonga.java)
    (larger image with details)

    b.  Togo
    (file:  Hw1FlagOfTogo.java)
    (larger image with details)

    c.  Mauritania
    (file:  Hw1FlagOfMauritania.java)
    (larger image with details)

     
  6. Bonus/Optional:  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. Find a Java expression that always equals (x % y) but that does not use the % operator.
       
    2. If  (x / y) equals (y % x), then what do we know must be true about x and y?  Prove it (as best you can).
       
    3. If ((x – 1) % y) / (y – 1)  is non-zero, then what do we know must be true about x and y?  Again, prove it as best you can.  Hint:  remember that this expression uses integer division, and so truncates as needed!

Carpe diem!