COM1204 Object-Oriented Design -- Midterm Exam -- Wednesday, July 25th

Summer 2001 -- Professor Futrelle
College of Computer Science, Northeastern U., Boston, MA


PRINT your name clearly ________________________________________________ Your ID no. __________________


Write all scratch work and answers in your blue book.

Question 1. (30 points)

Assume that there is a class Sup and a direct subclass of it, Sub. Their public constructors are related as follows:

What you are to do: Write the definitions (code) for the three constructors as well as minimal class definitions that support them (with private member variables). You do not need to write a main() that uses the constructors.

Question 2. (30 points)

Given the following text description, write out the definition (Java code) of the procedure find(): This non-static public procedure takes two integer arguments and returns true if both are contained in the object and false otherwise. For implementation purposes assume that the integers are contained in a private variable v of type Vector. Hint: Remember to use intValue() as needed.

Proper layout of your code and well-written (not overly wordy) comments are also required. You are not required to write the class definition.

Question 3. (30 points)

Describe some differences that arise when a superclass uses protected (or public) member variables versus private ones. Specifically, discuss:

Question 4. (10 points -- because it's difficult)

You are to study the following code on the back of this sheet and the specifications for lastWord() and explain what is correct and incorrect about the specifications. In addition, if any of the specifications are incorrect you must answer both of the following two questions:

The specifications for lastWords():

REQUIRES: int argument > 0
MODIFIES: this and int argument
EFFECTS: Prints a list of the last non-blank characters of each string in the collection of strings contained in the object, i.e., the characters following the last blank. Examines through the n-th member of the collection of strings, where n identifies the int argument.

The output from executing the code is Included as a comment at the end of the code. Brief explanations of the various types and functions used are given following the code, since you may not be familiar with all of them. If there is not enough information about them to answer the questions in the detail you would like, make some reasonable assumptions and explain them briefly.

import java.util.*;

public class Words{    

    public String[] s;

    public static void main(String[] args){

	String[] localS =  {
	    "34 Center St. ",
	    "34 Main Street ",
	    "34 Main St. ",
	    "34 Some Road ",
	    "34 Avenue A  "
	};
	
	Words w = new Words();
	w.s = localS;
	w.s = w.lastWords(5);

	System.out.println("Output is:");
	for(int k = 0; k < w.s.length; k++)
	    System.out.println(w.s[k]);
    }

    public String[] lastWords(int n){

	HashMap h = new HashMap();
	String ss = "";

	for(int i = 0; i < n; i++){
	    ss = new String(s[i]);
	    ss = ss.trim();
	    int ii = ss.lastIndexOf(" ");
	    ss = ss.substring(ii, ss.length());
	    h.put(ss, " ");
	}

	Object[] o = (h.keySet()).toArray();
	String[] sss = new String[o.length];
	for(int j = 0; j < o.length; j++)
	    sss[j] = (String)o[j];

	return sss;

    }
}

/*
 
Output is:
 Street
 Road
 A
 St.

*/ 

A HashMap stores a collection of keys and for each key, a value. There can only be one entry with a given key. A put() with the same key (first arg) and any other value just updates the value. trim() removes leading and trailing whitespace from a String. lastIndexOf() finds the starting index of the last occurrence of the String provided. substring() returns the portion of the string that starts at the first index and stops one short of the second index. keySet() returns the set of all keys in a HashMap and toArray() converts such a set to an array.