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

Please answer the question correctly and USE THE MOST EFFICIENT TECHNIQUE OF JAV

ID: 3872664 • Letter: P

Question

Please answer the question correctly and USE THE MOST EFFICIENT TECHNIQUE OF JAVA COLLECTION FRAME WORK to answer it. It is very essential that yoy do. A demo code required for the question is given below.

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

QUESTION:Imagine each input line is numbered, starting from 0. Read the whole file and then output the even-numbered lines (in order) followed by the odd-numbered lines (also in order).

Demo code(Note: write required code in space specified):

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

Please find my implementation.

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

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) throws IOException {

       // Your code goes here - see Part0 for an example

      

       LinkedList<String> evenLines = new LinkedList<String>();

       LinkedList<String> oddLines = new LinkedList<String>();

      

       String line;

       int count = 0;

       while((line = r.readLine()) != null) {

           if (count%2 == 0) {

               evenLines.add(line);

           } else {

               oddLines.add(line);

           }

          

           count++;

       }

      

       System.out.println("Even numbered lines: ");

       for(String l : evenLines)

           System.out.println(l);

      

       System.out.println(" Even numbered lines: ");

       for(String l : oddLines)

           System.out.println(l);

   }

   /**

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

       }

   }

}