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

below is the skeleton file for these question. JAVA you can use any sample input

ID: 3755676 • Letter: B

Question

below is the skeleton file for these question. JAVA

you can use any sample input

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 Part3 {

/**

* 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);

}

}

}

Explanation / Answer

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.ArrayList;

import java.util.Collections;

public class Part3 {

/**

*

* 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 {

ArrayList<String>al = new ArrayList<String>();

String line=r.readLine();

// reading the lines into arraylist

while(line!=null){

al.add(line);

line = r.readLine();

}

// sorting the lines in the arraylist

Collections.sort(al);

// writing the arrayList

for(String str:al){

w.println(str);

}

}

/**

*

* 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);

}

}

}