Use the following files:
DataSet.java
/**
Computes the average of a set of data values.
*/
public class DataSet
{
/**
Constructs an empty data set with a given measurer.
@param aMeasurer the measurer that is used to measure data values
*/
public DataSet(Measurer aMeasurer)
{
sum = 0;
count = 0;
maximum = null;
measurer = aMeasurer;
}
/**
Adds a data value to the data set.
@param x a data value
*/
public void add(Object x)
{
sum = sum + measurer.measure(x);
if (count == 0
|| measurer.measure(maximum) < measurer.measure(x))
maximum = x;
count++;
}
/**
Gets the average of the added data.
@return the average or 0 if no data has been added
*/
public double getAverage()
{
if (count == 0) return 0;
else return sum / count;
}
/**
Gets the largest of the added data.
@return the maximum or 0 if no data has been added
*/
public Object getMaximum()
{
return maximum;
}
private double sum;
private Object maximum;
private int count;
private Measurer measurer;
}
Measurer.java
/**
Describes any class whose objects can measure other objects.
*/
public interface Measurer
{
/**
Computes the measure of an object.
@param anObject the object to be measured
@return the measure
*/
double measure(Object anObject);
}
Word.java
/**
This class describes words in a document.
*/
public class Word
{
/**
Constructs a word by removing leading and trailing non-
letter characters, such as punctuation marks.
@param s the input string
*/
public Word(String s)
{
int i = 0;
while (i < s.length() && !Character.isLetter(s.charAt(i)))
i++;
int j = s.length() - 1;
while (j > i && !Character.isLetter(s.charAt(j)))
j--;
text = s.substring(i, j + 1);
}
/**
Returns the text of the word, after removal of the
leading and trailing non-letter characters.
@return the text of the word
*/
public String getText()
{
return text;
}
/**
Counts the syllables in the word.
@return the syllable count
*/
public int countSyllables()
{
int count = 0;
int end = text.length() - 1;
if (end < 0) return 0; // The empty string has no syllables
// An e at the end of the word doesn't count as a vowel
char ch = Character.toLowerCase(text.charAt(end));
if (ch == 'e') end--;
boolean insideVowelGroup = false;
for (int i = 0; i <= end; i++)
{
ch = Character.toLowerCase(text.charAt(i));
String vowels = "aeiouy";
if (vowels.indexOf(ch) >= 0)
{
// ch is a vowel
if (!insideVowelGroup)
{
// Start of new vowel group
count++;
insideVowelGroup = true;
}
}
else insideVowelGroup = false;
}
// Every word has at least one syllable
if (count == 0)
count = 1;
return count;
}
private String text;
}
WordMeasurerTester.java
/**
This program tests the WordMeasurer class.
*/
public class WordMeasurerTester
{
public static void main(String[] args)
{
DataSet data = new DataSet(new WordMeasurer());
data.add(new Word("Java"));
data.add(new Word("is"));
data.add(new Word("an"));
data.add(new Word("object"));
data.add(new Word("oriented"));
data.add(new Word("programming"));
data.add(new Word("language"));
System.out.println("Average measure: "
+ data.getAverage());
System.out.println("Expected: 2.0");
}
}