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

--------------------------------- TestA4 ? public class TestA4 { public static v

ID: 3913338 • Letter: #

Question

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

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

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

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

Explanation / Answer

package com.src;

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.java

package com.src;

import java.util.ArrayList;

public class TestA4 {

public static void main(String[] args) {

//WordDataArrayList test = new WordDataArrayList();

ArrayList<String> test = new ArrayList<>();

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

}

}

}

//TestWordScanner.java

package com.src;

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);

}

}

//WordScanner.java

package com.src;

public class WordScanner {

private String line;

private int minwordlength;

int index=0;

public String getLine() {

return line;

}

public int getMinwordlength() {

return minwordlength;

}

public void setLine(String line) {

this.line = line;

}

public void setMinwordlength(int minwordlength) {

this.minwordlength = minwordlength;

}

public WordScanner(String line, int minwordlength) {

// TODO Auto-generated constructor stub

this.line = line;

this.minwordlength = minwordlength;

}

public String nextWord() {

// TODO Auto-generated method stub

String word = "";

for(int i=index;i<line.length();i++) {

if(line.charAt(i) == ' ')

break;

else {

word += line.charAt(i);

}

}

if(word == "")

return null;

else

return word;

}

}