Menu

5 Marks Read Input One Line Time Output Current Line Smaller Line Read Far Smaller Respect Q43902464

[5 marks] Read the input one line at a time and outputthe current line if and only if it is smaller than any other lineread so far. (Here, smaller is with respect to the usual order onStrings, as defined by String.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;

public class Part2 {

/**
* 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) throwsIOException {
// Your code goes here – see Part0 for an example
}

/**
* 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 [5 marks] Read the input one line at a time and output the current line if and only if it is smaller than any other line…

OR