Computer Science 15-110 (Lecture 4), Spring 2010
Homework 1 Practice
Due:  Never.  This is not assigned work.


Read these instructions first!


  1. Short Answers
  2. Tracing
  3. Mystery Code
  4. Some Useful Arithmetic
  5. Console Programs
    1. thousandsDigit
    2. numberOfPoolBalls
    3. buyingFabric
  6. Painting Flags
    1. Belgium
    2. Burkina Faso

  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 each of these programs 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!
     
    1. // 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);
        }
      }

       
    2. // 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. 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's bonus section...  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. Console Programs
    1. thousandsDigit
      Write a program, Hw1PracticeThousandsDigit.java, that reads in a non-negative integer and prints just the thousands digit of that number.  If the number is less than 1000, its thousands digit is 0.  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 a non-negative integer: 12345
      ------------------------
      The thousands digit of 12345 is 2.

       
    2. numberOfPoolBalls
      A normal "rack" of pool balls has 5 rows and looks like this:

      Notice that each row has one more pool ball than the previous row, so we have 1+2+3+4+5 = 15 total pool balls.  With this in mind, write a program, Hw1PracticeNumberOfPoolBalls.java, that read in a non-negative integer number of rows for a rack of pool balls, and prints the total number of pool balls in that rack.  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 number of rows in a rack of pool balls: 7
      ------------------------
      A rack with 7 row(s) contains 28 ball(s).


      In this example, we are adding 1+2+3+4+5+6+7, which equals 28.

      Hint:  Remember to use well-named variables.  For example, you might use a variable named "rows" and another variable named "balls" or some such.

      Hint:  Remember that you may not use loops yet.  So you'll need a formula that adds the numbers from 1 to N (well, 1 to "rows", really).  Fortunately, we went over this formula in class last week.
       
    3. buyingFabric
      Fabric must be purchased in whole yards.  So if you require 24 inches (2 feet) of fabric, you still must purchase one yard, leaving a foot of scrap left over.  Write a program, Hw1PracticeBuyingFabric.java, that reads in the number of inches of fabric that are required, and prints out both the number of yards that must be purchased and the number of inches of scrap that will be left over.  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 inches of fabric required:  74
      74 inches equals 6 feet and 2 inches, requiring 3 yards of fabric, with 34 inches of scrap left over.

      Hint:  Be sure your program works when the input is a multiple of 36!
      Note: You may not use an "if" statement or any kind of conditional here.  You must solve this using a numeric expression!

       
  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

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

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

Carpe diem!