ITI 1221. Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa.

Slides:



Advertisements
Similar presentations
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
Advertisements

COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
CS 240Chapter 7 - QueuesPage 29 Chapter 7 Queues The queue abstract data type is essentially a list using the FIFO (first-in-first-out) policy for adding.
Topic 9 The Queue ADT.
1 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Lab 1: 1. Download all my programs in your computer under the same folder. 2. The tree shown in the following figure represents an expression: (((( 3 +
Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine queue processing Define a queue abstract.
Queue Overview Queue ADT Basic operations of queue
Chapter 7: Queues QUEUE IMPLEMENTATIONS QUEUE APPLICATIONS CS
Chapter 14 Queues. Chapter Scope Queue processing Using queues to solve problems Various queue implementations Comparing queue implementations Java Foundations,
1 CSC 222: Computer Programming II Spring 2004 Queues and simulation  queue ADT  enqueue (push), dequeue (pop), front, empty, size  library  application:
Queues21 Queues II: Applications. queues22 Palindrome recognition Palindrome: collection of characters that reads the same backwards and forwards Examples:
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Queues, Deques, and Priority Queues Chapter Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.
TCSS 342, Winter 2005 Lecture Notes
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
Queues, Deques, and Priority Queues Chapter Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Queues What is a Queue? Queue Implementations: Queue As Array
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Queues CSI 1101 N. El Kadri. 2 Definitions A queue is a linear abstract data type such that insertions are made at one end, called the rear, and removals.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
CS 2430 Day 26. Announcements Exam #2: Wednesday, April 3 –Review in lab on Tuesday, April 2 –Sample problems sent via .
1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other.
ITI Introduction to Computing II Lab-6 Dewan Tanvir Ahmed University of Ottawa.
Computer Science 112 Fundamentals of Programming II Modeling and Simulation.
COS 312 DAY 20 Tony Gauvin. Ch 1 -2 Agenda Questions? Capstone Progress reports over due Assignment 6 Posted – Due April 16 – Questions? Queues.
CSC 205 Programming II Lecture 22 Carwash Simulation.
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
A queue is a linear, homogeneous, container that stores and dispenses its content in a FIFO manner. FIFO - Fast In First Out The first (most distant) item.
Chapter Objectives  Learn how to represent a waiting line (queue)  Become proficient using the methods in the Queue  Understand how to implement the.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
1 Queues. 2 Queue Which of the following cases use similar structures? Cars lined up at a tollgate Cars on a 4-lane highway Customers at supermarket check-out.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 5: Queues Java Software Structures: Designing and Using Data.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 13: Queues Announcements.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
CS Data Structures I Chapter 7 Queue I. 2 Topics Introduction Queue Application Implementation Linked List Array ADT List.
CS 2430 Day 24. Announcements Quiz this Friday Program 5 posted on Monday Program 4 due date: Friday at 10pm Program 4 grace date: Wednesday at 10pm (don’t.
CS 367 Introduction to Data Structures Lecture 5.
CE 221 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Chapter 7 Queues Introduction Queue applications Implementations.
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
ITI Introduction to Computing II Lab-5 Dewan Tanvir Ahmed University of Ottawa.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
Queues By Jimmy M. Lu. Overview Definition Standard Java Queue Operations Implementation Queue at Work References.
UNIT II Queue.
Queues Implementations 6/17/2018.
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Queues.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
ADT list.
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Queues What is a Queue? Queue Implementations: As Array
ADT Queue (Array Implementation)
ITI Introduction to Computing II Lab-12
Queues Definition of a Queue Examples of Queues
CSCS-200 Data Structure and Algorithms
Presentation transcript:

ITI Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa

Today’s Objective  Case Study oSimulating a Supermarket Check out

Supermarket Checkout – General Idea (cont..) MarketModel CustomerCashier Queue CreatesGenerates Uses Contains Serves For simplicity, Let’s consider only one cashier, During Program we’ll handle it.

Supermarket Checkout – General Idea InputRange of Values Total minute (length of Simulation)0 < total < 1000 Average Time Spent per Customer0 < average <= total Probability of a new customer arrival in the next minute0 < probability <= 1 Output Total Customers processes Customers left in line Average waiting time

Supermarket Checkout  Two types of Cashiers  Express ( if number of items are less 13 )  Regular  Each cashier has an queue for the customers  A new customer joins to a queue having minimum number of customers for both cases (express and regular cashiers).

Customer  Each customer has oArrival time oInitial number of items oRemaining number of items for serve  Constructor to initialize the instance variables o public Customer(int arrivalTime, int numberOfItems)  Methods oAccess methods  arrival time and  number of remaining items oCurrent status of the Served (on going) customer  int numberOfServedItems()  number of items served so far oUpdate remaining items of a customer  void serve()

Customer – (cont..) public class Customer { private int arrivalTime; private int numberOfItems; private int initialnumberOfItems; // constructor public Customer(int arrivalTime, int numberOfItems) { this.arrivalTime = arrivalTime; this.numberOfItems = numberOfItems; this.initialnumberOfItems = numberOfItems; } public int numberOfServedItems() { return this.initialnumberOfItems - this.numberOfItems; } public void serve() {numberOfItems--; } // access methods public int arrivalTime() { return arrivalTime; } public int numberOfItems() { return numberOfItems; } }

Queue public interface Queue { public void enqueue(Object obj); public Object dequeue(); public boolean isEmpty(); public int size(); } public interface Queue { public void enqueue(Object obj); public Object dequeue(); public boolean isEmpty(); public int size(); }

Queue - ArrayQueue public class ArrayQueue implements Queue { private static final int MAX_QUEUE_SIZE = 10000; private Object[] q; private int front, rear, size; public ArrayQueue() { q = new Object[MAX_QUEUE_SIZE]; front = 0; rear = -1; size = 0; } public int size() { return size; } public boolean isEmpty() { return size == 0; } public boolean isFull() { return size == MAX_QUEUE_SIZE; } public class ArrayQueue implements Queue { private static final int MAX_QUEUE_SIZE = 10000; private Object[] q; private int front, rear, size; public ArrayQueue() { q = new Object[MAX_QUEUE_SIZE]; front = 0; rear = -1; size = 0; } public int size() { return size; } public boolean isEmpty() { return size == 0; } public boolean isFull() { return size == MAX_QUEUE_SIZE; }

Queue – ArrayQueue (cont..) public void enqueue(Object o) { // pre-condition: ??? if (rear == (MAX_QUEUE_SIZE -1)) { int j=0; for (int i=front; i<=rear; i++) { q[j++] = q[i]; } front = 0; rear = size - 1; } q[++rear] = o; size++; } public Object dequeue() { // pre-condition: ??? Object savedValue = q[front]; q[front] = null; front++; size--; return savedValue; } } // end of the class public void enqueue(Object o) { // pre-condition: ??? if (rear == (MAX_QUEUE_SIZE -1)) { int j=0; for (int i=front; i<=rear; i++) { q[j++] = q[i]; } front = 0; rear = size - 1; } q[++rear] = o; size++; } public Object dequeue() { // pre-condition: ??? Object savedValue = q[front]; q[front] = null; front++; size--; return savedValue; } } // end of the class

Cashier  Each cashier has oTotal waiting time of all customers oTotal number of served customers oTotal number of served items oCurrent customer oA queue to store waiting customers  Constructor oInitialize - instance variables oYou need a Queue Data Structure  Methods & Access methods oGet total waiting time oGet total served items oGet total served customers oAdd a new customer to the cashier queue oGet Number of customers in the queue oServe one customer at a time

Cashier – (cont..) public class Cashier { private int totalCustomerWaitTime, customersServed, totalItemsServed; private Customer currentCustomer; private Queue queue; // constructor public Cashier(){ totalCustomerWaitTime = 0; customersServed = 0; currentCustomer = null; totalItemsServed = 0; queue = new ArrayQueue(); } // access methods public int getTotalCustomerWaitTime() { return this.totalCustomerWaitTime; } public int getTotalItemsServed() { if(this.currentCustomer != null) return totalItemsServed + this.currentCustomer.numberOfServedItems(); else return totalItemsServed; } public int getTotalCustomersServed() { return customersServed; } public void addCustomer(Customer c){ queue.enqueue(c); } public int lengthOfQueue() { return queue.size(); } public class Cashier { private int totalCustomerWaitTime, customersServed, totalItemsServed; private Customer currentCustomer; private Queue queue; // constructor public Cashier(){ totalCustomerWaitTime = 0; customersServed = 0; currentCustomer = null; totalItemsServed = 0; queue = new ArrayQueue(); } // access methods public int getTotalCustomerWaitTime() { return this.totalCustomerWaitTime; } public int getTotalItemsServed() { if(this.currentCustomer != null) return totalItemsServed + this.currentCustomer.numberOfServedItems(); else return totalItemsServed; } public int getTotalCustomersServed() { return customersServed; } public void addCustomer(Customer c){ queue.enqueue(c); } public int lengthOfQueue() { return queue.size(); }

Cashier – (cont..) public void serveCustomers(int currentTime){ if (currentCustomer == null){ // no customers yet!!! if (queue.isEmpty()) return; else{ // dequeue first waiting customer and tally results currentCustomer = (Customer) queue.dequeue(); totalCustomerWaitTime = totalCustomerWaitTime + currentTime - currentCustomer.arrivalTime(); customersServed++; } // give a unit of service currentCustomer.serve(); // if current customer is finished, send it away if (currentCustomer.numberOfItems() == 0) { totalItemsServed += currentCustomer.numberOfServedItems(); currentCustomer = null; } } // end of the class public void serveCustomers(int currentTime){ if (currentCustomer == null){ // no customers yet!!! if (queue.isEmpty()) return; else{ // dequeue first waiting customer and tally results currentCustomer = (Customer) queue.dequeue(); totalCustomerWaitTime = totalCustomerWaitTime + currentTime - currentCustomer.arrivalTime(); customersServed++; } // give a unit of service currentCustomer.serve(); // if current customer is finished, send it away if (currentCustomer.numberOfItems() == 0) { totalItemsServed += currentCustomer.numberOfServedItems(); currentCustomer = null; } } // end of the class

Cashiers  Cashiers has oAn array of cashier  Constructor oInitialized the size of the cashiers array  Methods oAdding a new customer  Where size of the queue is minimum oServing customers at a particular instant of time otoString()  Average number of customers currently waiting  Average number of items per customer  Average waiting time per customer  Average number of customers served per cashier  Average number of items Handled per cashier  Total number of clients served  Total number of items handled

Cashiers – (cont..) import java.text.NumberFormat; public class Cashiers { private Cashier[] cashiers; private static final String nl = System.getProperty("line.separator"); public Cashiers(int n) { if (n < 1) throw new IllegalArgumentException(); cashiers = new Cashier[n]; for (int i=0; i<n; i++) cashiers[i] = new Cashier(); } public void addCustomer(Customer c) { int iMax, vMax; iMax = 0; for (int i=1; i<cashiers.length; i++) if (cashiers[i].lengthOfQueue() < cashiers[iMax].lengthOfQueue()) iMax = i; cashiers[iMax].addCustomer(c); } import java.text.NumberFormat; public class Cashiers { private Cashier[] cashiers; private static final String nl = System.getProperty("line.separator"); public Cashiers(int n) { if (n < 1) throw new IllegalArgumentException(); cashiers = new Cashier[n]; for (int i=0; i<n; i++) cashiers[i] = new Cashier(); } public void addCustomer(Customer c) { int iMax, vMax; iMax = 0; for (int i=1; i<cashiers.length; i++) if (cashiers[i].lengthOfQueue() < cashiers[iMax].lengthOfQueue()) iMax = i; cashiers[iMax].addCustomer(c); }

Cashiers – (cont..) public void serveCustomers(int currentTime) { for (int i=0; i<cashiers.length; i++) cashiers[i].serveCustomers(currentTime); } public String toString() { int nbCustomers = 0; int nbItems = 0; int waitingTime = 0; int nbServedCustomers = 0; String out = "Tally: " + nl; for (int i=0; i<cashiers.length; i++) { nbCustomers += cashiers[i].lengthOfQueue(); nbItems += cashiers[i].getTotalItemsServed(); waitingTime += cashiers[i].getTotalCustomerWaitTime(); nbServedCustomers += cashiers[i].getTotalCustomersServed(); } double aveLength = (double) nbCustomers / (double) cashiers.length; double aveItemsCustomer = (double) nbItems/ (double) nbServedCustomers; double aveItemsCashier = (double) nbItems/ (double) cashiers.length; double aveWaitingTimeCus = (double) waitingTime / (double) nbServedCustomers; double aveServedCustomersCashier = (double) nbServedCustomers/ (double) cashiers.length; public void serveCustomers(int currentTime) { for (int i=0; i<cashiers.length; i++) cashiers[i].serveCustomers(currentTime); } public String toString() { int nbCustomers = 0; int nbItems = 0; int waitingTime = 0; int nbServedCustomers = 0; String out = "Tally: " + nl; for (int i=0; i<cashiers.length; i++) { nbCustomers += cashiers[i].lengthOfQueue(); nbItems += cashiers[i].getTotalItemsServed(); waitingTime += cashiers[i].getTotalCustomerWaitTime(); nbServedCustomers += cashiers[i].getTotalCustomersServed(); } double aveLength = (double) nbCustomers / (double) cashiers.length; double aveItemsCustomer = (double) nbItems/ (double) nbServedCustomers; double aveItemsCashier = (double) nbItems/ (double) cashiers.length; double aveWaitingTimeCus = (double) waitingTime / (double) nbServedCustomers; double aveServedCustomersCashier = (double) nbServedCustomers/ (double) cashiers.length;

Cashiers – (cont..) NumberFormat nf = NumberFormat.getNumberInstance(); out += "Average number of customers currently waiting: " + nf.format(aveLength) +nl; out += "Average number of items per customer: " + nf.format(aveItemsCustomer)+nl; out += "Average waiting time per customer: " + nf.format(aveWaitingTimeCus)+nl; out += "Average number of customers served per cashier: " + nf.format(aveServedCustomersCashier)+nl; out += "Average number of items Handled per cashier: " + nf.format(aveItemsCashier)+nl; out += "Total number of clients served: " + nf.format(nbServedCustomers)+nl; out += "Total number of items handled: " + nf.format(nbItems)+nl; return out; } NumberFormat nf = NumberFormat.getNumberInstance(); out += "Average number of customers currently waiting: " + nf.format(aveLength) +nl; out += "Average number of items per customer: " + nf.format(aveItemsCustomer)+nl; out += "Average waiting time per customer: " + nf.format(aveWaitingTimeCus)+nl; out += "Average number of customers served per cashier: " + nf.format(aveServedCustomersCashier)+nl; out += "Average number of items Handled per cashier: " + nf.format(aveItemsCashier)+nl; out += "Total number of clients served: " + nf.format(nbServedCustomers)+nl; out += "Total number of items handled: " + nf.format(nbItems)+nl; return out; }

MarketModel public class MarketModel { private static final int SECONDS_PER_MINUTE = 60; private static final int MINUTES_PER_HOUR = 60; private static final int TICK = 5; private static final String nl = System.getProperty("line.separator"); private static final double probabilityOfNewArrival = 0.5; private Cashiers express; private Cashiers regular; private int lengthOfSimulation, aveNbItems; public MarketModel(int duration) { this.lengthOfSimulation = duration; express = new Cashiers(3); regular = new Cashiers(10); }

MarketModel – (cont..) public void runSimulation() { int currentTime = 0; while (currentTime < lengthOfSimulation) { if (Math.random() <= probabilityOfNewArrival) { Customer customer = new Customer(currentTime, (int) (50 * Math.random()) + 1); if (customer.numberOfItems() <= 12) express.addCustomer(customer); else regular.addCustomer(customer); } express.serveCustomers(currentTime); regular.serveCustomers(currentTime); if ((currentTime % (5 * SECONDS_PER_MINUTE)) == 0) System.out.println(this); currentTime += TICK; }

MarketModel – (cont..) public String toString() { String out = "MarketModel" + nl; out += "express lanes: " + express + nl; out += "regular lanes: " + regular + nl; return out; } public static void main(String[] args) throws java.io.IOException { MarketModel mm = new MarketModel(SECONDS_PER_MINUTE * MINUTES_PER_HOUR); mm.runSimulation(); }

The End