Question: 1. Paste the code below into your Java IDE and make sure it runs correctly. 2. Create a program that receives numeric data from the
1. Paste the code below into your Java IDE and make sure it runs correctly.
2. Create a program that receives numeric data from the user and then adds it to a queue.
3. Create a program that receives text based data and then adds it to a queue.
4. Create a program that automatically populates a queue on start up with 5 pieces of data and then displays each entry in the queue after receiving a command from the user.
package q_sample;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;
public class q_sample {
public static void main(String[] args) {
Queue myQueue = new LinkedList();
myQueue.offer("Monday");
myQueue.offer("Thusday");
boolean flag = myQueue.offer("Wednesday");
System.out.println("Wednesday inserted successfully? "+flag);
try {
myQueue.add("Thursday");
myQueue.add("Friday");
myQueue.add("Weekend");
} catch (IllegalStateException e) {
e.printStackTrace();
}
System.out.println("Pick the head of the queue: " + myQueue.peek());
String head = null;
try {
head = (String) myQueue.remove();
System.out.print("1) Push out " + head + " from the queue ");
System.out.println("and the new head is now: "+myQueue.element());
} catch (NoSuchElementException e) {
e.printStackTrace();
}
head = (String) myQueue.poll();
System.out.print("2) Push out " + head + " from the queue");
System.out.println("and the new head is now: "+myQueue.peek());
System.out.println("Does the queue contain 'Weekend'? " + myQueue.contains("Weekend"));
System.out.println("Does the queue contain 'Monday'? " + myQueue.contains("Monday"));
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
