/**
* An array implementation of a list of Words. The array is dynamic, able to grow if needed
* when an element is added to the list.
*
* @author D. Slater
* @version 3/10/2003
*
* modified by student:
* section:
*
* method to complete: buildList()
*
* methods to be written by student
* add
* contains
* numCharacters
* wordsWithSubstr
* wordsWithLength
* findLongestWord
* longestWords
* wordsBetween
*
*/
import java.io.*;
import java.util.*;
public class WordList {
// instance variables
private String [ ] list;
private int count;
/**
* Constructs an empty list with the specified initial capacity
*
* @param int capacity - the initial capacity of the list
*/
public WordList(int capacity) {
list = new String[capacity];
count = 0;
}
/**
* prompts user for filename, opens the specified file, and builds the list of Words
* from the file.
*
* TO BE COMPLETED BY THE STUDENT:
*/
public void buildList() throws IOException{
// build an object for keyboard input
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
// prompt for file name
System.out.println();
System.out.print("Enter the name of the file to open: ");
String filename = keyboard.readLine();
// build an object for file input
BufferedReader file = null;
try {
file = new BufferedReader(new FileReader(new File(filename)));
} catch (FileNotFoundException e) { // check for valid filename
System.out.println("File not found.");
System.exit(-1); // exit if invalid filename
}
// read the file
String line;
while ((line = file.readLine()) != null) {
StringTokenizer stk = new StringTokenizer(line, " .,:;?!");
while(stk.hasMoreTokens()) {
String word = stk.nextToken();
//*************************************************
// ADD CODE TO COMPLETE METHOD AFTER THIS BLOCK
// a) convert word to all lower case characters
// b) only add to the list if the word is not already in the list
// c) only add to the list if the word length > 3
//**************************************************
word = word.toLowerCase();
if (word.length() > 3 ) {
if (contains(word) == -1){
add(word);
}
//******************************************************************
//
// Write other methods after this comment block
//
//******************************************************************
public void add(String word){
if ( isFull()) {
resize();
}
list[count++] = word;
}
public int contains(String other){
int i;
for ( i = 0; i < count; i++) {
if (list[i].equals(other)){
return i;
}
}
return -1;
}
public int numCharacters(){
int num = 0;
int i;
for(i = 0; i < count; i ++){
num = num +list[i].length();
}
return num;
}
public Wordlist wordsWithSubstr(String substr){
int i;
Wordlist wl = new Wordlist(10);
for ( i = 0; i < count; i ++){
if (list[i].indexOf(substr) != -1){
wl.add(substr);
}
return wl;
}
}
public Wordlist wordsWithLength(int value){
int i;
Wordlist wl = new Wordlist(10);
for ( i = 0; i < count; i ++){
if (list[i].length() == value){
wl.add(list[i]);
}
return wl;
}
}
public Wordlist wordBefore(String word1){
int i;
Wordlist wl = new Wordlist(10);
for ( i = 0; i < count; i ++){
if (list[i].compareTo(word1) < 0) {
wl.add(list[i]);
}
return wl;
}
}
public int findLongestWord(){
int i;
int num = 0;
for ( i = 0; i < count; i ++){
if (list[i].length() > num) {
num = i;
}
}
return num;
}
public Wordlist longestWords(){
return wordsWithLength(findLongestWord());
}
//******************************************************************
//
// The following methods are complete and should
// be used when and if appropriate
//
//******************************************************************
/**
* Displays all of the elements of the list to the monitor
*/
public String toString() {
String str = "";
for(int i = 0; i < count; i++) {
str += list[i] + "\n";
}
return str;
}
/**
* Returns the number of elements in this list.
*
* @return int - the number of elements in this list.
*/
public int size() {
return count;
}
/**
* Tests if this list has no elements.
*
* @return boolean - true if this list has no elements; false otherwise.
*/
public boolean isEmpty() {
return count == 0;
}
/**
* Returns the current number of slots available in this list.
*
* @return int - the current number of slots available in this list.
*/
public int capacity() {
return list.length;
}
/**
* Tests if this list has has room for no more elements.
*
* @return boolean - true if this list has no available slots; false otherwise.
*/
public boolean isFull() {
return count == capacity();
}
/**
* Increases the capacity of this WordList instance, if necessary, to ensure
* that it can hold at least the double the current capacity of the list
*
*/
private void resize() {
String [] temp = new String [capacity() * 2 + 1];
for (int i = 0; i < size(); i++) {
temp[i] = list[i];
}
list = temp;
}
}