Computer Science 15-110 (Lecture 4), Spring 2010
Homework 1
Due:  Thu 21-Jan-2009 at 10pm (email copy to koz) (no late submissions accepted).
Hw1 Submission Coordinator:  koz


Read these instructions first!


  1. Attend Recitation
  2. Short Answers
  3. Tracing
  4. Mystery Code
  5. Console Programs
    1. nearestBusStop
    2. lineIntersection
  6. Painting Flags
    1. Central African Republic
    2. Netherlands Antilles
  7. Bonus/Optional:  Short Answers
  8. Bonus/Optional:  Duplicate Detector

  1. Attend Recitation
    This is worth 10% of the hw.  Students who attended recitation #1 receive this credit.  Those who did not attend do not receive credit.  Attending recitation is a required element of this course.  Please be sure to attend all future recitations (whether or not credit is given).  Ride the bike!!!
     
  2. 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 ________.
       
  3. 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)




     

     

     

  4. 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 4-digit integer (between 1000 and 9999): ");
        // You may assume the user enters a number in the desired range.
        int x = scanner.nextInt();
        int a = x%10;
        System.out.println(1000*a + x/10);
      }
    }
  5. Console Programs
    1. nearestBusStop
      Write a program, Hw1NearestBusStop.java, that reads a street number (which you may assume is positive), and prints the street number of the nearest bus stop, where buses stop on streets that are multiples of 8 (8th, 16th, 24th, etc). So we see:
      * For 11th street, the nearest bus stop is 8th street.
      * For 13th street, the nearest bus stop is 16th street.
      * For 12th street, however, both 8th street and 16th street are equally far, but we prefer the lower bus stop, which is 8th street.
      You can assume there is a bus stop on 0th street.  Your program must match the following output exactly (where the user input is underlined, and therefore can change each time you run the program):

      Enter the street number: 12
      ------------------------
      The nearest bus stop to street 12 is at street 8.


      Remember: do not use conditionals, loops, arrays, etc. This can be done (in one line of code!) using only what we have covered Week #1's notes (in fact, using just addition, division and multiplication of integers). 
       
    2. lineIntersection
      Write a program, Hw1LineIntersection.java, that reads 4 integers:  the slope m1 and y-intercept b1 of line1, then the slope m2 and y-intercept b2 of line2.  The program should print the (x,y) coordinate of the point of intersection of the two lines.  You may assume the two lines intersect at a point where both x and and y are integers.  Your program must match the following output exactly (where the user input is underlined, and therefore can change each time you run the program):

      Enter two lines that intersect at a point (x,y)
      where both x and y are integers:
      Enter m1: 5
      Enter b1: 2
      Enter m2: 3
      Enter b2: 4
      ------------------------
      These lines intersect at (1,7).

      In this example, we are intersecting the lines y=5x+2 and y=3x+4 (see?).  These lines intersect at the point (1,7).

      Hint:  To write this program, you will have to do some simple algebra to find the point of intersection of the equations y1=m1x+b1 and y2=m2x+b2.  Since the y value must be the same when these lines intersect (right?), you can simply set these two equations equal to each other (so y1=y2, that is, m1x+b1 = m2x+b2) and solve for x.  Then, plug this value of x into either equation to find y.
       

  6. 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, and you may not draw outside of the visible window.  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:
     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.

    Note:
      You do not have to be "pixel perfect" in positioning the stars, but you must be "very close", even as the window size changes.  In particular, you have to maintain the obvious alignment properties.  For example, in the first flag below (Central African Republic), the star must remain vertically centered in the blue stripe.  In the second flag below (Netherlands Antilles), the top star must be centered horizontally, the middle two stars must be centered vertically and lie entirely outside the red stripe, and the lower two stars must be left-flush and right-flush respectively with the red stripe.

    a. Central African Republic
    (file:  Hw1FlagOfTheCAR.java)
    Note:  Due to a DrJava bug, it cannot properly run files with names as long long as "Hw1FlagOfTheCentralAfricanRepublic.java" (even though they are legal Java file names, and other IDE's, like JCreator, can run this just fine).  Thus, we have to abbreviate the name as we have here.
    (larger image with details)

    b.  Netherlands Antilles
    (file:  Hw1FlagOfTheNetherlandsAntilles.java)
    (larger image with details)
     
  7. 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!
       
  8. Bonus/Optional:  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.

Carpe diem!