Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

------------------------------ WordData : import java.util.ArrayList; public cla

ID: 3913390 • Letter: #

Question

------------------------------

WordData :

import java.util.ArrayList;

public class WordData {

private String word;

private ArrayList<Integer> lineNumbers;

private int WordCount;

public WordData(String word, ArrayList<Integer> lineNumbers, int wordCount) {

this.word = word;

this.lineNumbers = lineNumbers;

WordCount = wordCount;

}

public String getWord() {

return word;

}

public ArrayList<Integer> getLineNumbers() {

return lineNumbers;

}

public int getWordCount() {

return WordCount;

}

public void setWord(String word) {

this.word = word;

}

public void setLineNumbers(ArrayList<Integer> lineNumbers) {

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<14 && i < lineNumbers.size();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<10; i++) test.add(111,"do");
System.out.println("Should be at(2 2)(99 103); be(1 1)(103); do(10 1)(111): "+test+" ----------");
test.add(111,"at");
test.add(111,"be");
test.add(111,"at");
System.out.println("Should be at(4 3)(99 103 111); be(2 2)(103 111); do(10 1)(111): "+test+" ----------");
for(int i=0; i<30; i++) test.add(200+i,"lots");
System.out.println("Now adds lots(30 30)(200,201,etc,214...229): "+test+" ----------");
}
catch(Exception e){
System.out.println("Extra WordDataArrayList tests failed");
}
  
try{
System.out.println(" Testing WordScanner operation: "+
"==============================");
WordScanner WordScanner("test-one-two",1);
String word;
System.out.println("Should be test one two null:");
do{
System.out.print((word = one.nextWord())+" ");
}while(word!=null);
System.out.println();
  
WordScanner two = new WordScanner("'t'e's't' O/N/E 2t2 t@h.r.e.e ",1);
System.out.println("Should be test one t three null:");
do{
System.out.print((word = two.nextWord())+" ");
}while(word!=null);
System.out.println();

}
catch(Exception e){
System.out.println("WordScanner tests failed");
}
  
}
  
}

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

// WordDataArrayList.java

import java.util.ArrayList;

public class WordDataArrayList extends ArrayList<WordData> {

                /**

                * 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<Integer> lines = new ArrayList<Integer>();

                                //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 < size(); i++) {

                                                WordData w = get(i);

                                                data += w.getWord() + "(" + w.getWordCount() + " "

                                                                                + w.getLineNumbers().size() + ")";

                                                data += "(";

                                                for (int j = 0; j < w.getLineNumbers().size(); j++) {

                                                                data += w.getLineNumbers().get(j) + " ";

                                                }

                                                data += "); ";

                                }

                                return data;

                }

}

/*OUTPUT (using the given test program)*/

Testing WordDataArrayList basic operation:

===========================================

Should be empty list:

----------

Should be at(1 1)(99):

at(1 1)(99 );

----------

Should be at(2 2)(99 103):

at(2 2)(99 103 );

----------

Should be at(2 2)(99 103) and be(1 1)(103):

at(2 2)(99 103 ); be(1 1)(103 );

----------

Testing WordData equals and compareTo:

======================================

should be false: false

should be true: true

negative/positive may be reversed if "be" comes before "at":

should be negative: -1

should be 0: 0

should be positive: 1

Testing WordDataArrayList additional operation:

===============================================

Should be at(2 2)(99 103); be(1 1)(103); do(10 1)(111):

at(2 2)(99 103 ); be(1 1)(103 ); do(10 1)(111 );

----------

Should be at(4 3)(99 103 111); be(2 2)(103 111); do(10 1)(111):

at(4 3)(99 103 111 ); be(2 2)(103 111 ); do(10 1)(111 );

----------

Now adds lots(30 30)(200,201,etc,214...229):

at(4 3)(99 103 111 ); be(2 2)(103 111 ); do(10 1)(111 ); lots(30 30)(200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 );

----------