This project needs to be done in Java. Thank you for your help. Objectives: *Cre
ID: 3907274 • Letter: T
Question
This project needs to be done in Java. Thank you for your help.
Objectives: *Create a queue data structure. Description: For this assignment you will be writing your own classes to implement a queue data structure. You will write your own queue data structure, not the one supplied in the Java Collections library. This means that you will write your own constructor(s), methods, and instance variables. You will implement this queue using a LinkedList type of data structure The IPhone77 is going to be released in three days. It's very important to you to be one of the first people to own this new piece of technology. So, you decide to line up outside the Apple Store in the Long Beach Marina the night before. When you and your friends get there, you see a line in front of the store. You realize that this line is "first come first served". You place yourselves in the next available spots in the line. Two hours before the store opens, a sales person comes out of the store with paper and pencil and goes through the entire line getting each person's name and counting the number of people in line. He puts up a sign at the end of line stating that no one else may join the line. Every few minutes after that, the store rep checks the beginning of the line to make sure that it is the same person. When the store finally opens, the people in line are sold phones. Make sure you use appropriate exception handling.Explanation / Answer
Given below is the code for the question.
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
MyQueue.java
-----
public class MyQueue {
class Node{
String data;
Node next;
Node(String data){
this.data = data;
this.next = null;
}
}
private Node first, last;
public MyQueue(){
first = null;
last = null;
}
public boolean isEmpty(){
return first == null;
}
public int size(){
int s = 0;
Node n = first;
while(n != null){
s++;
n = n.next;
}
return s;
}
public void add(String data){
Node n = new Node(data);
if(isEmpty()){
first = last = n;
}
else{
last.next = n;
last = n;
}
}
//remove from the front of queue, reutrn null if empty
public String remove(){
if(isEmpty())
return null;
else{
String s = first.data;
first = first.next;
if(first == null)
last = null;
return s;
}
}
//returns the first item without removing it, null if empty
public String peek(){
if(isEmpty())
return null;
else
return first.data;
}
public String toString(){
String s = "";
Node n = first;
if(n != null)
{
s += n.data;
n = n.next;
while(n != null){
s += ", " + n.data;
n = n.next;
}
}
return s;
}
}
TestMyQueue.java
-------
public class TestMyQueue {
public static void main(String[] args) {
MyQueue que = new MyQueue();
System.out.println("Queue size = " + que.size() + " Queue contents: " + que + " ");
System.out.println("Adding Johh, Bob, Alice, Robert to queue");
que.add("John");
que.add("Bob");
que.add("Alice");
que.add("Robert");
System.out.println("Queue size = " + que.size() + " Queue contents: " + que + " ");
System.out.println("First to go " + que.peek());
System.out.println("Serving " + que.remove());
System.out.println("Serving " + que.remove());
System.out.println("Queue size = " + que.size() + " Queue contents: " + que + " ");
System.out.println("Michael joins queue");
que.add("Michael");
System.out.println("Jessica joins queue");
que.add("Jessica");
System.out.println("Queue size = " + que.size() + " Queue contents: " + que + " ");
while(!que.isEmpty())
System.out.println("Serving " + que.remove());
System.out.println("Queue size = " + que.size() + " Queue contents: " + que + " ");
}
}
output
-----
Queue size = 0
Queue contents:
Adding Johh, Bob, Alice, Robert to queue
Queue size = 4
Queue contents: John, Bob, Alice, Robert
First to go John
Serving John
Serving Bob
Queue size = 2
Queue contents: Alice, Robert
Michael joins queue
Jessica joins queue
Queue size = 4
Queue contents: Alice, Robert, Michael, Jessica
Serving Alice
Serving Robert
Serving Michael
Serving Jessica
Queue size = 0
Queue contents:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.