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

Please answer question correctly and show the result of the working code. The ac

ID: 3749465 • Letter: P

Question

Please answer question correctly and show the result of the working code. The actual and demo code is provided .must take command line arguments of text files for ex : input.txt output.txt from a filetext(any kind of input for the text file should work as it is for testing the question)

This assignment is about using the Java Collections Framework to accomplish some basic text-processing tasks.

These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map, or SortedMap) to efficiently accomplish the task at hand. The best way to do these is to read the question and then think about what type of Collection is best to use to solve it. There are only a few lines of code you need to write to solve each of them.

Unless specified otherwise, sorted order refers to the natural sorted order on Strings, as defined by String.compareTo(s).

Explanation / Answer

package chegg2; import java.io.BufferedReader; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class Part0 { /** * Read lines one at a time from r. After reading all lines, output * all lines to w, outputting duplicate lines only once. Note: the order * of the output is unspecified and may have nothing to do with the order * that lines appear in r. * @param r the reader to read from * @param w the writer to write to * @throws IOException */ public static void doIt(BufferedReader r, PrintWriter w) throws IOException { Set s = new HashSet(); Set duplicates = new HashSet(); for (String line = r.readLine(); line != null; line = r.readLine()) { if(s.contains(line)){ duplicates.add(line); }else{ s.add(line); } } for (String duplicate : duplicates) { w.println(" These are the duplicates that are to be printed only once:: " + duplicate); } for (String text : s) { w.println(text); } } } package chegg2; import java.io.*; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class Part5 { /** * Your code goes here - see Part0 for an example * @param r the reader to read from * @param w the writer to write to * @throws IOException */ public static void doIt(BufferedReader r, PrintWriter w) throws IOException { // Your code goes here - see Part0 for an example Set s = new HashSet(); Set duplicates = new HashSet(); for (String line = r.readLine(); line != null; line = r.readLine()) { /* Since the duplicate lines has to be printed only ones we have created another Set collection for this. */ if(s.contains(line)){ duplicates.add(line); }else{ s.add(line); } } for (String duplicate : duplicates) { w.println(" These are the duplicates lines that are to be printed only once :: " + duplicate); } for (String text : s) { w.println(text); } } /** * The driver. Open a BufferedReader and a PrintWriter, either from System.in * and System.out or from filenames specified on the command line, then call doIt. * @param args */ public static void main(String[] args) { try { BufferedReader r; PrintWriter w; if (args.length == 0) { r = new BufferedReader(new InputStreamReader(System.in)); w = new PrintWriter(System.out); } else if (args.length == 1) { r = new BufferedReader(new FileReader(args[0])); w = new PrintWriter(System.out); } else { r = new BufferedReader(new FileReader(args[0])); w = new PrintWriter(new FileWriter(args[1])); } long start = System.nanoTime(); doIt(r, w); w.flush(); long stop = System.nanoTime(); System.out.println("Execution time: " + 10e-9 * (stop-start)); } catch (IOException e) { System.err.println(e); System.exit(-1); } } }