In this week\'s lab, you write methods to operate on a list of words and practic
ID: 3740609 • Letter: I
Question
In this week's lab, you write methods to operate on a list of words and practice using the ArrayList class in the Java Collection Framework. The JUnit test class needs the data file words.txt that contains about 235,000 words of English sorted in ascending dictionary order, so download this file (DropBox link) and copy it into your Lab 9 project folder. Write the following methods in the class WordProblems. From this wordlist file, the JUnit test class will use only the proper words made of only the lowercase characters a to z· The JUnit test class will output a message "Read 210687 words from words.txt. at initialization so that you know that the wordlist has been sucessfully read in. Your methods should not modify their parameter arraylist words in any way. Also, the JUnit tester requires that every method must return the words that it finds in the same alphabetical order that they occur in the parameter list words.Explanation / Answer
here is the code for the above stated problem
import java.util.Arrays;
import java.util.Comparator;
public class GFG
{
static class Word
{
String str;
int index;
Word(String str, int index)
{
this.str = str;
this.index = index;
}
}
static class DupArray
{
Word[] array;
int size;
public DupArray(String str[], int size)
{
this.size = size;
array = new Word[size];
int i;
for (i = 0; i < size; ++i)
{
array[i] = new Word(str[i], i);
}
}
}
static class compStr implements Comparator<Word>
{
public int compare(Word a, Word b)
{
return a.str.compareTo(b.str);
}
}
static void printAnagramsTogether(String wordArr[],
int size)
{
DupArray dupArray = new DupArray(wordArr, size);
int i;
for (i = 0; i < size; ++i){
char[] char_arr = dupArray.array[i].str.toCharArray();
Arrays.sort(char_arr);
dupArray.array[i].str = new String(char_arr);
}
Arrays.sort(dupArray.array, new compStr());
for (i = 0; i < size; ++i)
System.out.print(wordArr[dupArray.array[i].index]+" ");
}
public static void main(String args[])
{
String wordArr[] = {"cat", "dog", "tac", "god", "act"};
int size = wordArr.length;
printAnagramsTogether(wordArr, size);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.