EECS2030 Test 5F

Tuesday Nov 29 16:00-17:20
Lab 02
Version E


Instructions

There is one programming question and five other questions.

Instructions for submitting your programming question solution are given in the Programming question section. You may submit as many times as you want; your most recent submission will be the one recorded.

Instructions for submitting your answers to the short answer questions are given in the Short Answer Questions section. You may submit as many times as you want; your most recent submission will be the one recorded.

Programming question


Implement the Test5F class and provide the Javadoc comments needed to reproduce the API for the two required methods. You may omit the part of the comments that describe the examples (i.e., document the parameters, the return value (if any), and the method description excluding the examples).

Submit your program using the following command in a terminal (make sure you are in the directory containing your file Test5F.java):

submit 2030 test5F Test5F.java


package eecs2030.test5;

import java.util.List;

public class Test5F {

    /**
     * Returns a string where the case of each letter is flipped compared to the
     * corresponding letter in s. For example, if s is the string "abcDEF" then
     * flipCase(s) returns "ABCdef". The empty string is returned if s is equal
     * to the empty string.
     * 
     * @param s
     *            a non-null string consisting of English letter characters
     * @return a string where the case of each letter is flipped compared to the
     *         corresponding letter in s
     */
    public static String flipCase(String s) {
        if (s.length() == 0) {
            return "";
        }
        char c = s.charAt(0);
        if (Character.isLowerCase(c)) {
            return Character.toUpperCase(c) + Test5F.flipCase(s.substring(1));
        }
        return Character.toLowerCase(c) + Test5F.flipCase(s.substring(1));
    }

    /**
     * Removes all duplicated strings from a non-null and sorted list of strings
     * maintaining the sorted order of the strings. If the list contains unique
     * strings then the list remains unchanged. For example, if t is the list
     * ["a", "b", "c", "d"] then unique(t) would do nothing. If t is the list
     * ["w", "w", "w", "x", "y", "y", "z"] then unique(t) would modify t so that
     * it became the list ["w", "x", "y", "z"].
     * 
     * @param t
     *            a non-null and sorted list of strings that the method modifies
     *            so that it contains no duplicated strings (and the list remains in
     *            sorted order).
     */
    public static void unique(List<String> t) {
        if (t.size() < 2) {
            return;
        }
        String first = t.get(0);
        String second = t.get(1);
        if (first.equals(second)) {
            t.remove(0);
            Test5F.unique(t);
        }
        Test5F.unique(t.subList(1, t.size()));
    }

}

Other questions

Consider the following method:
  /**
   * Returns the string made up of the characters in s starting
   * from index idx and going to the end of s. For
   * example,
   * 
   * endOf("abcdef", 0) returns "abcdef"
   * endOf("abcdef", 1) returns "bcdef"
   * endOf("abcdef", 2) returns "cdef"
   * endOf("abcdef", 3) returns "def"
   * 
   * An empty string is returned if idx is greater than or equal to
   * the length of s.
   * 
   * @param s
   *          a string
   * @param idx
   *          the starting index in s
   * @pre. idx >= 0
   * @return the string made up of the characters in s starting from index idx
   *         and going to the end of s
   */
  public static String endOf(String s, int idx) {
    String result = "";
    if (idx >= s.length()) {
      result = "";
    }
    else {
      result = s.charAt(idx) + endOf(s, idx + 1);
    }
    return result;
  }
Question 1 (3 marks)

Prove that the above method is correct.

1 / 1
The base case, idx >= s.length(), returns a string of length zero which is correct because idx is an index beyond the end of the string s.

2 / 2
Assume that endOf(s, idx) correctly returns a string equal to the substring of s starting from index idx and going to the end of the string. The recursive case computes endOf(s, idx + 1) which is the equal to the correct string missing the first character; the returned string is formed by concatenating the first character and the string returned by endOf(s, idx + 1) which is the correct string.



Question 2 (3 marks)

Prove that the above method terminates.

1 / 1
Let the size of the problem endOf(s, idx) be equal to n = (s.length() - idx) or n = 0, which ever is greater. n is an integer value greater than or equal to zero.

2 / 2
The recursive invocation is endOf(s, idx + 1) which has size (s.length() - idx - 1) < n; therefore, the method terminates.



Question 3 (2 marks)

Let f : ℕ → ℕ be defined by f(n) = 5n4 + 3n + 2. Prove that f ∈ O(n4). You must state reasonable values of M and m (or F and M if you are using the notation from the notes) and then prove that the appropriate inequality is true.

You may write n4 as n^4 in your answer.

    5n^4 + 3n + 2
< 5n^4 + 3n^4 + 2n^4 for all n > 1
= 10n^4

Therefore, for M = 10 and m = 1 we have

| 5n^4 + 3n + 2 | < | 10n^4 | for n > 1



Question 4 (1 mark)

In our discussion of model-view-controller, we said that the controller responds to events. When are events generated in a graphical user interface application? (a one sentence answer should be sufficient)

Events are generated when the user interacts with the GUI (other answers are also possible).



Question 5 (1 mark)

In the linked list implementation discussed in the lectures, why is the Node class a private inner class of LinkedList? (a one or two sentence answer should be sufficient)

Node is a private inner class because it is an implementation detail of LinkedList that clients do not need to know about.