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

Question 2 (12 marks total)

A. 2 marks

What is the definition of term "method postcondition"?

A method postcondition is a condition that the method promises will be true after the method finishes running.

Grading scheme: 2 marks

 

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   |
             ---------------------
             |                   |
             |                   |
    a = 100   (the value of the argument m)
    b = 100   (the value of the parameter n)
    c = 900a   (the address of the returned string)

Grading scheme: 3 marks, one each for a, b, and c

 

C. 3 marks

Complete the Javadoc necessary to exactly reproduce the API documentation for the method totalDays(int, int) 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)
    /**
     * Computes the total number of days in the period of time from 
     * Jan 1 of the start year to Dec 31 of the end year accounting 
     * for the presence of leap years.
     * 
     * <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>
     * 
     * @param startYear
     *            the start year
     * @param endYear
     *            the end year
     * @return the number of days from Jan 1 of the start year to Dec 31 of the
     *         end year 
     * @pre. endYear is greater than or equal to startYear
     */
    public static int totalDays(int startYear, int endYear)

Grading scheme: The submission must match the solution for the @param, @return, and @pre. parts of the Javadoc (the documentation for the tags may be on the next line after the tag). The documentation for the method description should closely match the solution (minor spelling errors are acceptable.)

Deduct 1 mark for each deviation from the solution up to a total of 3 marks.

 

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?

(a)
1. should not be used because it violates the precondition that the list size be greater than or equal to one
2. should be used because the list satisfies the preconditions and the expected result is correct
3. should not be used because it violates the precondition that the list be sorted

(b)
Input list: [2000] or any other list of size 1
Expected result: 2000
Explanation: the list of size 1 is a boundary case (the shortest possible list that satisfies the method preconditions)

Grading scheme: 2 marks for (a); give 1 mark if there is at least one correct answer. 2 marks for (b); 1 mark for the test case and 1 mark for the explanation.

 

To submit your written questions:

submit 2030L secEtest1B answers.txt