EECS2030 Test 5G


Programming question

Implement the Test5G class.


package test5;

import java.util.List;

public class Test5G {
  private Test5G() {
    // prevent instantiation
  }

  /**
   * Tests if all characters in a string are equal. Returns false
   * if s is the empty string.
   * 
   * @param s
   *          a string to test
   * @return true if all of the characters of s are equal, and
   * false otherwise
   */
  public static boolean allEqual(String s) {
    // NOTE: This method invokes the recursive method
    // allEqual(String s, char c) that you must implement
    boolean result = false;
    if (!s.isEmpty()) {
      result = Test5G.allEqual(s, s.charAt(0));
    }
    return result;
  }

  /**
   * Tests if all characters in a string s are equal to the given
   * character.
   * 
   * @param s
   *          a string
   * @pre. s.length() > 0
   * @param c
   *          a character
   * @return true if all characters in s are equal to
   *         c
   */
  static boolean allEqual(String s, char c) {
    boolean result = false;
    char first = s.charAt(0);
    if (s.length() == 1) {
      result = first == c;
    }
    else {
      char second = s.charAt(1);
      result = (first == second) && Test5G.allEqual(s.substring(1), c);
    }
    return result;
  }

  /**
   * Counts the number of elements in t that are strictly greater
   * than value
   * 
   * @param t
   *          a list of integers
   * @param value
   *          the value to compare against
   * @return the number of elements in t that are strictly greater
   *         than value
   */
  public static int countGreaterThan(List<Integer> t, int value) {
    int count = 0;
    if (t.isEmpty()) {
      count = 0;
    } 
    else {
      double first = t.get(0);
      if (first > value) {
        count = 1;
      }
      count += Test5G.countGreaterThan(t.subList(1, t.size()), value);
    }
    return count;
  }

}

Other questions

Consider the following method:
/**
 * Checks if the length of the given string is even.
 * A length of zero is considered even.
 * 
 * @param s a string
 * @return true if s.length() is an even number
 */
public static boolean isEven(String s) {
  boolean result = false;
  if (s.isEmpty()) {
    result = true;
  }
  else if (s.length() == 1) {
    result = false;
  }
  else {
    result = isEven(s.substring(2));
  }
  return result;
}
Question 1

Prove the above method correct.



Question 2

Prove that the above method terminates.



Question 3

Explain the role of the model, the view, and the controller in the model-view-controller design pattern.



Question 4

Consider the following recurrence relation:

T(n) = 2T(n / 2) + n

In plain English, state the meaning of the recurrence relation.



Question 5

Prove that n log(n) + 100n + 5 is an element of O(n log(n))