Description
Your task is to write a WordList class that uses an ArrayList to store a collection of
String objects. Your class should provide the following methods:
• WordList(): a constructor to initialize an empty list of words.
• WordList(String [] array): a constructor to initialize a list of words with all of
the strings in the array passed as argument.
• boolean readWords(String filename): adds to the list all of the words
contained in the file passed as parameter. The file may contain several lines and
several words per line. Words may include punctuation symbols (such as commas,
semicolons, and periods) as their last character. The method should return true if the
file is opened successfully. Otherwise, the method should handle the exception and
return false.
• int count() : returns the total number of words stored in the list.
• int count(String target): returns the number of times that the target is stored
in the list. Case and punctuation symbols should be ignored when comparing words.
• int replace(String old, String new, boolean ignoreCase): replaces all
occurrences of the old word with the new word. The method returns the number of
replacements made as a result. The boolean argument indicates whether case should
be considered when matching the strings to be replaced (in other words, false means
that only those entries matching exactly old should be replaced).
• int display(int wordsPerLine): displays the contents of the list. The single
parameter indicates the number of words that should be displayed per line. Of course,
it is possible that the last line will have fewer words. The method returns the total
number of lines displayed.
• String toString(): returns a String containing the contents of the list, surrounded
by [ ] brackets. Individual words should be separated by single spaces.
Your solution should be stored in the WordList.java source file. Comments for your class
and its methods should follow javadoc guidelines. The source file WordListTest.java and a
sample text file will be provided to test some of the functionality of your class. However,
you should test it more thoroughly before submitting it to Mimir for grading. Remember:
all you need to submit is your WordListTest.java source file.
SAMPLE OUTPUT FROM TEST PROGRAM:
0 words in the list
beatles appears 0 times
5 words in the list
can appears 1 time(s)
can replaced 1 time(s)
beatles appears 0 times
WORDS:
Oh say do
you see
Total # of lines: 2
CONTENTS: [Oh say do you see]
167 words in the list
rock appears 4 time(s)
rock replaced 4 time(s)
beatles appears 2 times
WORDS:
So much has been said and written about the Beatles,
and their story is so mythic in its sweep, that
it’s difficult to summarize their career without restating clichés that
have already been digested by tens of millions of rock-and-roll
fans. To start with the obvious, they were the greatest
and most influential act of the rock-and-roll era, and introduced
more innovations into popular music than any other rock-and-roll band
of the 20th century. Moreover, they were among the few
artists of any discipline that were simultaneously the best at
what they did and the most popular at what they
did. Relentlessly imaginative and experimental, the Beatles grabbed a hold
of the international mass consciousness in 1964 and never let
go for the next six years, always staying ahead of
the pack in terms of creativity but never losing their
ability to communicate their increasingly sophisticated ideas to a mass
audience. Their supremacy as rock-and-roll icons remains unchallenged to this
day, decades after their breakup in 1970.
Total # of lines: 17