Question

In: Computer Science

The input is broken into chunks of consecutive lines, where each pair of consecutive chunks is...

The input is broken into chunks of consecutive lines, where each pair of consecutive chunks is separated by a line containing "----snip----". Read the entire input and break it into chunks C1,…,Ck. Then output the chunks in reverse order Ck,…,C1 but preserving the order of the lines within each chunk.

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 Part7 {
   
   /**
    * 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: " + 1e-9 * (stop-start));
      } catch (IOException e) {
         System.err.println(e);
         System.exit(-1);
      }
   }
}

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks



//Part7.java

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;

public class Part7 {

        /**
         * 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 {
                // creating a list of list of strings to store chunks of lines
                ArrayList<ArrayList<String>> chunks = new ArrayList<ArrayList<String>>();
                // creating a list to store current chunk
                ArrayList<String> currentChunk = new ArrayList<String>();
                // looping through each line in the input buffer
                for (String line = r.readLine(); line != null; line = r.readLine()) {
                        // checking if line equals "----snip----"
                        if (line.equals("----snip----")) {
                                // adding current chunk to chunks list
                                chunks.add(currentChunk);
                                // reinitializing currentChunk to store next chunk of lines
                                currentChunk = new ArrayList<String>();
                        } else {
                                // else, adding line to currentChunk
                                currentChunk.add(line);
                        }
                }
                // at the end, if currentChunk is not empty, adding to chunks
                if (!currentChunk.isEmpty()) {
                        chunks.add(currentChunk);
                }
                // now looping through each chunk in reverse order
                for (int i = chunks.size() - 1; i >= 0; i--) {
                        // looping through each line in current chunk, in normal order,
                        // displaying it
                        for (String line : chunks.get(i)) {
                                System.out.println(line);
                        }
                }
        }

        /**
         * 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: " + 1e-9 * (stop - start));
                } catch (IOException e) {
                        System.err.println(e);
                        System.exit(-1);
                }
        }
}

Related Solutions

Whack Ltd, a tennis racquet manufacturer, has broken the production process into three lines, each in...
Whack Ltd, a tennis racquet manufacturer, has broken the production process into three lines, each in a different department: framing, stringing, and finishing. The following information relates to each department for the month of April:                              FRAMING STRINGING FINISHING Direct Costs       $20,000      $30,000         $66,000 Indirect Costs $10,000      $20,000         $30,000 Labour Hours   2,000 hrs   5,000 hrs      3,000 hrs Machine hours 500 hrs      0 hrs            0 hrs Production for April is 10,000 racquets Refer to the table above. The overhead recovery rate for stringing...
Write a program in C++ (parking.cc) that reads a group of input lines. Each line contains...
Write a program in C++ (parking.cc) that reads a group of input lines. Each line contains an A for arrival or a D for departure, which is terminated by a :, and a license plate number, which is terminated by a :. The program should print a message each time a car arrives or departs. When a car arrives, the message should specify when the garage is full. If there is no room for a car, the car simply leaves....
Write the digestive organ(s) that is/are associated with each statement. Where carbohydrates are first broken down...
Write the digestive organ(s) that is/are associated with each statement. Where carbohydrates are first broken down (in humans) Where plant material is fermented (in foregut fermenting herbivores) Where water is reabsorbed (in humans) Where gastric juices break down proteins (in humans) Where plant material is fermented (in hindgut fermenting herbivores) Where most nutrients are absorbed and enzymes are also secreted (in humans) Where enzymes and neutralizing bicarbonate is secreted (in humans) Where bile is stored (in humans)
Write a short C++ program that takes all the lines input to standard input and writes...
Write a short C++ program that takes all the lines input to standard input and writes them to standard output in reverse order. That is, each line is output in the correct order, but the ordering of the lines is reversed. Please use vector datatype standaard library #include <vector> Thanks
in P2P, each peer only upload chunks to a limited number of peers? Why is the...
in P2P, each peer only upload chunks to a limited number of peers? Why is the Bit torrent designed in such a way?
In the case of the baroreflex, what is the sensory input? Where is the sensory input...
In the case of the baroreflex, what is the sensory input? Where is the sensory input sent and integrated within the CNS (where do the receptor neurons send their signal)? Note: this is the same area of the brain from which autonomic outflow originates.        c. Name 3 anatomical locations within the cardiovascular system where efferent output is sent (these correspond with the three factors that can affect blood pressure in the short-term)
Using Python: 1. Compute the difference of differences between consecutive numbers of a series: input ser...
Using Python: 1. Compute the difference of differences between consecutive numbers of a series: input ser = pd.Series([1, 3, 6, 10, 15, 21, 27, 35]) output: [nan, 2.0, 3.0, 4.0, 5.0, 6.0, 6.0, 8.0] [nan, nan, 1.0, 1.0, 1.0, 1.0, 0.0, 2.0] 2. Compute the euclidean distance between two series: Input: p = pd.Series([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) q = pd.Series([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]) Desired Output: 18.165
What kind of conic section (or pair of straight lines) is given by the quadratic form?...
What kind of conic section (or pair of straight lines) is given by the quadratic form? Transform it to principal axes. Express xT = [x1 x2] in terms of the new coordinate vector ?T = [y1 y2] Show details.    x1^2 + x1x2+x2^2=10
Fix this broken code? # Get our input from the command line import sys string =...
Fix this broken code? # Get our input from the command line import sys string = sys.argv[1] # Your code goes here if string 'Bingo' print('Missed') else: print('Hit!')
The runtime of a program is proportional to n1.5 where n is the input size.  For input...
The runtime of a program is proportional to n1.5 where n is the input size.  For input size 100 the runtime is 51 ms.  What is the new runtime when the input size is quadrupled?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT