Overview
This is a short set of problems using interfaces. You will implement a class called StringList that uses interfaces for performing various kinds of processing on streams of text, as well as some sample classes that implement those interfaces. In all, you’ll implement six classes:1
2
3
4
5
6StringList.java
NonCommentLineSelector.java
CommentRemover.java
LetterCollecter.java
LineNumberer.java
LocCounter.java
Note that the file StringListTest.java, found in the default package, will not compile until you have created stubs for the six required classes.
None of the code is very complex; the purpose is just to get you thinking about interfaces a bit.
A StringList (not surprisingly) represents a list of strings. (Most likely, in fact, you’ll use an instance variable of type ArrayListto store the strings themselves.) The interesting part is in the operations map, filter, and reduce. These are based on the four interfaces defined in the package api: Combiner, IntCombiner, Selector, and Transformation. They are all very simple and have only one method each. You can take a look at the javadoc or sample code for more details.1
public StringList map(Transformation t)
Returns a new StringList obtained by invoking the given Transformation’s apply method to each string in this StringList. The apply method just takes a string and returns a (possibly different) string.1
public StringList filter(Selector selector)
Returns a new StringList containing only the elements of this StringList for which the Selector’s select method returns true. The select method just takes a string and returns true or false.1
public String reduce(Combiner combiner, String initialValue)
Returns a string resulting from a reduction operation using the given Combiner and the given initial value. A Combiner has one method, combine, that takes two strings and returns a string. The idea of a “reduction” is to initialize an accumulator variable with the given initial value, and then to iterate over the list, replacing the accumulator value with the result of combining it with the next item in the list.1
public int reduce(IntCombiner combiner, int initialValue)
Returns an integer resulting from a reduction operation using the given IntCombiner and the given initial value. Similar to the reduce operation above, but the accumulated value is an int.
The meaning of the reduce operation may not be obvious. The next section of this document includes a detailed explanation and examples. You should also look at the example StringListTest.java. found in the default package of the sample code.
In addition to the StringList class, you’ll implement some examples of classes that implement the interfaces in the api package so you can try things out. These are:1
public class NonCommentLineSelector implements Selector
The select method returns true if the given string does not have “//“ as its first nonwhitespace characters.1
public class CommentRemover implements Transformation
The apply method returns a new string in which any text following “//“ is removed.1
public class LocCounter implements IntCombiner
Given an integer n and a string, the combine method returns n if the string is a comment line, a blank line, or a line whose only text, other than an end-of-line comment, is a single curly brace; otherwise the method returns n + 1. (Using a LocCounter in the reduce method has the general effect of counting “lines of code” that are actual program statements.)`
public class LetterCollector implements Combiner
Given two strings first and second, appends onto first all characters in second that don’t already occur in first. (Using a LetterCollector in the reduce method returns a string in which each character occurring in the strings appears exactly once.) Not case sensitive.