Question

In: Computer Science

1. Write a queue client, "LineNum," that takes an integer command line argument “n” and prints...

1. 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 would need to use a queue to implement it for full credit. You should add the strings inputted by the user to a queue using the enqueue method. Then you should remove and return "n" strings from the queue using the dequeue method. The nth string that is returned by the dequeue method should then be printed out.

  • Sample runs would be as follows.

    >java LineNum 2
    apple
    save
    that
    is
    too
    me
    <CTRL-D>
    2 from the first is save

    >java LineNum 3
    this
    is
    not
    a
    shorter
    version
    <CTRL-D>
    3 from the first is not

This is the code I have, but i keep getting errors.

public class LineNum { // public class LineNum
public static void main(String[] args) { // main method
int n = Integer.parseInt(args[0]); // variable n parses integers from args
Queue<String> queue = new Queue<String>(); // new String queue
//LinkedList<java.lang.String> Dequeue = new LinkedList();
while (!StdIn.isEmpty()) { // while standard input is empty
String item = StdIn.readString();
// String item read string from standard input
if (!item.equals("-")) // if item equal - then ebqueue item
queue.enqueue(item);
else if (!queue.isEmpty()) // otherwise if queue is empty
StdOut.println(queue.dequeue() + " "); // print dequeue + a space
}
  
StdOut.println(queue);
for (int i = 1; i < n; i++) // for loop i=1, <n, increments i
queue.dequeue(); // dequeues element on each loop until i = n
// StdOut.print(queue.size() + " left on queue)");
// StdOut.println("The dequeue contains: " +queue.dequeue() + " ");
StdOut.println(n+ " from the first is " +queue.dequeue());
// Prints number from first and current element in dequeue
  
}
}
these are the errors i'm getting:

\introcs> javac LineNum.java
LineNum.java:5: error: cannot find symbol
queue<String> elements = new LinkedList<>(); // new String queue
^
symbol: class queue
location: class LineNum
LineNum.java:5: error: cannot find symbol
queue<String> elements = new LinkedList<>(); // new String queue
^
symbol: class LinkedList
location: class LineNum
LineNum.java:11: error: cannot find symbol
queue.enqueue(item);
^
symbol: variable queue
location: class LineNum
LineNum.java:12: error: cannot find symbol
else if (!queue.isEmpty()) // otherwise if queue is empty
^
symbol: variable queue
location: class LineNum
LineNum.java:13: error: cannot find symbol
StdOut.println(queue.dequeue() + " "); // print dequeue + a space
^
symbol: variable queue
location: class LineNum
LineNum.java:16: error: cannot find symbol
StdOut.println(queue);
^
symbol: variable queue
location: class LineNum
LineNum.java:18: error: cannot find symbol
queue.dequeue(); // dequeues element on each loop until i = n
^
symbol: variable queue
location: class LineNum
LineNum.java:21: error: cannot find symbol
StdOut.println(n+ " from the first is " +Queue.dequeue());
^
symbol: variable Queue
location: class LineNum
8 errors

Solutions

Expert Solution

CODE

import java.util.LinkedList;

import java.util.Queue;

import java.util.Scanner;

public class Main { // public class LineNum

public static void main(String[] args) { // main method

int n = Integer.parseInt(args[0]); // variable n parses integers from args

Queue<String> queue = new LinkedList<String>(); // new String queue

//LinkedList<java.lang.String> Dequeue = new LinkedList();

Scanner sc = new Scanner(System.in);

while(true) {

String item = sc.nextLine();

// String item read string from standard input

if (!item.equals("-")) // if item equal - then ebqueue item

queue.add(item);

else

break;

}

System.out.println(queue);

if (n > queue.size()) {

System.out.println("not enough elements...");

return;

}

for (int i = 1; i < n; i++) // for loop i=1, <n, increments i

queue.remove(); // dequeues element on each loop until i = n

// StdOut.print(queue.size() + " left on queue)");

// StdOut.println("The dequeue contains: " +queue.dequeue() + " ");

System.out.println(n+ " from the first is " +queue.remove());

// Prints number from first and current element in dequeue

}

}


Related Solutions

Write a C program called test that takes one command line argument, an integer N. When...
Write a C program called test that takes one command line argument, an integer N. When we run test: ./test N the program will do this: the parent process forks N child processes each child process prints its process ID, exits the parent process waits for all child processes to exit, then exits
Python Write a program that takes a text filename as command line argument, and prints number...
Python Write a program that takes a text filename as command line argument, and prints number of times each letter occurred in this file.
Write a program that takes an integer N from the command line and uses StdRandom.uniform() to...
Write a program that takes an integer N from the command line and uses StdRandom.uniform() to generate a random sequence of integers be- tween 0 and N – 1. Run experiments to validate the hypothesis that the number of integers generated before the first repeated value is found is ~√?N/2.
Write a java code that: 1) Takes as an argument an integer number, say N 2)...
Write a java code that: 1) Takes as an argument an integer number, say N 2) Creates an array of size N 3) Populates the array with random numbers between 0 and 10 * N. This is, the values of the elements of the array should be random numbers between 0 and 10 * N. 4) Finally, the code outputs the index of the smallest element and the index of the largest element in the array
The following code takes in a command line argument and copies it into a buffer before...
The following code takes in a command line argument and copies it into a buffer before working on it. Explain in detail any problems that you see in the following C code and how you would fix it. Note only describe how you would fix it, do not actually rewrite or give me another version of the code. void bad_function(char *input) { char dest_buffer[32]; char input_len = strlen(input); if (input_len < 32) { strcpy(dest_buffer, input_len); printf(“Command line argument is %s.\n”,dest_buffer);...
Write a C or C++ program A6p2.c(pp) that accepts one command line argument which is an integer n between 2 and 4 inclusi...
Write a C or C++ program A6p2.c(pp) that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60 random integers between 1 and 49 inclusive and store them in a 5 by 12 two dimensional integer array (e.g.,int a[5][12];). Use pthread to create n threads to square all 60 array elements. You should divide this update task among the n threads as evenly as possible. Print the array both before and after the...
Write a program that prints the sum of its command-line arguments (assuming they are numbers). For...
Write a program that prints the sum of its command-line arguments (assuming they are numbers). For example, java Adder 3 2.5 -4.1 should print The sum is 1.4
Write a C++ program that prints out all of the command line arguments passed to the...
Write a C++ program that prints out all of the command line arguments passed to the program. Each command line argument should be separated from the others with a comma and a space. If a command line argument ends in a comma, then another comma should NOT be added
Write a Java application that accepts a bar code as a command line parameter and prints...
Write a Java application that accepts a bar code as a command line parameter and prints out the ZIP code. Assume that the bar code uses the symbols "|" and ":" for the long and short bars, respectively. Provide warnings on errors in the bar code specifying what exactly is wrong. The bar code input should be in the format specified in Problem 1, including the pair of the full bars at the beginning and at the end. Important: The...
a). Write a program that asks the user to enter an integer N and prints two...
a). Write a program that asks the user to enter an integer N and prints two integers, root and power, such that 1 < power < 6 and N = root ** power. If no such pair of integers exists, it should print a message to that effect. There are two loops, one for power and one for root. Order the loops so that if N = 64, then your program find that N = 8 ** 2 rather than...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT