Queues Objectives Design and write a method to insert items into a queue Design
ID: 3802796 • Letter: Q
Question
Queues
Objectives
Design and write a method to insert items into a queue
Design and write a method to remove items from a queue
Design and write a method to look at the first and last item on the queue
Design and write a method to determine if the queue contains any elements
Design and implement a queue using a linked list
Use a queue in a Java program
Assignment Overview
This programming exercise introduces the Queue data structure. The students must create all the necessary methods for the queue and use the queue in a Java program.
Deliverables
A listing of the fully commented, working source code of the Java program
Test data for the code
A screen shot of the application in execution
Name it Assignment_5_1 .
Write the Java source code necessary to build a solution for the problem below:
You have been asked to create a customer service program for a new phone store that will be opening soon. Since this company anticipates being the agent for a rising new product that will be able to host the most popular video games always released at midnight on the eve of the anticipated announcement date they want a customer service module that will help them manage the customers as they arrive. Customers will be added to the queue as they arrive, removed once they have been served. Sometimes the queue will be search to determine who is there. As a prototype for this system, you are to use the linked list created in Assignment 3.1 to create a queue class and a test program that can handle this new system. The queue class must include a method to insert elements into the queue, remove elements from the queue, look at the first and last element of the queue without removing the elements from the queue, and search for an element in the queue. Use the following algorithm to simulate a simple version of the new system.
Choose a random integer between 1 and 5 to determine the minutes at which a customer arrives.
When a customer arrives, choose a random integer between 1 and 5 to determine the number of minutes that the customer must remain in the checkout line.
Repeat the two steps for a 12 hour (720 minute) simulation.
Run the simulation again with a random integer of 1 and 3 to compare the number of customers in the checkout line against the original 12 hour simulation.
Queues
Objectives
Design and write a method to insert items into a queue
Design and write a method to remove items from a queue
Design and write a method to look at the first and last item on the queue
Design and write a method to determine if the queue contains any elements
Design and implement a queue using a linked list
Use a queue in a Java program
Assignment Overview
This programming exercise introduces the Queue data structure. The students must create all the necessary methods for the queue and use the queue in a Java program.
Deliverables
A listing of the fully commented, working source code of the Java program
Test data for the code
A screen shot of the application in execution
Step 1 Create a new project.
Name it Assignment_5_1 .
Step 2 Build a solution.
Write the Java source code necessary to build a solution for the problem below:
You have been asked to create a customer service program for a new phone store that will be opening soon. Since this company anticipates being the agent for a rising new product that will be able to host the most popular video games always released at midnight on the eve of the anticipated announcement date they want a customer service module that will help them manage the customers as they arrive. Customers will be added to the queue as they arrive, removed once they have been served. Sometimes the queue will be search to determine who is there. As a prototype for this system, you are to use the linked list created in Assignment 3.1 to create a queue class and a test program that can handle this new system. The queue class must include a method to insert elements into the queue, remove elements from the queue, look at the first and last element of the queue without removing the elements from the queue, and search for an element in the queue. Use the following algorithm to simulate a simple version of the new system.
Choose a random integer between 1 and 5 to determine the minutes at which a customer arrives.
When a customer arrives, choose a random integer between 1 and 5 to determine the number of minutes that the customer must remain in the checkout line.
Repeat the two steps for a 12 hour (720 minute) simulation.
Run the simulation again with a random integer of 1 and 3 to compare the number of customers in the checkout line against the original 12 hour simulation.
Explanation / Answer
import java.util.Objects;
import java.util.Random;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Sam
*/
public class NewOrgnanization {
class Node {
String data;
long wait;
Node next;
public Node(String data, long wait) {
this.data = data;
this.wait = wait;
next = null;
}
@Override
public int hashCode() {
int hash = 5;
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Node other = (Node) obj;
if (!Objects.equals(this.data, other.data)) {
return false;
}
return Objects.equals(this.next, other.next);
}
}
class Queue {
Node first;
Node last;
int count;
public Queue() {
first = null;
last = null;
count = 0;
}
public void insert(String data, long wait) {
if (first == null)
first = last = new Node(data, wait);
else
last = last.next = new Node(data, wait);
count ++;
}
public String delete () {
String string;
if (first == null)
return null;
else if (first.equals(last)){
string = last.data;
first = last = last.next;
}
else {
string = first.data;
first = first.next;
}
count++;
return string;
}
public boolean isEmpty(){
return count == 0;
}
public Node getFirst() {
return first;
}
public Node getLast() {
return last;
}
public boolean search(String sField){
Node temp = first;
while (temp != null) {
if(temp.data.equals(sField))
return true;
temp = temp.next;
}
return false;
}
}
private String randomName(){
char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < 7; i++) {
char c = chars[random.nextInt(chars.length)];
sb.append(c);
}
return sb.toString();
}
private final Queue q = new Queue();
public void execute() throws InterruptedException {
long startTime = System.nanoTime();
long insertTime = (long) (Math.round(Math.random()*5)*6e+10 + startTime);
long serviceTime = 0;
int count = 0;
while (System.nanoTime() - startTime < 3.6e+12){
if (insertTime < System.nanoTime()){
q.insert(randomName(), (long) (Math.round(Math.random()*5)*6e+10+insertTime));
insertTime += Math.round(Math.random()*5);
count ++;
if (count == 1)
serviceTime = q.getFirst().wait;
}
if (serviceTime > 0 && serviceTime < System.nanoTime()){
q.delete();
count--;
if (q.getFirst()!= null)
serviceTime = q.getFirst().wait;
else
serviceTime = 0;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.