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: 3873040 • 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 store them in a buffer (a data structure of your choosing). If the current line has odd length, then output the oldest line in the buffer (and remove it from the buffer). In this way, the total number of output lines will be equal to the number of odd-length input lines.

DEMOCODE;

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

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

I have modified the doIt method, Please note that, input will terminate when user will type "exit" word in the console/file. You can modify your exit condition by replacing the keyword with exit. Cheers :)

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;

import java.util.Queue;

public class Part4 {

/**

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

String sCurrentLine;

Queue<String> q = new LinkedList<String>();

while (!(sCurrentLine = r.readLine()).equals("exit")) {

if(sCurrentLine.length()%2 ==0)

{

q.add(sCurrentLine);

}

else

{

if(!q.isEmpty())

{

String line = q.remove();

w.println(line);

}

q.add(sCurrentLine);

}

}

w.println("printing queue items");

for(String item:q)

{

w.println(item);

}

}

/**

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

}

}

}