Computer Science 15-110, Spring 2010
Recitation for Week #5


  1. Review Hw4
  2. CA-Directed Lab
    1. mostFrequentLetter
    2. isHappyNumber
    3. nthHappyNumber
    4. isHappyPrime
    5. nthHappyPrime
  3. Student-Directed (Collaborative) Lab
    1. isRotation
    2. isEmirpsPrime
    3. nthEmirpsPrime
    4. More Practice

  1. Review Hw4
    CA's will briefly review highlights of hw4.  Students needing deeper review of the material should set up a separate meeting with their CA or instructor.
     
  2. CA-Directed Lab
    1. mostFrequentLetter
      Write the method mostFrequentLetter that takes a string and returns the most-frequently-occurring letter in the given string, with ties going to the first character alphabetically. Counts are case-insensitive, so 'a' and 'A' both count as an 'A', and the result is an uppercase character. If the string is empty or null, the result should be the char value 0.  Note that to receive full credit, you must write at least one well-chosen helper method for this method.

      Here is a test method for you:
        public static void testMostFrequentLetter() {
          System.out.print("Testing mostFrequentLetter... ");
          assert(mostFrequentLetter("STUV") == 'S');
          assert(mostFrequentLetter("tuvw") == 'T');
          assert(mostFrequentLetter("UTSR") == 'R');
          assert(mostFrequentLetter("This is a test") == 'S');
          assert(mostFrequentLetter("") == ((char)0));
          assert(mostFrequentLetter(null) == ((char)0));
          System.out.println("Passed all tests!");
        }
    2. isHappyNumber
      A happy number is defined by the following process.  Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.  Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers.

      Based on this, write a method, isHappyNumber, that takes an integer and returns true if it is a happy number and false otherwise.  Note that non-positive numbers are not happy.

      Hint #1:  Interestingly, every number that ends in a cycle ends in the SAME cycle.  You will probably need this hint to solve this problem.

      Hint #2:  For more on this, including a list of happy numbers below 500, see the Wikipedia page on Happy Numbers.

      Here is a test method for you:
        public static void testIsHappyNumber() {
          System.out.print("Testing isHappyNumber... ");
          assert(isHappyNumber(-1) == false);
          assert(isHappyNumber(0) == false);
          assert(isHappyNumber(1) == true);
          assert(isHappyNumber(10) == true);
          assert(isHappyNumber(12) == false);
          assert(isHappyNumber(13) == true);
          assert(isHappyNumber(495) == false);
          assert(isHappyNumber(496) == true);
          System.out.println("Passed all tests!");
        }
    3. nthHappyNumber
      Write the method nthHappyNumber that takes an int n and returns the nth Happy Number (where the 0th happy number is 1).  If n is negative, return -1.

      Here is a test method for you:

        public static void testNthHappyNumber() {
          System.out.print("Testing nthHappyNumber... ");
          assert(nthHappyNumber(-5) == -1);
          assert(nthHappyNumber(0) == 1);
          assert(nthHappyNumber(1) == 7);
          assert(nthHappyNumber(2) == 10);
          assert(nthHappyNumber(3) == 13);
          assert(nthHappyNumber(4) == 19);
          assert(nthHappyNumber(5) == 23);
          assert(nthHappyNumber(10) == 49);
          System.out.println("Passed all tests!");
        }
    4. isHappyPrime
      A "happy prime" is a number that is both happy and prime (see the Wikipedia page on Happy Primes for details).  Write a method, isHappyPrime, that takes an int and returns true if it is a happy prime and false otherwise.

      Here is a test method for you:

        public static void testIsHappyPrime() {
          System.out.print("Testing isHappyPrime... ");
          assert(isHappyPrime(-1) == false);
          assert(isHappyPrime(5) == false);
          assert(isHappyPrime(7) == true);
          assert(isHappyPrime(13) == true);
          assert(isHappyPrime(14) == false);
          assert(isHappyPrime(23) == true);
          assert(isHappyPrime(73) == false);
          assert(isHappyPrime(79) == true);
          System.out.println("Passed all tests!");
        }
    5. nthHappyPrime
      Write the method nthHappyPrime that takes an int n and returns the nth Happy Prime (where the 0th happy prime is 7).  If n is negative, return -1.

      Here is a test method for you:

        public static void testNthHappyPrime() {
          System.out.print("Testing nthHappyPrime... ");
          assert(nthHappyPrime(-5) == -1);
          assert(nthHappyPrime(0) == 7);
          assert(nthHappyPrime(1) == 13);
          assert(nthHappyPrime(2) == 19);
          assert(nthHappyPrime(3) == 23);
          assert(nthHappyPrime(4) == 31);
          assert(nthHappyPrime(5) == 79);
          assert(nthHappyPrime(10) == 167);
          System.out.println("Passed all tests!");
        }
  3. Student-Directed (Collaborative) Lab
    1. isRotation
      Write a method named isRotation that takes two strings and returns true if each string is a rotation of the other, and false otherwise.  The rotations of "abcd" are:  "abcd" (so a string is a rotation of itself!), "dabc", "cdab", and "bcda".  The empty string is a rotation of itself, and the null string is not a rotation of any string.  Do not create any new Strings in your solution -- just compare characters from the two Strings.  Hint:  you may want to create a helper method that checks if the two strings are a rotation with a given offset, then call this helper method repeatedly with every legal offset.
       
    2. isEmirpsPrime
      As the Wikipedia page on emirps primes indicates:  "An emirp (prime spelled backwards) is a prime number that results in a different prime when its digits are reversed. [...] The sequence of emirps begins 13, 17, 31, 37, 71, 73, 79, 97, 107, 113, 149, 157.."  Based on this, write a method named isEmirpsPrime that takes an int and returns true if it is an emirp prime and false otherwise.
       
    3. nthEmirpsPrime
      Following the pattern we used for nthPrime and nthHappyPrime, write a method named nthEmirpsPrime that takes an int n and uses your isEmirpsPrime method to compute the nth emirps prime (where the 0th emirps prime is 13).  If n is negative, return -1.
       
    4. More Practice
      Time permitting, CA's will offer more problems for students to work on (say, using nested loops to create interesting 2d graphical patterns, though this is not a requirement).

carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem