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


Read the instructions from hw3 first (changing "hw4" for "hw3" where appropriate).


  1. Prime Methods
    1. nthBalancedPrime
    2. nthEmirpsPrime
    3. nthHappyPrime
    4. nthSelfPrimeInBase10
  2. String Methods
    1. reverse
    2. isPalindrome
    3. isRotation
    4. simpleCalculator
  3. Kinds of Loops
  4. Bonus/Optional:  Painting Dominoes

  1. Prime Methods
    Write each of the following methods, along with appropriate test methods and helper methods.  For definitions of these terms, see the excellent List of Prime Number on Wikipedia.  In particular, this can help you in defining your test methods.

    Note  for these nthFoo problems, assume the parameter is a zero-based integer.  So, for example, nthPrime(0) would return 2, and nthPrime(1) would return 3.  Return -1 for any negative parameters (so nthPrime(-3) would return -1).  Use this same pattern for all the nthFoo problems.

    Also note  your nthFoo methods must compute
    the answers.  You may not hardcode the answers based on the lists on that Wikipedia page (even though you may, of course, hardcode your test methods based on those lists!).

    Also note: there are many other flavors of primes in that list that you should be comfortable implementing, say on a future quiz or test...
     
    1. nthBalancedPrime
    2. nthEmirpsPrime
    3. nthHappyPrime
      Hint:  when checking whether or not a number is happy, infinite loops always contain 4's, and non-infinite loops always end in 1's, so you can keep going until you either reach a 4 or a 1.
    4. nthSelfPrimeInBase10
       
  2. String Methods
    Write each of the following methods, along with appropriate test methods and helper methods.  Remember that you may only use the charAt and length methods from the String class (so, for example, the equals method from the String class is off-limits here).
    1. reverse
      This method takes a string and returns the same string in reverse (so reverse("abc") returns "cba").  The reverse of the empty string is the empty string, and the reverse of null should be null.
       
    2. isPalindrome
      This method takes a string and returns true if the string is a palindrome (the same forwards as backwards) and false otherwise.  Note that the empty string is a palindrome (if a degenerate one), and the null string is not a palindrome.  Your test should be case-insensitive, so "aA" is a palindrome, and it should skip spaces and punctuation, so "Madam, I'm Adam" is also a palindrome.
       
    3. isRotation
      This method takes two strings and returns true if one is a rotation of the other and false otherwise.  For example, "abcde" is a rotation of "cdeab".  Every non-null string (including the empty string) is a rotation of itself.  The null string is not a rotation of any string (not even another null string)..  This method is case-sensitive and treats spaces and punctuation the same as other characters (so "abcde" is not a rotation of "cd eab").
       
    4. simpleCalculator
      This method takes a string containing a simple arithmetic problem containing two possibly-negative integers separated by an operator (one of +, -, *, /, or %), and returns the integer result of the computation.  For example, simpleCalculator("-14/3") would return the integer value -4 (so it does integer division).  Do not worry about overflow or division by zero (it is acceptable to throw an exception in that case).
       
  3. Kinds of Loops
    In the file Hw4KindsOfLoops.java, choose any one of the preceding problems and solve (or "re-solve") it several times:  once using only "for" loops, once using only "while" loops, once using only "do-while" loops, and once using only infinite loops and break/continue (do not use break or continue in any of the other solutions).  Also, include a brief paragraph explaining the advantages of each loop construct in general, and then indicate which of the four approaches is best for this particular problem (whichever one you chose) and why so.
     
  4. Bonus/Optional:  paintingDominoes
    In the file Hw4PaintingDominoes.java, paint a set of 28 dominoes matching the following figure as closely as possible (while only painting rectangles and ovals):

    Note:  The same restrictions from above apply.  In particular, you may not use drawLine, but instead must paint the white lines using fillRect in some way.

    Note:
    You must use loops appropriately here, and you also should use well-chosen helper methods (say, for painting the dots)

    Note: On close inspection, you may notice that the corners of the dominoes are rounded slightly.  You are not responsible for this effect.

Carpe diem!