Computer Science 15-100 (Sections T & U), Spring 2008
Homework 7a
Due:  Tue 26-Feb-2008 at 3:00pm (online submission) and at class (physical copy)
(no late submissions accepted).



  1. mostFrequentCharacter
    Write a method with this signature:
      public static char mostFrequentCharacter(String s) {
      }

    This method takes a (possibly-null) string s and returns the character that occurs the most frequently in the string.  If there is a tie, return the most-occurring character with the lowest Unicode value.  If the string is empty or null, return ((char)0).  While there are several ways to solve this problem, you must use this approach:  write a helper method that takes a string and a char and returns the number of times that char occurs in that string.  Then, in mostFrequentCharacter, loop over every character in the string and call your helper method for each character in turn.  Here is the start of a test method:
      public static void testMostFrequentCharacter() {
          verify(mostFrequentCharacter(null) == ((char)0));
          verify(mostFrequentCharacter("abcd") == 'a');
          verify(mostFrequentCharacter("abcd bcd cd") == 'c');
          verify(mostFrequentCharacter("a b c d") == ' ');
      }

     
  2. binaryToDecimal
    Write a method with this signature:
      public static int binaryToDecimal(String s) {
      }

    This method takes a (possibly-null) string s that represents an unsigned binary number and returns the decimal equivalent of that number as an int.  If the string is null, or empty, or contains any characters besides '0' or '1', the method should return -1.  As usual, do not worry about overflow.  While there are several ways to solve this problem, you must use an approach that requires a loop in which you inspect each digit of the binary number in turn as you construct the result.  Here is the start of a test method:
      public static void testBinaryToDecimal() {
          verify(binaryToDecimal(null) == -1);
          verify(binaryToDecimal("01001") == 9);
          verify(binaryToDecimal("11001") == 25);
      }

     
  3. decimalToBinary
    Write a method with this signature:
      public static String decimalToBinary(int n)
      }

    This method takes an int and, if it is non-negative, returns a string representing an unsigned binary number without leading 0's.  If the parameter is negative, the method should return null.  As usual, do not worry about overflow.  While there are several ways to solve this problem, you must use an approach that requires a loop in which you first find the largest power of 2 that is no greater than the number n, then you check each successively lower power of 2 adding a '1' to the result if you can subtract that power of 2 from n and adding a '0' if you cannot.  For example, if n equals 25.  The largest power of 2 not greater than 25 is 24 = 16.  Now we loop over the values 24=16, 23=8, 22=4, 21=2, and 20=1, as such:
      n   value   new digit   running result   new n
     25   24=16   1 (16<=25)       "1"         26 - 16 = 9
      9   23=8    1 ( 8<=9 )       "11"        9 - 8 = 1
      1   22=4    0 ( 4 > 1)       "110"       Still 1
      1   21=2    0 ( 2 > 1)       "1100"      Still 1
      1   20=1    1 ( 1<= 1)       "11001"     1 - 1 = 0 
    Hence we see that 25 in decimal is converted to the string "11001" which represents the same number in unsigned binary.  Here is the start of a test method:
      public static void testDecimalToBinary() {
          verify(decimalToBinary(-1) == null);
          verify(decimalToBinary(0).equals("0")); // this is the only result that can start with a '0'
          verify(decimalToBinary(9).equals("1001"));
          verify(decimalToBinary(25).equals("11001"));
      }

     
  4. sumOfNumbersWithThrees
    Write a method with this signature:
      public static int sumOfNumbersWithThrees(int n)
      }


    This method takes an int n and, if it is positive, returns the sum of the first n positive integers that contain a 3 (these numbers being 3, 13, 23, 30, 31, 32, ...).  If n is non-positive, the method returns -1.  Here is the start of a test method:
      public static void testSumOfNumbersWithThrees() {
          verify(sumOfNumbersWithThrees(0) == -1);
          verify(sumOfNumbersWithThrees(1) == 3);
          verify(sumOfNumbersWithThrees(2) == 16);  // 16 = 3 + 13
          verify(sumOfNumbersWithThrees(3) == 39);  // 39 = 3 + 13 + 23
          verify(sumOfNumbersWithThrees(4) == 69);  // 69 = 3 + 13 + 23 + 30
      }

Carpe diem!