Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

need help with this in java Write a queue client, LineNum, that takes an integer command line argument n and prints the nth string from

need help with this in java Write a queue client, "LineNum," that takes an integer command line argument n and prints the nth string from the first string found on standard input. [MO6.2]
Please note that you should use an object of the Queue class given in the textbook (program 4.3.6) to implement it. You should add the strings inputted by the user to a queue using the enqueue method. After the user has finished inputting strings (and they've been added to the queue) you should remove "n" strings from the queue by calling the dequeue method (which would also return the string that is removed from the queue) n times. The string that is returned by the dequeue method when it is called the nth time should then be printed out.
Sample runs would be as follows.
>java LineNum 2
apple
save
that
is
too
me
2 from the first is save
>java LineNum 3
this
is
not
a
shorter
version
3 from the first is not the code referenced in the question is public class Queue
{
private Node first;
private Node last;
private class Node {
private Item item;
private Node next;
}
public boolean isEmpty(){
return first == null;
}
public void enqueue(Item item){
Node oldlast = last;
last = new Node();
last.item = item;
last.next = null;
if (isEmpty()) first = last;
else oldlast.next = last;
}
public Item dequeue(){
if (isEmpty()) throw new NoSuchElementException("Queue underflow");
Item item = first.item;
first = first.next;
n--;
if (isEmpty()) last = null; // to avoid loitering
return item;
}
public static void main(String[] args){
Queue queue = new Queue();
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Integration Services

Authors: Brian Knight, Devin Knight

1st Edition

1118850904, 9781118850909

More Books

Students also viewed these Databases questions