EECS2030E Test 1

Version C

You have 80 minutes to complete this test. This is a closed book test.


GETTING STARTED

  1. Start eclipse; this will take a couple of minutes because eclipse is being run for the first time using your test account.
  2. Download this project file.
  3. Import the test project by doing the following:
    1. Under the File menu choose Import...
    2. Under General choose Existing Projects into Workspace and press Next
    3. Click the Select archive file radio button, and click the Browse... button. You may have to wait about 10 seconds before the file browser appears.
    4. In the file browser that appears, navigate to your home directory.
    5. Select the file test1C.zip and click OK
    6. Click Finish.
  4. All of the files you need for this test should now appear in eclipse.
  5. Open a terminal. You will use this terminal to submit your work.
  6. Copy and paste the command cd workspace/Test1C/src/test1 into the terminal and press enter.

Resources


Question 1 (18 marks total)

Implement the utility class described by this API. You do not have to include javadoc comments.

To submit your programming question:

submit 2030L secEtest1C StringUtil.java

Question 2 (12 marks total)

A. 2 marks

In the declaration of a public static final constant what does the keyword final mean?

 

B. 3 marks

Consider the following (incomplete) implementation of shortest(String s, String t) from Question 1:

public static String shortest(String s, String t) {
    String result = "";

    /* rest of implementation not shown here */

    return result;
}

Suppose a client writes a main method that includes the following three lines of Java code:

String first = "coffee";
String second = "java";
String s = StringUtil.shortest(first, second);

The memory diagram illustrating the state of memory at the moment that the method is about to return its result back to the client is shown below. What suitable values of a, b, and c would complete the memory diagram?

             ---------------------
             |    main method    |
             ---------------------
first     100|         a?        |
second    102|         b?        |
s         104|         c?        |
             ---------------------
             |                   |
             |                   |
             ---------------------
          300|   String object   |
             ---------------------
             |                   |
             ---------------------
          400|   String object   |
             --------------------- 
             |                   |
             ---------------------
             |  shortest method  |
             ---------------------
       s  800|        300a       |
       t  802|        400a       |
  result  804|     not shown     |
             ---------------------
             |                   |

 

C. 3 marks

Identify the 3 errors in the Javadoc shown below for the method shortest(String s, String t) from Question 1.

/**
 * Returns the shortest of the two strings s and t. For example,
 * <code>shortest("python", "java")</code> returns the string
 * <code>"java"</code> because the string <code>"java"</code> is shorter
 * than the string <code>"python"</code>. 
 * 
 * @param t 
 *            a non-null string
 * @throws IllegalArgumentException 
 *            if s and t have the same length
 * @pre. s.length() != t.length()
 */
public static String shortest(String s, String t)

 

D. 4 marks

Consider the following (partial) implementation of the method distance(String s, String t) from Question 1:

public static int distance(String s, String t) {
    int result = -1;
    if (s.length() > 0) {
        // Assume that there is some code here (not shown) that attempts
        // to implement the method correctly.
    }
    return result;
}
    

(a) Is there a test case that would reveal an error in the implementation shown above? If so, what is it?

(b) When writing a unit test for distance(String s, String t), should we use the input strings s = "java" and t = "coffee"? Why or why not?

 

To submit your written questions:

submit 2030L secEtest1C answers.txt