Please answer question correctly and show the result of the working code. The ac
ID: 3749467 • 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
// Attaching Code for Part 7
/* START OF CODE */
/*
* Read the input one line at a time. At any point after reading the first 42 lines, if some
* line is blank (i.e., a string of length 0) then output the line that occurred 42 lines prior to
* that one. For example, if Line 242 is blank, then your program should output line 200. This
* program should be implemented so that it never stores more than 43 lines of the input at any
* given time.
*/
// JRE used is jre1.8.0_91
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.Queue;
import java.util.LinkedList;
public class Part7 {
/**
* 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 {
// Using q queue which will at any time store a maximum of 43 elements
Queue q = new LinkedList();
int i = 0;
for (String line = r.readLine();line != null;line = r.readLine()){
i += 1;
q.add(line);
if( i>=43 ){
String head = (String) q.remove();
if(line.length() == 0 )
w.println(head);
}
}
}
/**
* 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);
}
}
}
/* END OF CODE */
// For input , a sample input file contains the following
shshshhs
shshshshhs1233
sgshsshshs
hshshshssh
shshshshsh
hshshshshsh
hshshshshsh
hshshshshsh
hshshshshsh
hshshshshsh
shshshshshshshsh
hshshshshsh
hshshshshsh4556
vhshshshshsh
shshs
sgssgs
sgssgs
hshshshshsh
hshshshshsh
hshshshshsh
hshshshshsh
hshshshshsh
shshshshshshshsh
hshshshshsh
hshshshshsh
vhshshshshsh
shshs
sgssgs
sgssgs
hshshshshsh
hshshshshsh
hshshshshsh
hshshshshsh
hshshshshsh
shshshshshshshsh
hshshshshsh
hshshshshsh
vhshshshshsh
shshs
sgssgs
sgssgs
hshshshshsh
hshshshshsh
hshshshshsh
hshshshshsh
hshshshshsh
shshshshshshshsh
hshshshshsh
hshshshshsh
vhshshshshsh
shshs
sgssgs
sgssgs
ssgsggsgs // Output
shshshshhs1233
hshshshshsh4556
Execution time: 0.00865828
//Explanation :- We see that line Number 44 and 55 are blank. So, the strings at line number 2 and 13 should be printed respectively which is the output of the program as pasted above.