Question
In this lab, we return to the FileProcessor framework from Lab 6 for writing tools that process text files one line at the time. This
In this lab, we return to the FileProcessor framework from Lab 6 for writing tools that process text files one line at the time. This time this framework is used to implement another Unix command line tool tail that can be used to extract the last n lines of the given file. Write a class Tail that extends FileProcessor> and has the following methods:
public Tail(int n)
The constructor that stores its argument n, the number of last lines to return as the result, into a private data field. In this lab, you have to choose for yourself what instance fields you need to define in your class to make the required methods work.
@Override public void startFile()
Start processing a new file from the beginning. Nothing to do here, really.
@Override public void processLine(String line)
Process the current line. Do something intelligent here. Be especially careful not to be a "Shlemiel" so that your logic of processing each line has to loop through all of the previous lines that you have collected and stored so far. As a hint to avoid being a "Shlemiel", note that the List that you create and return does not necessarily have to be specifically an ArrayList, but perhaps some other subtype of List that allows O(1) mutator operations at both of its ends would be much more appropriate to use here.
@Override public List endFile()
Returns a List instance that contains precisely the n most recent lines that were given as arguments to the method processLine in the same order that they were originally read in. If fewer than n lines have been given to the method processLine since the most recent call to the method startFile, this list should contain as many lines as have been given.
Warandpeace.txt download for testing
https://www.dropbox.com/s/8igts6fzz8ubws4/warandpeace.txt?dl=0
*TEST FILE*
import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.util.Random; import java.io.*; import java.util.*; import java.util.zip.CRC32; public class TailTest { @Test public void scanWarAndPeace() throws IOException { CRC32 check = new CRC32(); int totalLines = 0; for(int k = 10; k <= 100000; k = k * 10) { Tail tail = new Tail(k); BufferedReader fr = new BufferedReader( new InputStreamReader(new FileInputStream("warandpeace.txt"), "UTF-8") ); List result = tail.processFile(fr); totalLines += result.size(); fr.close(); for(String line: result) { check.update(line.getBytes()); } } assertEquals(10 + 100 + 1000 + 10000 + 63852, totalLines); assertEquals(1946545910L, check.getValue()); } }
*FILE PROCESSOR*
import java.io.BufferedReader;
import java.io.IOException;
public abstract class FileProcessor
protected abstract void startFile();
protected abstract void processLine(String line);
protected abstract R endFile();
public final R processFile(BufferedReader in) throws IOException {
startFile();
String line = "";
while ((line = in.readLine()) != null) {
processLine(line);
}
R result = endFile();
return result;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started