Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.Scanner; /** * @author Your Name * @version CSIS 3103 * Customers class is to simulate various queue options, such as adding new customers

import java.util.Scanner; /** * @author Your Name * @version CSIS 3103 * Customers class is to simulate various queue options, such as adding new customers at the rear of the queue, * removing customer from the front, and displaying first customers */ public class CustomersQueue { public static void main(String[] args) { // initiate an ArrayQueue object called "customers" to represent a waiting queue // create a scanner // create a variable "command" to store user's selected option (returned from the method: menu) // In the while loop as long as the user's option is not 'X'- exit, then continue the iteration // you may consider using case-switch statement or multiple-if statement // If user selected option is 'N', then call offer method in ArrayQueue // If user selected option is 'D', then call peek method in ArrayQueue // If user selected option is 'R', then call poll method in ArrayQueue // If user selected option is 'L', then return queue size //** If user selected option is 'W', then (1)ask user to enter the customer's name (2) call "waitlength" method // And then display the number of people ahead of user entered customer, or show error message if the customer // is not found. // close the scanner } /** * waitLength method * @param customers * @param who * @return the length of the queue (the number of customers in the queue) */ private static int waitLength(ArrayQueue customers, String who) { int count = 0; for (String e : customers) { if (e.equals(who)) return count; count++; } return -1; } /** * getCustomer method asked user to enter customer's name * @param scan * @return user entered customer's name as string */ private static String getCustomer(Scanner scan) { System.out.println("Enter customer name:"); return scan.nextLine().trim(); } /** * A method called menu: show six options to users: * N - New Customer * D - Display First Customer in Line * R - Remove First Customer in Line * L - Display Line Length * W - Display wait length for specific customer * X - Exit * * @param scan * @return user's option: data type is char */ private static char menu(Scanner scan) { //display the six options to the user // trim the user input option String next = scan.nextLine().trim(); // create a variable (char type) to store user's selection // If user entered a valid selection then return the command, otherwise // display an error message indicating it is invalid selection and display the menu again return 'X'; // placeholder. return command; } } 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.util.Collection; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Queue; /** * Queue implemented using a circular array. * * @author Helen Wei * @version CSIS 3103 */ public class ArrayQueue implements Queue { private E[] data; private int front; private int rear; private int capacity; private int size; private static final int DEFAULT_INITIAL_CAPACITY = 10; /** * Constructs a Queue with initial capacity of 10. */ public ArrayQueue() { this(DEFAULT_INITIAL_CAPACITY); } /** * Constructs a queue with a specified initial capacity. * @param initCapacity The initial capacity. */ public ArrayQueue(int initCapacity) { data = (E[]) new Object[initCapacity]; capacity = initCapacity; size = 0; front = 0; rear = capacity - 1; } @Override public boolean offer(E e) { if (e==null) throw new NullPointerException("Cannot add a null reference to the queue"); if (size == capacity) { reallocate(); } size++; rear = (rear + 1) % capacity; data[rear] = e; return true; } private void reallocate() { int newCapacity = 2 * capacity; E[] temp = (E[]) new Object[newCapacity]; int j = front; for (int i = 0; i < size; i++) { temp[i] = data[j]; j = (j+1) % capacity; } front = 0; rear = size-1; capacity = newCapacity; data = temp; } @Override public boolean add(E e) { return offer(e); } @Override public E peek() { if (size==0) return null; return data[front]; } @Override public E element() { if (size==0) throw new NoSuchElementException("Queue is empty"); return data[front]; } @Override public E poll() { if (size==0) return null; E e = data[front]; data[front] = null; front = (front + 1) % capacity; size--; return e; } @Override public E remove() { if (size==0) throw new NoSuchElementException("Queue is empty"); E e = data[front]; data[front] = null; front = (front + 1) % capacity; size--; return e; } @Override public boolean addAll(Collection c) { throw new UnsupportedOperationException(); } @Override public void clear() { throw new UnsupportedOperationException(); } @Override public boolean contains(Object o) { if (o==null) throw new NullPointerException("This queue does not support null elements."); int i = front; for (int j = 0; j < size; j++) { if (data[i].equals(o)) return true; i = (i + 1) % capacity; } return false; } @Override public boolean containsAll(Collection c) { for (Object e : c) { if (!contains(e)) return false; } return true; } @Override public boolean isEmpty() { return size==0; } @Override public Iterator iterator() { return new QueueIterator(); } @Override public boolean remove(Object o) { throw new UnsupportedOperationException(); } @Override public boolean removeAll(Collection c) { throw new UnsupportedOperationException(); } @Override public boolean retainAll(Collection c) { throw new UnsupportedOperationException(); } @Override public int size() { return size; } @Override public Object[] toArray() { Object[] array = new Object[size]; int j = front; for (int i = 0; i < size; i++) { array[i] = data[j]; j = (j + 1) % capacity; } return array; } @Override public  T[] toArray(T[] a) { if (a.length < size) a = (T[]) new Object[size]; int j = front; for (int i = 0; i < size; i++) { try { a[i] = (T) data[j]; } catch (ClassCastException e) { throw new ArrayStoreException(); } j = (j + 1) % capacity; } return a; } private class QueueIterator implements Iterator { private int nextCount; private int nextIndex; public QueueIterator() { nextIndex = front; } @Override public boolean hasNext() { return nextCount < size; } @Override public E next() { E e = data[nextIndex]; nextIndex = (nextIndex + 1) % capacity; nextCount++; return e; } @Override public void remove() { // TODO Auto-generated method stub } } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 14th East European Conference Adbis 2010 Novi Sad Serbia September 2010 Proceedings Lncs 6295

Authors: Barbara Catania ,Mirjana Ivanovic ,Bernhard Thalheim

2010th Edition

3642155758, 978-3642155758

More Books

Students also viewed these Databases questions