Informatics 42 • Winter 2008 • David G. Kay • UC Irvine

Third Homework

Get your work checked and signed off by a classmate, then show it to your TA in lab by Monday, January 28.

(a) Make a new version of your Reverse program from last week so that instead of storing the file in an ArrayList it uses a Stack<String> instead. In what ways does this simplify your earlier code? (Note that Java's Stack class doesn't use the standard term for the 'top' operation.)

(b) Make a copy of your line-numbering program from last week; rename it to Stats. It should read a file and print out (to the standard output, not to another file) the total number of lines, the average line length (to three decimal places), the number of empty (zero-length) lines, the average length of non-empty lines (again to three places), the length of the longest line, the longest line itself, the length of the shortest non-empty line, and the shortest non-empty line itself.

These are some classic "micro-algorithms" on a collection of data: Count the items, sum the items, find the largest, find the smallest.

(c) A Scanner can read "words" from its input, just as it can read ints or doubles or lines. By default, the Scanner treats as a word everything delimited (separated) by white space (spaces, tabs, and/or newlines); it's possible to specify different delimiters, but that's something we'll do later. (Since these aren't always English words, the technical term we use is "tokens.") You can create a Scanner that reads from a string, rather than an input stream, and use the methods hasNext() and next() to do the reading.

Enhance your Stats program to print the total number of tokens in the file, the average number of tokens per line, the number of tokens on the line with the fewest tokens (greater than zero), the number of tokens on the line with the most tokens, the shortest (non-empty) token, the longest token, and the average length of a token.

(d) Give the average-case run-time polynomial and the O-notation for the following code segment. Count each assignment statement (except those controlling for-loops) and each function call.

System.out.println("This line is executed only once.");
int total = 0;
for (int i = 1; i <= n; i++)
{
    int x = readAnInteger();  // count this line twice:  assignment plus call
    total += x;
    if (i % 2 == 0)
        System.out.println(x);
}
System.out.print("Total:  ");
System.out.println(total);
System.out.println("The end.");

(e) Years ago, before they built a bridge, you could reach the Isle of Skye in the Scottish Highlands from two points on the mainland: Mallaig and Kyle of Lochalsh. Each town ran a car ferry service—a boat that takes cars (and people) to Skye and back.

(e.1) Each Mallaig ferry has a single automobile entrance. The cars drive on and go as far back in the boat as they can. When the ferry reaches Skye, the cars leave the boat by the same entrance, so the car at the far end of the boat is the last one off. Is the Mallaig ferry more like an array, a stack, a queue, or a tree?

(e.2) Each ferry from Kyle of Lochalsh has a ramp at both ends. The cars drive onto the boat using the ramp at one end; each car drives as far down the boat as possible. When the ferry reaches Skye, it docks so that the ramp at the other end connects with the dock and the cars drive straight off. (The Balboa Island ferry works the same way.) Is the Kyle of Lochalsh ferry more like an array, a stack, a queue, or a tree?

(e.3) Which ferry is more efficient for loading and unloading cars, and why?


Written by David G. Kay, Winter 2005. The algorithm analysis question and the Skye ferry question are exam questions from ages ago.

Modified by David G. Kay, Winter 2006, by Alex Thornton, Winter 2007, and by David G. Kay, Winter 2008.