NEED in JAVA You can use sorting functions like quicksort, insertion sort For yo
ID: 3885225 • Letter: N
Question
NEED in JAVA
You can use sorting functions like quicksort, insertion sort
For your poetry class, you would like to tabulate a list of rhyming words. For example, if you had the following words (electrics, ethnic, clinic, coughed, laughed, metrics), your program should produce the following output:
Write a program Rhyming.java that reads in a sequence of words from standard input and prints them out in the order specified above. Specifically, we define here rhyming words as a group of words sharing a rhyme, and we define that rhyme as being the longest possible suffix common to all words in the group. For instance, in the example above, we do not consider "ic", "ics", or "ed" to be rhymes because they are suffixes of longer common suffixes.
The output order is defined as follows.
Words are listed in increasing order of suffix length
Among suffixes of the same length, words are listed in alphabetical suffix order
Words with common suffix (rhyming words) are listed in alphabetical order
Looking back at the previous example: the suffixes are nic (length=3), trics (length=5), and ughed(length=5). They appear in that order as a result of rule 1 (length 3 before length 5) and rule 2 (trics comes before ughed alphabetically). Finally, each set of rhyming words are listed alphabetically (rule 3). In addition, groups of rhyming words are separated by a newline. Note that the same word can rhyme with different groups of words. For instance: (friendly, lucky, murky, rapidly) would lead to the following output:
Again, while "y" is not a rhyme for the group (lucky, murky) (it is a suffix of the actual rhyme "ky"), it is a rhyme for the group (friendly, lucky, murky, rapidly), as it is the longest common suffix.
Input / Output
Your program should read from standard input, using StdIn.java. It should process the input using the following command line:
The file contains a list of strings, one string per line. Your program should output to standard output all the words sorted as the example stated above. To facilitate the visual assessment of your result, start each new line (corresponding to a series of rhyming words in alphabetical order) with an opening square bracket followed by a space character and, symmetrically, terminate each new line with a space character followed by a closing square bracket. In addition, each word should contain a pipe symbol ("|") that indicates the beginning of the identified suffix. Again, refer to the examples above and to the solution files provided below.
For your poetry class, you would like to tabulare a list of hyming words. For example, if you had the folowing words (eecrics, ethnic, clini, coughad, lauphed, metries), your program should produce the following outpat lec trica, e trica1 ut and prints them o t 1n e order s ried above. Specifcnly we define here rhyming wo 'isor 'el to be rhyes because they ar suffixes u langer cem suffixe s as a group of won s s anng a r yme, and we eine th t yme as being t e on est possible suffix common to all Write o eram Rhy 1 ng 28that reads in sequence of w ords from smnda wwnis in the graup. For instance, in the example a, we do not consider " The outpat onder is defined as follows. . Words are listed in increasing order of suftix Jeneth Among suffisthe same length, avards ane listed in alphahetical suffix onier Wards wih couon sullix [rhyming wrus) are lisled in alpiabetical arder Looking back at the previous exam ple: he suffixes are n c fleng h=3) n s fleng h=5) and he ength 5). Th e appear in that arder as a result of rule l length 3 eme length 5 and rule 2 ncs comes be e u hed alp ale ically. Finally, each set af rhyming words are listed alpba etically rule 3 L addit on, groups of yming words are s rated by a newline. No e at be same d can rhyz e ith dine ent groups o words. For inst no friendly, lucky murky, rapidly would lead to the following output: Erindly, rpi dly 1 Again, while "y" is not a rhye Tor tbe group lucky,murky) (it is a sulTix of dhe acual hyrme 'ky).it is a diyme o thegroup riendly, lucky, murky, rapidly)as it is the longest coosuflix. Input Output Your program should read from stardand input, using stdI.Java. It shoud process oe input using e toowing command line The tile contains a List ot strings, one sorine per line. Your program should ourput to stacdard cutput all the words scrted as toe example stared above. To facilitate the visoal assessment of yoar result, stut c new line (coesponding to a serics of yming ords in alphabetical oeder) wath ao apening squae acket followed by a space characer and, symmetrically,eminate each new line with a spacechaacter folloaed hy a cosing sqare bracket. Th addition, ench ond should contain a pipe symhol ") that indicates the heginning ofthe idemtified suffix Again, refer to the eaamples above and to de solutiva files provided below.Explanation / Answer
Rhyming.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.PrintWriter;
import java.io.Reader;
public class RhymingWords {
public static void main(String[] args) throws IOException {
FileReader wordFile = new FileReader("C:/Users/nikos7/Desktop/words.txt");
Reader rhymingwords = reverseOrder(sort(reverseOrder(wordFile)));
BufferedReader inp = new BufferedReader(rhymingwords);
String input;
while ((input = inp.readLine()) != null) {
System.out.println(input);
}
inp.close();
}
public static Reader reverseOrder(Reader src) throws IOException {
BufferedReader input = new BufferedReader(src);
PipedWriter pipeOut = new PipedWriter();
PipedReader pipeIn = new PipedReader(pipeOut);
PrintWriter out = new PrintWriter(pipeOut);
new ReverseThread(out, input).start();
return pipeIn;
}
public static Reader sort(Reader src) throws IOException {
BufferedReader input = new BufferedReader(src);
PipedWriter pipeOut = new PipedWriter();
PipedReader pipeIn = new PipedReader(pipeOut);
PrintWriter out = new PrintWriter(pipeOut);
new SortThread(out, input).start();
return pipeIn;
}
}
class SortThread extends Thread {
private PrintWriter output = null;
private BufferedReader input = null;
public SortThread(PrintWriter output, BufferedReader input) {
this.output = output;
this.input = input;
}
@Override
public void run() {
int MAXWORDS = 250;
if (output != null && input != null) {
try {
String[] words = new String[MAXWORDS];
int numwords = 0;
while ((words[numwords] = input.readLine()) != null) {
numwords++;
}
quicksort(words, 0, numwords - 1);
for (int i = 0; i < numwords; i++) {
output.println(words[i]);
}
output.close();
} catch (IOException e) {
System.err.println("Sort: " + e);
}
}
}
private static void quicksort(String[] str, int low0, int high0) {
int loPart = low0;
int hiPart = high0;
if (loPart >= hiPart) {
return;
}
String middle = str[(loPart + hiPart) / 2];
while (loPart < hiPart) {
while (loPart < hiPart && str[loPart].compareTo(middle) < 0) {
loPart++;
}
while (loPart < hiPart && str[hiPart].compareTo(middle) > 0) {
hiPart--;
}
if (loPart < hiPart) {
String T = str[loPart];
str[loPart] = str[hiPart];
str[hiPart] = T;
loPart++;
hiPart--;
}
}
if (hiPart < loPart) {
int T = hiPart;
hiPart = loPart;
loPart = T;
}
quicksort(str, low0, loPart);
quicksort(str, loPart == low0 ? loPart + 1 : loPart, high0);
}
}
class ReverseThread extends Thread {
private PrintWriter output = null;
private BufferedReader inputre = null;
public ReverseThread(PrintWriter out, BufferedReader in) {
this.output = out;
this.inputre = in;
}
@Override
public void run() {
if (output != null && inputre != null) {
try {
String input;
while ((input = inputre.readLine()) != null) {
output.println(reverseIt(input));
output.flush();
}
output.close();
} catch (IOException e) {
System.err.println("Reverse: " + e);
}
}
}
private String reverseIt(String source) {
int it, len = source.length();
StringBuffer target = new StringBuffer(len);
for (it = (len - 1); it >= 0; it--) {
target.append(source.charAt(it));
}
return target.toString();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.