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: 3872665 • 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 you do. Please make sure it is very efficient and doesnt run out of memory. 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:Read the input one line at a time and, if the line numbered i is empty and i 42, then output the line numbered (i - 42). In this way, the number of lines you output is no more than the number of blank lines in the input.

DEMO CODE(SPACE TO WRITE CODE IS GIVEN):

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

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;

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

       String line;

       int count = 0;

      

       // reading lines

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

           count++;

           if (line.isEmpty() && (count >= 43)) {

               System.out.println(count);

           }

       }

   }

   /**

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

       }

   }

}