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;
}
}
----------------------
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"); } } }
Step 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