1 answer

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

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

Part0.java is a sample program that reads data one line at a time from some input source and writes data to an output destination.  You should use this as a basis for code. 
5) [12.5 marks] Read the whole input one line at a time. Then output all lines sorted by length, with the shortest lines first. In the case where two lines have the same length, resolve their order using the usual "sorted order". 
import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashSet; import java.util.Iterator; 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<>();          for (String line = r.readLine(); line != null; line = r.readLine()) {             s.add(line);         }          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);         }     } } 
import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter;  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     }      /**      * 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);         }     } } 

Answers

Abc

package abc2;  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<String> s = new HashSet<String>();         Set<String> duplicates = new HashSet<String>();          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 abc2;  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<String> s = new HashSet<String>();         Set<String> duplicates = new HashSet<String>();          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); } } }

.

Similar Solved Questions

1 answer
Assume the following information: Milling Department Materials Conversion Total Cost of beginning work in process inventory...
Assume the following information: Milling Department Materials Conversion Total Cost of beginning work in process inventory $ 10,000 $ 15,000 $ 25,000 Costs added during the period 291,600 385,000 676,600 Total cost $ 301,600 $ 400,000 $ 701,600 Assume the equivalent units of production for material...
1 answer
A converging lens makes an image of an object located a finite distance away. Which of...
A converging lens makes an image of an object located a finite distance away. Which of the following statements is true? A. The image is virtual B. The image is real C. the object distance is positive D. the object distance is negative E. the image can be either real or virtual...
1 answer
If a 490 mL ordinary glass beaker is filled to the brim with ethyl alcohol at...
If a 490 mL ordinary glass beaker is filled to the brim with ethyl alcohol at a temperature of 6.00°C, how much (in mL) will overflow when their temperature reaches 22.0°C? mL (b) How much less water would overflow under the same conditions? mL...
1 answer
15) 5. For this molecule: a. How many chemically different hydrogens? b. How many chemically different...
15) 5. For this molecule: a. How many chemically different hydrogens? b. How many chemically different carbons? c. What would be the mass of the molecular ion in El-MS?...
1 answer
Pina Company has the following stockholders’ equity accounts at December 31, 2020. Common Stock ($100 par...
Pina Company has the following stockholders’ equity accounts at December 31, 2020. Common Stock ($100 par value, authorized 7,600 shares) $505,600 Retained Earnings 291,700 Prepare entries in journal form to record the following transactions, wh...
1 answer
Question Help Claim: Most adults would erase all of their personal information online if they could....
Question Help Claim: Most adults would erase all of their personal information online if they could. A software firm survey of 586 randomly selected adults showed that 57% of them would erase all of their personal information online if they could. Find the value of the test statistic The value of th...
1 answer
Write an open_file() and a function that reads a csv data file and returns a dictionary...
Write an open_file() and a function that reads a csv data file and returns a dictionary Microsoft Word - project09_final_RJE.docx 1/17 This project focuses on analyzing a publicly available dataset containing information about the spread of nCoV. Since this is an active situation, this dataset is c...
1 answer
Accounting
Bob Browning's weekly gross earnings for the present week were $2,690. Browning has two exemptions. Using the wage bracket withholding table in Exhibit 2 with a $75 standard withholding allowance for each exemption, what is Browning's federal income tax withholding? Round your answ...
1 answer
Epidemiology: confounding (10) In the early 1940s, epidemiologists and public health officials set out to prove...
Epidemiology: confounding (10) In the early 1940s, epidemiologists and public health officials set out to prove hat smoking causes lung cancer. Epidemiologists designed studies, analyzed data, and constructed logical arguments to demonstrate this relationship. Cigarette companies argued that ass...
1 answer
8. (a) For what values of a, b, and c can the matrix A below be...
8. (a) For what values of a, b, and c can the matrix A below be diagonalized? 2 marks 0 a 1 0 =10 0 0 c b) Let A be an n xn matrix. In class, we showed that if R" admits a basis of eigenvectors of A, then there is an invertible atrix P such that P- AP is diagonal (i.e., is zero everywhere apart ...
1 answer
Describe the life cycle of Toxoplasma gondii in the nonfeline host and describe its importance to...
Describe the life cycle of Toxoplasma gondii in the nonfeline host and describe its importance to veterinary and human medicine. Also discuss how transmission to humans can be prevented. 
...
1 answer
How do you find the limit of #(1+2/x)^x# as x approaches #oo#?
How do you find the limit of #(1+2/x)^x# as x approaches #oo#?...
1 answer
Suppose that a particle starts at the origin of the real line and moves along the...
Suppose that a particle starts at the origin of the real line and moves along the line in jumps of one unit. For each jump, the probability is p(0≤p≤1) that the particle will jump one unit to the left and the probability is 1−p that the particle will jump one unit to the right. Find the ...
1 answer
6) Balance the following unbalanced oxidation-reduction reaction for acid conditions? [12 points] MnOd(aq) + H2SO (aq)...
6) Balance the following unbalanced oxidation-reduction reaction for acid conditions? [12 points] MnOd(aq) + H2SO (aq) Mr"(aq) + S0? (aq) 7) Small amounts of the metal barium (Ba, MW = 137.33 g/mol) can be produced by electrolysis. The half-reaction for the production of barium metal is Ba? +2 B...
1 answer
Third time asking this question. It's a doozy apparently. Hoping someone can lock it down for...
Third time asking this question. It's a doozy apparently. Hoping someone can lock it down for me! Assume none of the answers you see are correct. Appreciate the help :) Watch Corporation of Switzerland claims that its watches on average will neither gain nor lose time during a week. A sample of...
1 answer
(5) Recall that X ~Uniform(10, 1,2,... ,n - 1)) if if k E (0, 1,2,... ,n -1, P(x k)0 otherwise (a...
(5) Recall that X ~Uniform(10, 1,2,... ,n - 1)) if if k E (0, 1,2,... ,n -1, P(x k)0 otherwise (a) Determine the MGF of such a random variable. (b) Let X1, X2, X3 be independent random variables with X1 Uniform(10,1)) X2 ~Uniform(f0, 1,2]) Xs~ Uniform(10, 1,2,3,4]). X3 ~ U x2 ~ Uniform(10, 1,2)) 13U...
1 answer
Homework: HW 14 - Chapter 14 Score: 0 of 1 pt 4.2.77 The following tables gives...
Homework: HW 14 - Chapter 14 Score: 0 of 1 pt 4.2.77 The following tables gives the population for a country, in milions of persons, for the years 1990-2009. Forecast the population in the years 2010 and 2011. Save 11 of 13 (10 complete) HW Score: 74.36%, 9.67 of 13 pts Question Help * Year 1990 199...