Question
---------------------- WordData : import java.util.ArrayList; public class WordData { private String word; private ArrayList lineNumbers; private int WordCount; public WordData(String word, ArrayList lineNumbers, int wordCount)
----------------------
WordData :
import java.util.ArrayList;
public class WordData {
private String word;
private ArrayList
private int WordCount;
public WordData(String word, ArrayList
this.word = word;
this.lineNumbers = lineNumbers;
WordCount = wordCount;
}
public String getWord() {
return word;
}
public ArrayList
return lineNumbers;
}
public int getWordCount() {
return WordCount;
}
public void setWord(String word) {
this.word = word;
}
public void setLineNumbers(ArrayList
this.lineNumbers = lineNumbers;
}
public void setWordCount(int wordCount) {
WordCount = wordCount;
}
@Override
public String toString() {
String res = word + " appears " + WordCount + " times on " + lineNumbers.size() + " lines: ";
int i=0;
for(i=0;i
res += lineNumbers.get(i) + ",";
}
res +=( lineNumbers.get(i) +"..." + lineNumbers.get(lineNumbers.size()-1));
return res;
}
@Override
public boolean equals(Object other) {
// TODO Auto-generated method stub
WordData obj = (WordData) other;
return word.equals(obj.word);
}
int compareTo(Object other) {
WordData obj = (WordData) other;
if(this.WordCount > obj.WordCount) {
return -1;
}else if(this.WordCount == obj.WordCount) {
return 0;
}
else
return 1;
}
}
------------------------------
WordDataArrayList :
import java.util.ArrayList;
public class WordDataArrayList extends ArrayList
/**
* method to add the occurrence of a word on a line
*/
public void add(int line, String word) {
/**
* defining an array list of integers to create a new WordData object
*/
ArrayList
//adding the given line number
lines.add(line);
//creating a word data object
WordData w = new WordData(word, lines, 1);
//checking if the list already contains the given word
if (contains(w)) {
//getting the index of the word data
int index = indexOf(w);
//checking if the current line number is already added
if (!get(index).getLineNumbers().contains(line)) {
//adding the line number
get(index).getLineNumbers().add(line);
}
//incrementing the word count
get(index).setWordCount(get(index).getWordCount() + 1);
} else {
//word not on the list, adding it
super.add(w);
}
}
@Override
public String toString() {
String data = "";
/**
* iterating through all word data elements and appending to a String variable in proper format
*/
for (int i = 0; i
WordData w = get(i);
data += w.getWord() + "(" + w.getWordCount() + " "
+ w.getLineNumbers().size() + ")";
data += "(";
for (int j = 0; j
data += w.getLineNumbers().get(j) + " ";
}
data += "); ";
}
return data;
}
}
-------------------------
TestA4 :
public class TestA4 { public static void main(String[] args) { WordDataArrayList test = new WordDataArrayList(); try{ System.out.println("Testing WordDataArrayList basic operation: "+ "==========================================="); System.out.println("Should be empty list: "+test+" ----------"); test.add(99,"at"); System.out.println("Should be at(1 1)(99): "+test+" ----------"); test.add(103,"at"); System.out.println("Should be at(2 2)(99 103): "+test+" ----------"); test.add(103,"be"); System.out.println("Should be at(2 2)(99 103) and be(1 1)(103): "+test+" ----------"); } catch(Exception e){ System.out.println("Basic WordDataArrayList tests failed"); } try{ System.out.println(" Testing WordData equals and compareTo: "+ "======================================"); System.out.println("should be false: "+test.get(0).equals(test.get(1))); System.out.println("should be true: "+test.get(0).equals(test.get(0))); System.out.println("negative/positive may be reversed if \"be\" comes before \"at\":"); System.out.println("should be negative: "+test.get(0).compareTo(test.get(1))); System.out.println("should be 0: "+test.get(0).compareTo(test.get(0))); System.out.println("should be positive: "+test.get(1).compareTo(test.get(0))); } catch(Exception e){ System.out.println("WordData equals/compareTo tests failed"); } try{ System.out.println(" Testing WordDataArrayList additional operation: "+ "==============================================="); for(int i=0; i
} catch(Exception e){ System.out.println("WordScanner tests failed"); } } }
----------------------------
TestWordScanner :
public class TestWordScanner { public static final int minWordLength = 1; public static String line = "I'm sure _I_ shan't be able! I shall be a great deal too far off to trouble myself"+ " about you: you must manage the best way you can;--but I must be kind to them,' thought"+ " Alice, 'or perhaps they won't walk the way I want to go!"; public static void main(String[] args) { WordScanner getWords = new WordScanner(line,minWordLength); String word; do { word = getWords.nextWord(); if(word != null) System.out.println(word); } while(word != null); } }
PART 3: THe WORDSCANNER CLASS- You will need to pick out all of the individual words in a file that contains a book. The scanner class can find "tokens" (sequences of non-blank characters) but that is not the same as finding "words". So once again a customized class should be createdStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started