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


Read these instructions first!



Programming guidelines:

  1. Short Answers
  2. Mystery Code
  3. Non-Graphical Programming Problems
    1. factorial
    2. gcd
    3. lcm
    4. indexOf
    5. lastIndexOf
    6. isSubstring
    7. nthHappyNumber
  4. Painting Flags (United States)
  5. Bonus/Optional:  wordsearchContains

  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 possibly-negative integer values, and "a", "b", and "c" are boolean values, and ignore any errors (such as dividing by zero):
     
    1. The following code does not loop forever because of ____________.
        int i=0;
        while (i <= 0)
           i--
      ;
       
    2. If ((Math.pow(x,2) < 15) && (Math.abs(x) > x)) is true, then x must be between  ____________ and ___________ (inclusive).
       
    3. Fill in the blank with one of “always”, “sometimes”, or “never”.  Be sure to show your work, using a truth table to prove your answer:
      ((a || b) ? (!a && b) : (a || !b))   ________________  equals (!a)
       
  2. Mystery Code
    In the written portion of your submission, state what each of the following methods do in general, and in just a few words of plain English.

    a)
    public static boolean f(int x) {
      assert(x > 0);
      boolean b = false;
      while (x > 0) {
        if (x % 10 == 3)
          b = !b;
        x = x/10;
      }
      return b;
    }
    b)
    public static String g(int x) {
      if (x == 0) return "0";
      String s = "";
      int y = Math.abs(x);
      while (y > 0) {
        s = (y%2) + s;
        y /= 2;
      }
      if (x < 0) s = "-" + s;
      return s;
    }
  3. Non-Graphical Programming Problems
    Do the following problems as defined by their comments in the file Lab4.java.  Read the comments and review the test cases carefully!
    1. factorial
    2. gcd
    3. lcm
    4. indexOf
    5. lastIndexOf
    6. isSubstring
    7. nthHappyNumber
       
  4. Painting Flags (United States)
    Paint the United States flag using loops for the stripes and the stars (which will be painted as 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: 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).

    a. United States
    (file:  Lab4FlagOfUnitedStates.java)
    (larger image with details)
     
  5. Bonus/Optional:  wordsearchContains
    Write the following method:
       public static boolean wordsearchContains(String wordsearch, String word)
    This method takes a two strings, the first representing a wordsearch (stored in a single string!), and returns true if the second string occurs in that wordsearch and false otherwise.

    To represent a wordsearch as a string, we simply store each row concatenated to the next where we separate the rows with a | (that is, a vertical bar, or a pipe).  So, consider this wordsearch:

    t a c w
    n o o c
    d o g x

    The rows are: "tacw", "nooc", and "dogx", so we represent the entire wordsearch as: "tacw|nooc|dogx".
    Note that this wordsearch contains:

    cat
    cod
    coon
    dog
    ox


    You are guaranteed the wordsearch contains only lowercase letters, and that the rows are all the same positive length (you do not need to handle other cases in any way).

    Here is a test method for you:
      public static void testWordsearchContains() {
        System.out.print("Testing wordsearchContains... ");
        // These tests cover this wordsearch:
        //   t a c w
        //   n o o c
        //   d o g x
        assert(wordsearchContains("tacw|nooc|dogx", "cat"));
        assert(wordsearchContains("tacw|nooc|dogx", "cod"));
        assert(wordsearchContains("tacw|nooc|dogx", "coon"));
        assert(wordsearchContains("tacw|nooc|dogx", "dog"));
        assert(wordsearchContains("tacw|nooc|dogx", "ox"));
        assert(!wordsearchContains("tacw|nooc|dogx", "caw"));
        assert(!wordsearchContains("tacw|nooc|dogx", "cow"));
        assert(!wordsearchContains("tacw|nooc|dogx", "con"));
        assert(!wordsearchContains("tacw|nooc|dogx", "dogs"));
        assert(!wordsearchContains("tacw|nooc|dogx", "ax"));
        System.out.println("Passed all tests!");
      }
    Note:  This approach of representing a 2-dimensional board in a 1-dimensional string is not a very good idea.  Soon, we will learn about other ways we can more naturally represent this data, such as in a 2-dimensional array.

Carpe diem!