EECS2030E Test 1

Version A

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 test1A.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/Test1A/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 secEtest1A DiceUtil.java

SOLUTION


Question 2 (12 marks total)

A. 2 marks

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

static means that the field belongs to the class (not to any particular object of the class).

Also acceptable: static means that there is only one copy of the field.

Grading scheme: 2 marks for either answer

 

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.

        // implementation not shown
    }
}

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

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

The memory diagram illustrating the state of memory at the moment that the method body starts to run is shown below. What suitable values of a, b, and c would complete the memory diagram?

             ---------------------
             |    main method    |
             ---------------------
myString  100|       400a        |
       m  102|     not shown     |
       s  104|     not shown     |
             ---------------------
             |                   |
             |                   |
             ---------------------
          a? |   String object   |
             ---------------------
             |                   |
             |                   |
             ---------------------
             |  repeat method    |
             ---------------------
       s  800|         b?        |
       n  802|         c?        |
             ---------------------
    a = 400a   (the address of the myString object)
    b = 400a   (the address of the myString object)
    c = 5      (the value of m)

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 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>
 * dice                 returns
 * ----------------------------
 * [2, 2, 3]               2
 * [3, 4, 5, 5]            4
 * [1, 2, 3, 4, 4]         3
 * [1, 1, 4, 5, 5, 6]      4
 * </pre>
 * 
 * 
 * 
 * 
 */
public static int median(List<Integer> dice)
/**
 * Given a list of dice sorted in ascending order, returns the
 * median value of the dice in the list. The median value is
 * defined as the value of the die in the middle of the list.
 * If the list has an even number of dice, then the value of
 * the last die in the front half of the list is returned.
 * For example:
 * 
 * <pre>
 * dice                 returns
 * ----------------------------
 * [2, 2, 3]               2
 * [3, 4, 5, 5]            4
 * [1, 2, 3, 4, 4]         3
 * [1, 1, 4, 5, 5, 6]      4
 * </pre>
 * 
 * @param dice a list of dice
 * @return the median value of the dice in the list
 * @throws IllegalArgumentException if the list of dice is empty
 */
public static int median(List<Integer> dice)

Grading scheme: The submission must match the solution for the @param, @return, and @throws 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 median(List<Integer> dice) from Question 2:

Input list        Expected result
[1, 2, 3, 4] 2
[1, 2, 3, 4, 5] 3

At least two important test cases are missing. Provide the missing test cases and explain with one sentence for each case why they are important.

Input list: the empty list
Expected result: an IllegalArgumentException is thrown
Explanation: the API says that the method throws an IllegalArgumentException when the list is empty.

Input list: [1] (or any other list of size 1)
Expected result: 1
Explanation: This is a border case (the smallest possible odd sized list)

Input list: [1, 2] (or any other list of size 2)
Expected result: 1
Explanation: This is a border case (the smallest possible even sized list)

Grading scheme: 2 marks each for any two of the three test cases above; the explanation must be reasonable (otherwise deduct 1 mark).

 

To submit your written questions:

submit 2030L secEtest1A answers.txt