/** ***********************************************************************
 *  Compilation:  javac -Xlint Tail.java
 *  Execution:    java Tail
 *  Dependencies: StdIn.java StdOut.java 
 *  
 *
 * % java Tail testes/*
 * ==> testes/ind.txt<==
 * changed for light and transient causes; and accordingly all experience
 * hath shewn, that mankind are more disposed to suffer, while evils are
 * sufferable, than to right themselves by abolishing the forms to which
 * they are accustomed. But when a long train of abuses and usurpations,
 * pursuing invariably the same Object evinces a design to reduce them
 * under absolute Despotism, it is their right, it is their duty, to
 * throw off such Government, and to provide new Guards for their future
 * security.--Such has been the patient sufferance of these Colonies; and
 * such is now the necessity which constrains them to alter their former
 * Systems of Government.
 *
 * ==> testes/les-miserables.txt<==
 *
 *
 *  Most people start at our Web site which has the main PG search facility:
 *
 *       http://www.gutenberg.org
 *
 *  This Web site includes information about Project Gutenberg-tm,
 *  including how to make donations to the Project Gutenberg Literary
 *  Archive Foundation, how to help produce our new eBooks, and how to
 *  subscribe to our email newsletter to hear about new eBooks.
 *
 *  ==> testes/teste1.txt<==
 *  aaa bbbb aa aaa ccc dd
 *
 *  ==> testes/teste2.txt<==
 *  Como é bom estudar MAC0323!
 *  Como é bom estudar MAC0323!
 *
 *
 *************************************************************************/

import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.Queue;


public class Tail {
    // atributos de estado 
    private String fileName;
    private int k;
    private Queue<String> tail;

    public Tail(String fileName) {
        this(fileName, 10);
    }   

    public Tail(String fileName, int k) {
        this.fileName = fileName;
        this.k = k;
        prepareTail();
    }

    // Enqueue lines of the file
    private void prepareTail() {
        In in;
        try {
            in = new In("./"+fileName);
            tail = new Queue<>();
            while (!in.isEmpty()) {
                String s = in.readLine();
                if(tail.size() == k)
                    tail.dequeue();
                tail.enqueue(s);
            }
        }
        catch (IllegalArgumentException e) {
            StdOut.println(e);
        }
    }

    /** Returns an iterable object containing the last k lines.
     *  The value of k depends on the constructor.
     */
    public Iterable<String> lines() {
        // Create a copy of the object queue, so we won't have to
        // give a private object to the Client.
        Queue<String> tailPub = new Queue<>();
        while(tail.size() != 0)
            tailPub.enqueue(tail.dequeue());
        return tailPub;
    }

   /***************************************************************************
    *  métodos privados
    **************************************************************************/

    // apenas para o uso do unit test abaixo
    private static void use() {
        String message = "Use: java Tail [-n NUM] [FILE]\n" +
            "   Print the last 10 lines of each FILE to standard output.\n" +
            "   With more than one FILE, precede each with a header giving the file name.\n" +
            "   -n NUM: output the last NUM lines, instead of the last 10";
        StdOut.println(message);
    }
    
   /** Unit test
    */
    public static void main(String[] args) {
        if (args.length == 0) {
            use();
            return;
        }
        
        if (args[0].equals("-n")) {
            int k = Integer.parseInt(args[1]);
            for (int i = 2;  i < args.length; i++) {
                StdOut.println("==> " + args[i] + "<==");
                Tail tail = new Tail(args[i], k);
                for (String line: tail.lines()) {
                    StdOut.println(line);
                }
                StdOut.println();
            }
            
        }
        else {
            for (int i = 0;  i < args.length; i++) {
                StdOut.println("==> " + args[i] + "<==");
                Tail tail = new Tail(args[i]);
                for (String line: tail.lines()) {
                    StdOut.println(line);
                }
                StdOut.println();
            }
        }
    }
}


@@ -1939,32 +1939,25 @@
 e, int k) %7B%0A
-        
+%09
 this.fileNam
@@ -2221,16 +2221,25 @@
 sEmpty()
+ && k %3E 0
 ) %7B%0A    

Update EP4 to work with -n 0