COM1204 Summer 2001 -- Review for Final Exam

Version of 8/19/01 -- Subject to revision

Some of the following questions will appear, verbatim, on the Final Exam. Other questions will be variations on the questions below. All questions on the Final will be closely related to the topics below.

Example question 1.

(A question based tightly on the material shown in Figures 2.2, 2.3 and 2.4 in the textbook.)

Example question 2.

Assume you have a class A and a subclass of it, B. Write a definition for a constructor B(int, String) that calls a superclass constructor, passing it only the first, int, argument and sets an instance variable iString of B to the second argument. Write out the full class and constructor definitions for A and B as well as a test of B via its main(). A bit of improvization may be required.

Example question 3.

Copy the following code into your exam booklet, leaving adequate room for comments you must add. The comments you write should address the following issues, at a minimum. (The comments you write should be more detailed and extensive than comments you would write in a "real" source file.)

public class IntVector{

    private int[] arr = new int[2];
    private int k = 0;
    private int cap = 2;

    public static void main(String[] args){
        IntVector iv  = new IntVector();

        IntVecIter ivi = iv.iterator();

        while (ivi.hasNext())

    public void add(int toAdd) {
        if (k == cap) {
            int[] tempArr = new int[cap*2];
            for (int i = 0; i <  cap ; i++)
                tempArr[i] = arr[i];
            arr = tempArr;
            cap *= 2;
        arr[k++] = toAdd;

    } // add()

    // IntVecIter does  *not* implement the Object-based Iterator interface
    // but is a simpler int-based  one.

    private class IntVecIter {
        private  int index = 0;

        public boolean hasNext(){
            return (index  <  k);

        public int next() {
            return arr[index++];

    } // IntVecIter

    public IntVecIter iterator() {
        return new IntVecIter();

/* Output of the above, as expected:

$ java IntVector


Example question 4.

4A. Write the specifications (REQUIRES, MODIFIES and EFFECTS) for the following procedure, funOne().

4B. Write a main() function that will test this class for an array argument consisting of an array with elements 1, 3 and 7. What should the test print? (You could also design a test that would raise an exception if the procedure failed to produce an answer that you know it should.)

    public static double funOne(int[] arr) {
        double sum = 0.0;

        for(int i = 0; i < arr.length; i++) {
            sum += arr[i];
        return sum/arr.length;

    } // funOne

Example question 5.

Discuss the following refactoring advice from the point of view of good OO design. In addition, write out a definition (source code) for isNotEligableForDisability() You needn't give a class definition to accompany the definition.

"Consolidate Conditional Expression": You have a sequence of conditional tests with the same result. Combine them into a single conditional expression and extract it.

// Before:

        double disabilityAmount() {
                if (_seniority < 2) return 0;
                if (_monthsDisabled > 12) return 0;
                if (_isPartTime) return 0;
                // compute the disability amount

// After:

        double disabilityAmount() {
                if (isNotEligableForDisability()) return 0;
                // compute the disability amount

Example question 6.

Discuss the following refactoring advice from the point of view of good OO design.

"Replace Array with Object": You have an array in which certain elements mean different things. Replace the array with an object that has a field for each element.

// Before:

        String[] row = new String[3];
        row [0] = "Liverpool";
        row [1] = "15";

// After:

        Performance row = new Performance();