Computer Science APEA 15-100, Summer 2009
Homework 7
Due:  Fri 10-Jul-2009 at 8:59am (email copy only)
(no late submissions accepted).


Read these instructions first!


  1. Reading
  2. Finish Lab 7
  3. ch5Exercises
  4. isInteger
  5. nthPerfectNumber
  6. Flag of Greece

  1. Reading
    Continue reading the material for the next quiz, which covers 3.1, 3.2, 3.4, 3.5, 3.6 (just printf), 3.8, 5.1, 5,2, 5.3, 5.5, 5.7, and 5.8.  You will have time tomorrow and over the weekend to finish this reading.
     
  2. Finish Lab 7
    Finish all the examples from lab 7.  Include the solutions in your submission, all in one file named Lab7.java.  Note: the lab collaboration policy continues to apply to the lab problems.  So you can collaborate at will to solve these problems.  This only applies to the lab problems -- all other problems in this assignment are subject to the usual homework collaboration policy.
     
  3. ch5Exercises
    Do the following Exercises from Chapter 5 (pp 281-283):
    Exercises 5.7,  5.8,  5.9,  5.10
     
  4. isInteger
    Write the following method:
       public static boolean isInteger(String s)
    This method takes a (possibly-null) string s and returns true if and only if that string represents an integer value.  That is, if the string is non-null, non-empty, and only contains digits (0 through 9), with an optional leading + or - sign.  Note that just a sign is not allowed, nor two or more signs, so neither "+" nor "++3" are legal.  Also, extra leading zeros are allowed, so "00032" is legal.  Finally, do not worry about overflow, so "12345678987654321" is legal.  Write your own test method.
     
  5. nthPerfectNumber
    Write the following method:
       public static int nthPerfectNumber(int n)
    This method takes an integer "n" and returns the nth perfect number, where a perfect number is an integer that is the sum of its proper divisors (that is, the numbers less than it that evenly divide it). For example, the proper divisors of 6 are 1, 2, and 3, and 6 = 1 + 2 + 3, so 6 is a perfect number.  Conversely, the proper divisors of 8 are 1, 2, and 4, and 8 != 1 + 2 + 4, so 8 is not a perfect number.  If n is less than 1, the method should return -1.  And, as usual, do not worry about overflow.  Here is a test method for you:
      public static void testNthPerfectNumber() {
        System.out.print("Testing nthPerfectNumber... ");
        assert(nthPerfectNumber(-5) == -1);
        assert(nthPerfectNumber(0) == -1);
        assert(nthPerfectNumber(1) == 6);
        assert(nthPerfectNumber(2) == 28);
        assert(nthPerfectNumber(3) == 496);
        System.out.println("Passed all tests!");
      }

    Helper Method:  unspecified (but required!)

    Again, in writing your nthPerfectNumber method, you must use a well-conceived helper method.  You must also include a test method for your helper method (and, again, you will be graded, in part, on the quality of the test cases in that test method).

    Note:  As should be apparent, decomposition into helper methods is a great way to design your programs.  You are expected to use this approach in all future programming assignments.
     

  6. Flag of Greece
    In the file Hw7FlagOfGreece.java, write a program that displays the flag of Greece::
      (larger image with details)
    Note:  Your program must use one well-chosen loop to draw the wide stripes.  However, you do not need to use a loop to draw any portion of the top-left inset.  You must also handle the "thin white line" problem -- remember, for each stripe, compute its top edge and bottom edge the same way, then take the difference for the stripe height.  You must also color match.

Carpe diem!