Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

java programming language Exercise Tail Function. The unix tail function takes t

ID: 3749545 • Letter: J

Question

java programming language

Exercise Tail Function. The unix tail function takes two arguments, the name of a file and a non-negative integer, n. It then opens and reads the specified file line by lne, printing the last n lines to the console. Your job is to provide a Java implementation of this function using your own implementation of a FixedCapacityQueue In a project named Fixed CapacityQueue, complete the definition of the following API for a LIFO queue: public class FixedCapacityQueue implements IterablecItem> FixedCapacityQueue (int capacity) // create empty queue with indicated capacity // add an item // remove/return the least recently added an item // is the quuefull? // is the queue empty? // number of items i the queue // front-to-back iterator void enqueue(Item item) Item dequeue) boolean isFul10 boolean isEmptyO int size() Iterator iterator As we have seen in the implementations for array based stacks and queues, must complete the definition of an appropriate class implemening Iterator so that the method iterator can initiate an object from this class. The main method of this queue class should test the definition by emulating the tail function. That is, it shuld take two command-line parameters, a string representing the name of a file, and a non-negative integer. It should open the specifed file, read le by lne, and finally print the last lines as specified. The lines that are read should be stored in a FixedCapacityQueue that is as small as possible to accomplish this task and the final lines should be printed using a foreach construction (which requires a functioning iterator)

Explanation / Answer


Given below is the code for the question. In order to run the code, you will need to provide 2 command line arguments - filename, and no. of lines to display
e.g. java FixedCapacityQueue input.txt 3
If running in eclipse, click Run/Run Configuration.../ In the window that comes, click Arguments tab, there you type input.txt 3

To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.Scanner;
public class FixedCapacityQueue<Item> implements Iterable<Item> {
private Item[] data;
private int size;
public FixedCapacityQueue(int capacity){
data = (Item[]) new Object[capacity];
size = 0;
}
public void enqueue(Item item){
if(!isFull())
data[size++] = item;
}
public Item deque(){
if(isEmpty())
return null;
else
{
Item val = data[0];
//push all other elements one position to left after removing the 0th item
for(int i = 1; i < size; i++)
data[i-1] = data[i];
size--;
return val;
}
}
public boolean isFull(){
return size == data.length;
}
public boolean isEmpty(){
return size == 0;
}
public int size(){
return size;
}
@Override
public Iterator<Item> iterator() {
return new QueueIterator();
}
public class QueueIterator implements Iterator<Item>{
int current = 0;
@Override
public boolean hasNext() {
return current < size;
}
@Override
public Item next() {
Item val = data[current];
current++;
return val;
}
}
public static void main(String[] args) {
if(args.length != 2){
System.out.println("Please provide filename and the no. of lines to display as command line arguments");
System.out.println("for example, c:\> java FixedCapacityQueue input.txt 5");
return;
}
Scanner infile = null;
try {
infile = new Scanner(new File(args[0]));
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
int numLines = Integer.parseInt(args[1]);
FixedCapacityQueue<String> queue = new FixedCapacityQueue<String>(numLines);
while(infile.hasNextLine()){
if(queue.isFull())
queue.deque();
queue.enqueue(infile.nextLine());
}
infile.close();
System.out.println("The last " + numLines +" in the file " + args[0]);
for (String line : queue) {
System.out.println(line);
}
}
}

sample input file: input.txt
=============
Burger 250
Icecream 300
Juice 200
Cola 400
output
=======
The last 2 in the file inputFile.txt
Juice 200
Cola 400