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.
Make sure you submit your coding answers to this assessment before you leave class. Make sure everyone's name is on the submission.
Prof. Nizich
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"));
}
}
//This below program is the rectifyed one and it work
successfully
package com.acadgild;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;
public class q_sample {
public static void main(String[] args) {
//changes made in the below code by
adding String on both the sides of the interface as queue is an
interfave class.
Queue<String> myQueue = new
LinkedList<String>();
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"));
}
}
#program -02
package com.acadgild;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Second {
public static void main(String[] args) {
// TODO Auto-generated method
stub
Scanner scan = new
Scanner(System.in);
System.out.println("Enter the
number as theinput!");
int x= scan.nextInt();
Queue queue = new
LinkedList();
queue.add(x);
for (Object object : queue) {
System.out.println("Successfully added the input taken from the
user"
+ "Added to the queue"+object.toString());
}
}
}
#Program - 03
package com.acadgild;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Third {
public static void main(String[] args) {
// TODO Auto-generated method
stub
Scanner scan = new
Scanner(System.in);
System.out.println("Enter the
number as theinput!");
int x= scan.nextLine();//Change the
int to the Line
Queue queue = new
LinkedList();
queue.add(x);
for (Object object : queue) {
System.out.println("Successfully added the input taken from the
user"
+ "Added to the queue"+object.toString());
}
}
}
#program -04
package com.acadgild;
import java.util.*;
public class QueueDemoQueue {
static String newLine = System.getProperty("line.separator");
public static void main(String[] args) {
System.out.println(newLine + "Queue in Java" + newLine);
System.out.println("-----------------------" + newLine);
System.out.println("Adding items to the Queue" + newLine);
//Creating queue would require you to create instannce of
LinkedList and assign
//it to Queue
//Object. You cannot create an instance of Queue as it is
abstract
Queue queue = new LinkedList();
//you add elements to queue using add method
queue.add("A");
queue.add("B");
queue.add("C");
queue.add("D");
queue.add("E");
System.out.println(newLine + "Items in the queue..." + queue +
newLine);
//You remove element from the queue using .remove method
//This would remove the first element added to the queue, here
Java
System.out.println("remove element: " + queue.remove() +
newLine);
//.element() returns the current element in the queue, here when
"java" is removed
//the next most top element is .NET, so .NET would be
printed.
System.out.println("retrieve element: " + queue.element() +
newLine);
//.poll() method retrieves and removes the head of this queue
//or return null if this queue is empty. Here .NET would be printed
and then would
//be removed
//from the queue
System.out.println("remove and retrieve element, null if empty: " +
queue.poll() +
newLine);
//.peek() just returns the current element in the queue, null if
empty
//Here it will print Javascript as .NET is removed above
System.out.println("retrieve element, null is empty " +
queue.peek() + newLine);
}}
Get Answers For Free
Most questions answered within 1 hours.