} Create a new Java file, and name it A202. Java. [Do not prefix it with your na
ID: 3672438 • Letter: #
Question
}
Create a new Java file, and name it A202. Java. [Do not prefix it with your name.) Name the class appropriately to match. You have been provided with a main program file, called TestA2Q2.java Create the following instance variables: an uninitialized array of type String, two String variables to store input and output file names, and an integer numWords that we will use to specify how many words are currently in the array. Make all instance variables private Define only one constructor that accepts two String parameters to initialize the input and output file names. It should also initialize the array of type String to a size of 20 (remember to use constants as appropriate), and set numWords to 0, because the list will initially contain no words Define the following methods . private void insert(String newWord) This method will insert newWord into the array, keeping the array sorted in ascending order. This method should expand the array if required. To do this, you will need to use an "ordered insertion" algorithm, as covered in COMP 1010. Remember that to compare two String values you cannot use . You must use the compareTo method string1.compareTo(string2) will give a negative number if string1 is less than string2, a positive number if stringl is greater than string2, and a 0 if they're identical private int indexOf (String newWord) This method will perform a binary search on the array (remember that you keep the array sorted so that you can use the binary search algorithm) and return the index of newWord in the array, or-1 if newWord does not exist in the array. . public void removeDuplicateLines ) This method will read the input file line by line and if the current line has not been seen before, it will be written to the output file. Otherwise it will print a message saying that this word has been seen beforeExplanation / Answer
TestA2Q2.java
public class TestA2Q2 {
public static void main(String[] args) {
A2Q2 remover = new A2Q2("words.txt", "newWords.txt");
remover.removeDuplicateLines();
System.out.println("End of processing. Programmed by Stew Dent.");
}
}
A2Q2.java
import java.io.*;
import java.util.Scanner;
public class A2Q2 {
private String inputFileName;
private String outputFileName;
private int numWords;
private String[] orderedArray;
public A2Q2(String inputFileName, String outputFileName) {
this.inputFileName = inputFileName;
this.outputFileName = outputFileName;
orderedArray = new String[20];
numWords = 0;
}
public int BinarySearch(String[] a, int low, int high, String searchValue) {
// recursive version
int mid, comp;
if (high <= low) {
if (searchValue.compareTo(a[(high + low) / 2]) == 0)
return (high + low) / 2;
else
return -1;
}
mid = (high + low) / 2;
comp = searchValue.compareTo(a[mid]);
if (comp > 0) {
return BinarySearch(a, mid + 1, high, searchValue);
} else if (comp < 0) {
return BinarySearch(a, low, mid, searchValue);
} else //when a[mid] is the search value..
{
return mid;
}
} //end function
private void insert(String newWord) {
//for ordered
if (numWords <= 0)
orderedArray[numWords] = newWord;
else {
int i;
for (i = 0; i < numWords; i++) {
int comp = orderedArray[i].compareTo(newWord);
if (comp > 0) {
if (numWords >= 20) {
String[] newArray = new String[numWords + 1];
System.arraycopy(orderedArray, 0, newArray, 0, numWords);
orderedArray = newArray;
}
System.arraycopy(orderedArray, i, orderedArray, i + 1, numWords - i);
orderedArray[i] = newWord;
break;
}
}
if (i == numWords)
orderedArray[numWords] = newWord;
}
numWords++;
}
private int indexOf(String newWord) {
return BinarySearch(orderedArray, 0, numWords - 1, newWord);
}
public void removeDuplicateLines() {
try (
Scanner in = new Scanner(new FileInputStream(inputFileName));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFileName)))
) {
while (in.hasNext()) {
String line = in.nextLine().trim();
if (numWords == 0 || indexOf(line) == -1) {
insert(line);
bw.write(line);
bw.newLine();
} else {
System.out.println(line + " has been seen before!!");
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
words.txt
hello
foo
teacher
comp1020
bar
hello
teacher
java
repeat
java
newWords.txt
hello
foo
teacher
comp1020
bar
java
repeat
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.