7 5 Marks Read Input One Line Time Output Current Line Already Read Least 1000 Lines Great Q43904619
7. [5 marks] Read the input one line at a time andoutput the current line if and only if you have already read atleast 1000 lines greater than the current line and at least 1000lines less than the current line. (Again, greater than and lessthan are with respect to the ordering defined byString.compareTo().)
package comp2402a1;
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: theorder
* of the output is unspecified and may have nothing to do with theorder
* 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) throwsIOException {
Set<String> s = new HashSet<>();
for (String line = r.readLine(); line != null; line =r.readLine()) {
s.add(line);
System.out.println(line);
}
for (String text : s) {
w.println(text);
System.out.println(“t”);
}
}
/**
* The driver. Open a BufferedReader and a PrintWriter, either fromSystem.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);
}
}
}
Expert Answer
Answer to 7. [5 marks] Read the input one line at a time and output the current line if and only if you have already read at least…
OR