EECS2030E Test 1

Version B

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. 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 test1B.zip and click OK
    6. Click Finish.
  3. All of the files you need for this test should now appear in eclipse.
  4. Open a terminal. You will use this terminal to submit your work.
  5. Copy and paste the command cd workspace/Test1B/src/test1 into the terminal and press enter.

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 secEtest1B TimeUtil.java

Question 2 (12 marks total)

A. 2 marks

What is the definition of term "method postcondition"?

 

B. 3 marks

Consider the following class having only one method:

public class StringUtil {

    public static String repeat(String s, int n) {
        // Returns a new string made by repeating the string s
        // a total of n times.

        if (n < 0) {
            n = -n;
        }
        String result = /* implementation not shown */;
        return result;
    }
}

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

String myString = "abc";
int m = -100;
String s = StringUtil.repeat(myString, m);

The memory diagram illustrating the state of memory when the method reaches the return statement is shown below. What suitable values of a, b, and c would complete the memory diagram?

             ---------------------
             |    main method    |
             ---------------------
myString  100|     not shown     |
       m  102|        a?         |
       s  104|        c?         |
             ---------------------
             |                   |
             |                   |
             ---------------------
             |   String object   |
             ---------------------
             |                   |
             |                   |
             ---------------------
             |  repeat method    |
             ---------------------
       s  800|     not shown     |
       n  802|         b?        |
  result  804|        900a       |
             ---------------------
             |                   |
             ---------------------
          900|   String object   |
             ---------------------
             |                   |
             |                   |

 

C. 3 marks

Complete the Javadoc necessary to exactly reproduce the API documentation for the method median(List<Integer> dice) from Question 2. (NOTE: the <pre> tags are html markup, not Javadoc tags; you can ignore them for the purposes of this question.)

/**
 * 
 * 
 * 
 * 
 * <pre>
 * totalDays(2000, 2000)  returns  366        (Jan 1, 2000 to Dec 31, 2000)
 * totalDays(2001, 2002)  returns  365 + 365  (Jan 1, 2001 to Dec 31, 2002)
 * totalDays(2001, 2004)  returns  365 + 365 + 365 + 366  (Jan 1, 2001 to Dec 31, 2004)
 * </pre>
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 */
public static int totalDays(int startYear, int endYear)

 

D. 4 marks

Consider the following test cases for the method mostFrequent(List<Integer> years) from Question 2.

Test ID   Input list Expected result
1. [] IllegalArgumentException
2. [1999, 1999, 2000] 1999
3. [2015, 2015, 2016, 2016, 2016, 2016, 2000]     2016

(a) For each of the 3 test cases, use one sentence to state whether or not the test case should be used and explain why or why not.

(b) There is an important test case that is missing. What is it and why do you think that it is important?

 

To submit your written questions:

submit 2030L secEtest1B answers.txt