Computer Science 15-100 (Sections T & U), Fall 2007
Homework 6
Due:  Fri 12-Oct-2007 at 8:30am (email copy) and at recitation (physical copy)



  1. REMINDER:  Though not part of this assignment, Exercises 5.14 to 5.35 and Programming Projects 5.1 to 5.30 are excellent problems to study for the upcoming test!
     
  2. In this problem, you will write static versions of some of the methods included in the String class.  For this reason, you may only use two methods from the String class:  String.length() and String.charAt().
     
    1. Write a method with this signature:
           public static int compareTo(String a, String b)
      This method takes two non-null strings and compares them lexicographically (alphabetically, but using Unicode values), returning a negative number if String a precedes String b, zero if they are equal, and a positive number if String a follows String b.  First, the method tries to find the smallest value of i such that the ith character of String a differs from the ith character of String b.  In this case, it returns the difference of these characters' Unicode values.  If no such character exists, then there are two cases:  if the strings are of equal length, then they are in fact equal, so the method returns 0.  Otherwise, one string is a prefix of the other, so the method returns the difference in the lengths of the strings.  Note that all capital letters precede all lowercase letters -- so, in particular, "Zoo" precedes "antelope".
       
    2. Write a method with this signature:
           public static int lastIndexOf(String s, char c, int fromIndex)
      This method takes a possibly-null string, a character, and an integer "fromIndex", and returns the largest value k such that (k <= fromIndex) and the given character equals the character at index k in the given string.  If no such character exists, the method returns -1.  Note that null strings contain no characters (so the return value would be -1 in all such cases).  Also, note that there is no restriction on the value of fromIndex. If it is greater than or equal to the length of this string, it has the same effect as if it were equal to one less than the length of this string: this entire string may be searched. If it is negative, it has the same effect as if it were -1: -1 is returned.
       
    3. Write a method with this signature:
           public static String toLowerCase(String s)
      This method takes a possibly-null string and returns a possibly-new String where each uppercase letter (from 'A' to 'Z') is replaced by its equivalent lowercase letter (from 'a' to 'z').  If the String s does not contain any uppercase letters, or if it is null, then it is returned as-is (no new string is created).
       
    4. Write a method with this signature:
           public static String trim(String s)
      This method takes a possibly-null string and returns a possibly-new String where the leading and trailing whitespace is omitted, where we will take "whitespace" to mean all spaces (' '), tabs ('\t'), newlines ('\n'), returns ('\r'), or form feeds ('\f').  If the String s does not contain any leading or trailing whitespace, or if it is null, then it is returned as-is (no new string is created).
       
  3. In this problem, you will write static versions of some of the methods included in the Integer and Double class.  For this reason, you may not use any methods from those classes, and you may only use two methods from the String class:  String.length(), String.charAt(), and String.equals().
     
    1. Write a method with this signature:
           public static int parseInt(String s)
      This method takes a possibly-null string and parses it as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be a minus sign '-' to indicate a negative value.  The resulting integer is returned.  If the string is null, or if it does not abide by the spec in some other way, then the value -1 is returned (note that the real parseInt method throws an exception in these cases, but seeing as we have not yet learned about exceptions, we'll just return -1).
       
    2. Write a method with this signature:
           public static double parseDouble(String s)
      This method takes a possibly-null string and parses it as a signed double floating-point number.  First, the string is trimmed of its leading and trailing whitespace.  Then there are numerous cases to consider.  First, the string can be "NaN", meaning "Not a Number", in which case the value Double.NaN is returned.  Second, the string can be "Infinity" or "+Infinity", in which case Double.POSITIVE_INFINITY is returned.  Or it can be "-Infinity", in which case Double.NEGATIVE_INFINITY is returned.  Finally, the string can begin with an optional plus ('+') or minus ('-') sign, followed by zero or more decimal digits, followed by an optional decimal point ('.') followed again by zero or more decimal digits.  Note that the number must contain at least one digit, though it can be on either side of the optional decimal point.  If the string is null, or if it does not abide by the spec in some other way, then the value -1 is returned (note that the real parseDouble method throws an exception in these cases, but seeing as we have not yet learned about exceptions, we'll just return -1).
      Note:  The spec here is much simpler than the real one.  For example, here we omit scientific notation and hexadecimal digits, among other simplifications.
       
  4. Goldbach's Conjecture states that every even number greater than 2 is the sum of two prime numbers.  For example, 4 equals 2+2, 6 equals 3+3, 8 equals 3+5, and 10 equals 3+7 (and 5+5, in fact, so we see there may be more than one such sum for any given even number).  While mathematicians have used computers to verify this conjecture for evens into the trillions and beyond, so it sure seems true, nobody has been able to prove it is always true.  It remains one of the great outstanding problems in mathematics.

    Write a method with this signature:
         public static int goldbachPrime(int n)
    This method takes an even integer n, where n is greater than 2, and returns the smallest integer p such that p is prime and (n - p) is prime.  Since p + (n - p) equals n, this method really confirms Goldbach's Conjecture for its argument n.  Note that you will need a loop to find the first prime, p, but then you do not need a loop for the second prime -- for any given prime p, just check if (n - p) is prime, too.
     
  5. Two numbers are twin primes if both numbers are prime and their difference is 2.  So, for example, 3 and 5 are twin primes, as are 5 and 7, and 11 and 13.  Here is a table of the first several twin primes:
         n     nth twin primes
         1         (3, 5)
         2         (5, 7)
         3         (11, 13)
         4         (17, 19)
         5         (29, 31)

    Write a method with this signature:
         public static int nthTwinPrime(int n)
    This method takes a positive integer n and returns the first of the nth twin primes.  So, in n == 1, the 1st twin primes are (3,5), so the method would return 3.  If n==5, the 5th twin primes are (29,31) so the method would return 29.
     

Carpe diem!