Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I must use the heap class that is provided. The simulation process involves handling events, where the number of tellers needed for efficient service is
I must use the heap class that is provided. The simulation process involves handling events, where the number of tellers needed for efficient service is determined by inputting the initial number of tellers before the start of the simulation. The aim is to determine the average wait time for customers in line and this information will be used by bank officers to ensure smooth customer service. Use a Max-Heap as the underlying data structure for the customer queue. Heap.java is provided for you on Canvas. Define a class Customer which has the fields processTime and priority. In order to compare Customer objects, you need to override the compareTo method in the Customer class. Build a class Simulation, which coordinates each arrival and each departure. The timer should be used to automatically advance your simulation. SIMULATION CLASS package lab4; import javax.swing.Timer; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.*; public class BankSimulation { static int totalWaitTime = 0; static int totalCustomers = 0; static int tellerCount = 0; static HeapcustomerList = new Heap(); static int[] tellerAvailability; public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Welcome to the Bank Simulation!"); System.out.print("Please enter the number of Tellers: "); //gather user input tellerCount = scan.nextInt(); //append tellers to array tellerAvailability = new int[tellerCount]; //initiate simulation Timer timer = new Timer(100, new TimerListener()); timer.start(); while(true){ } } private static class TimerListener implements ActionListener { int counter = 0; @Override public void actionPerformed(ActionEvent e) { System.out.println("Tick "+counter); for (int i = 0; i 0){ tellerAvailability[i]--; if (tellerAvailability[i] == 0) { Customer leave = customerList.deleteMaximum(); if (leave != null) { totalWaitTime += counter - leave.priority; totalCustomers++; System.out.println("Customer Leaves, customer waited for " + (counter - leave.priority) + " units of time."); } } } } if (counter %5 == 0){ int randomPriority = (int)(Math.random() * 10) + 1; int randomProcessTime = (int)(Math.random() * 10)+ 1; customerList.insert(new Customer(randomProcessTime, randomPriority)); System.out.println("New customer has arrived with the priority " + randomPriority + " and transaction time: " + randomProcessTime); } for (int i = 0; i HEAP CLASS
package lab4; import java.util.Iterator; public class Heapimplements Iterable { private Key[] heap; private int size; public Heap(int initCapacity) { heap = (Key[]) new Object[initCapacity + 1]; size = 0; } public Heap() { this(1); } public boolean isEmpty() { return size == 0; } public Key max() { if (isEmpty()) { return null; } return heap[1]; } private void swapKeys(int i, int j) { Key exchange = heap[i]; heap[i] = heap[j]; heap[j] = exchange; } private void resize(int capacity) { assert capacity > size; Key[] temp = (Key[]) new Object[capacity]; for (int i = 1; i = heap.length - 1) resize(2 * heap.length); heap[++size] = x; swimUp(size); } public Key deleteMaximum() { if (isEmpty()) { return null; } Key max = heap[1]; swapKeys(1, size--); sinkDown(1); heap[size+1] = null; if ((size > 0) && (size == (heap.length - 1) / 4)) resize(heap.length / 2); return max; } private void swimUp(int i) { while (i > 1 && isLess(i/2, i)) { swapKeys(i, i/2); i = i/2; } } public int size() { return size; } private void sinkDown(int i) { while (2*i ) heap[i]).compareTo(heap[j]) iterator() { return new HeapIterator(); } private class HeapIterator implements Iterator { private Heap dup; public HeapIterator() { dup = new Heap (size()); for (int i = 1; i Welcome to the Bank Sinulation! Welcome to the Bank simulation! Please enter a number of Tellers: Please enter a number of Tellers: 2 6 Tick Tick Tick 1 New customer has arrived with the priority 3 kkkkkkkkkkk and transaction time: 9 Tick 2 Tick 1 Tick 3 Tick 2 New customer has arrived with the priority 8 and transaction time: 8 Tick 4 Tick 3 New customer has arrived with the priority 4 and transaction time: 13 Tick 4 Tick 5 New customer has arrived with the priority 2 and transaction time: 5 Tick 5 Tick 6 New customer has arrived with the priority 6 Tick 7 Tick 6 kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk New customer has arrived with the priority 10 and transaction time: 16 1 customers waiting right now! Tick 442 Tilck 8 Tick 443 kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk Customer Leaves, customer waited for 47 units of time. Tick 1283 Tick 444 Tick 1284 Customer Leaves, customer waited for 4 units of time. *ick 1285 Customer's serviced: 200...Average waiting time: 27.44 Tick 445 Tick 1286 ************************************ Customer Leaves, customer waited for 881 units of time. Customer's serviced: 200.. Average waiting time: 834.15 Tick 1287
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started