EECS2030E Practice Test 1B

PRACTICE

This is a practice test. The actual test may contain fewer or more questions. The actual test may or may not supply you with other materials (such as starter code).

The Java API is here.


Question 1

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


    package test1;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Pratice1B {
        
        private Pratice1B() {
            
        }
    
        
        public static Integer last(List<Integer> t) {
            return t.get(t.size() - 1);
        }
        
        
        public static int totalArea(List<Integer> widths, List<Integer> heights) {
            if (widths.size() != heights.size()) {
                throw new IllegalArgumentException();
            }
            int area = 0;
            for (int i = 0; i < widths.size(); i++) {
                if (widths.get(i) > 0 && heights.get(i) > 0) {
                    area += widths.get(i) * heights.get(i);
                }
            }
            return area;
        }
        
        
        public static int alternatingSum(List<Integer> t) {
            int sum = 0;
            int multiple = 1;
            for (Integer i : t) {
                sum += multiple * i;
                multiple *= -1;
            }
            return sum;
        }
        
        
        public static List<Integer> encode(List<Integer> t) {
            List<Integer> rle = new ArrayList<Integer>();
            int count = 1;
            int prev = t.get(0);
            if (t.size() > 1) {
                for (int i = 1; i < t.size(); i++) {
                    int digit = t.get(i);
                    if (digit == prev) {
                        count++;
                    } else {
                        rle.add(count);
                        rle.add(prev);
                        count = 1;
                        prev = digit;
                    }
                }
            }
            rle.add(count);
            rle.add(prev);
            return rle;
        }
    
    }

Question 2

Create a text file named answers.txt (use File->New-> Untitled text file in eclipse). Type your answer to the following question in the text file.

A.

State the definition of a method precondition.

A method precondition is a condition that the client must ensure is true immediately before a method is invoked.

B.

Suppose that the method name alternatingSum from Question 1 was implemented like so:

        public static int alternatingSum(List<Integer> t) {
          int sum;
          /* code not shown here that assigns the correct value to sum */
          return sum;
        }
        

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

        List<Integer> aList = Arrays.asList(8, 9, 10, 11);
        int sum = Practice1B.alternatingSum(aList);
        

The first line of code creates the list [8, 9, 10, 11], and the second line of code calls the alternatingSum method from Question 1.

The memory diagram illustrating the state of memory for the two lines of client code is shown below. What suitable values of a, b, and c would complete the memory diagram?

                   ---------------------
                   |    main method    |
                   ---------------------
        aList   100|       300a        |
          sum   102|        a?         |
                   ---------------------
                   |                   |
                   |                   |
                   ---------------------
                300|    List object    |
                   ---------------------
                   |                   |
                   |                   |
                   ---------------------
                   |  alternatingSum   |
                   ---------------------
             t  500|         b?        |
           sum  502|         c?        |
                   ---------------------
        

a = -2, b = 300a, c = -2

a and c are both equal to the value of the alternating sum (8 - 9 + 10 - 11 = -2). b is equal to the address of the list.

C.

Provide the Javadoc necessary to exactly reproduce the API documentation for the method last(List<Integer>) from Question 1.

            /**
            * Returns the last integer in the argument list.
            * 
            * @param t a list of integers
            * @return the last integer in t
            * @pre. t is not empty
            */
        
D.

Provide 3 test cases for the method Practice1B.totalArea. Make sure that each test case tests a different feature of the method (i.e., don't provide 3 test cases that all check if the correct area is returned). For each test case, provide a one sentence explanation of what the test case is testing.

        There are many possible test cases; below are 4 examples:
        
        widths               : [1, 5, 10]
        heights              : [2, 3, 4]
        expected return value: 57
        explanation          : tests typical widths and heights
        
        widths               : [1, 5, -10]
        heights              : [2, 3, 4]
        expected return value: 17
        explanation          : tests a negative width
        
        widths               : [1, 5, 10]
        heights              : [2, -3, 4]
        expected return value: 17
        explanation          : tests a negative height
        
        widths               : [1]
        heights              : [2, 3, 4]
        expected return value: IllegalArgumentException
        explanation          : tests that an exception is thrown if list sizes are different