This activity asks you to implement several similar algorithms using files
of data and then making some generalizations about those algorithms, combining
them, and making them work on collections other than files.
Counting Data
Create a new class, Counter, within your extremes project. Write
two methods for that class, one that prints the number of words in a given
data file and one that prints the total number of characters in all the
words in a given data file. In both cases, you should use the Scanner
class to access each word in the file.
Create a data file, countTest, to test your program. After writing, saving, compiling, and testing your methods,
you should try to come up with a technique for reducing the amount of
repeated code within these two methods. Filtering Data
Create a new class, Filter, within your extremes project. Write
two methods for that class, one that prints only the words in a given data
file that start with a given prefix and one that prints only the words in a
given data file that have a given length. In both cases, you should use the Scanner
class to access each word in the file.
Create a data file, filterTest, to test your program. After writing, saving, compiling, and testing your methods,
you should try to come up with a technique for reducing the amount of
repeated code within these two methods. Transforming Data
Create a new class, Transformer, within your extremes project. Write
two methods for that class, one that prints each of the numbers in a given data
file after multiplying them by 10 and one that prints each of the words in a
given data file after determining how long their
hailstone sequence is. In both cases, you should use the Scanner
class to access each number in the file.
Create a data file, transformTest, to test your program. In this
example, all of the "words" in your data file should be integer
values.
After writing, saving, compiling, and testing your
methods, you should try to come up with a technique for reducing the amount
of repeated code within these two methods.
Generalizing Your Programs
At this point, you should be able to perform several different algorithms
on a file of data and you should be very familiar with the similarities of
these algorithms. Try to see if you can identify exactly the methods required
by each class used in your algorithms and then see if you can create an
inheritance hierarchy that captures these similarities and allows you to apply
your algorithms, as well as new ones, to a collection.
|