Sit-in Lab 4 Orders Topic: Stack and Queue. Overview Simulate an order management system that:  Can take multiple orders and processing them according.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
SFTW241 Group A4 Presentation of Grouping Process.
CS1020 Week 9: 19 th March Contents  Practice Exercise 33  Practice Exercise 34  Take-home Lab #4 Week 92.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Copyright © Texas Education Agency, Advanced Computer Programming Data Structures: Collections.
Recitation 1 CS0445 Data Structures Mehmud Abliz.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Stacks and Queues Pepper. Why History Simplicity Operating Systems – Function Call Stack.
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.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
CS2852 Week 3, Class 2 Today Stacks Queues SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
Lab 6 Stack ADT. OVERVIEW The stack is one example of a constrained linear data structure. In a stack, the elements are ordered from most recently added.
Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.
Computer Science 209 Software Development Inheritance and Composition.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
CS2852 Week 5, Class 2 Today Queue Applications Circular Queue Implementation Testing SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:
Sit-In Lab 2 - OOP Restaurant.  Manage a restaurant and perform these types of queries: Assign a favorite table to a specific group Assign the lexicographically-smallest.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
Take-Home Lab #02 CS1020 – DATA STRUCTURES AND ALGORITHMS 1 AY SEMESTER 2 1.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on ArrayLists COMP 102.
COMP 103 Maps and Queues. RECAP  Iterators (for-each loop)  Bag, Sets, and Stacks - a class, not interface TODAY  Maps and Queues 2 RECAP-TODAY QUICK.
Contest Algorithms January 2016 Describe the maxflow problem, explain the Ford-Fulkerson, Edmonds-Karp algorithms. Look at the mincut problem, bipartite.
A 2-D Array is a structure that storage space both vertically and horizontally. Thus, the array has both rows and columns. 2-D Arrays are used to create.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
CS1020 – Data Structures And Algorithms 1 AY Semester 2
4. Java language basics: Function
QueueStack CS1020.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Week 3 - Friday CS221.
Software Development Inheritance and Composition
Stacks and Queues.
User input We’ve seen how to use the standard output buffer
Introduction to Methods in java
TK1114 Computer Programming
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
CMSC 341 Lecture 5 Stacks, Queues
Parallel Programming with MPI and OpenMP
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stack Memory 1 (also called Call Stack)
Java Methods Stacks and Queues A & AB Object-Oriented Programming
Queues.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Popping Items Off a Stack Lesson xx
COMPUTER 2430 Object Oriented Programming and Data Structures I
תגבור פרונטלי 6 ברוכים הבאים מבוא למדעי המחשב 2018
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
ADT list.
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Introduction to Programming
python.reset() Also moving to a more reasonable room (CSE 403)
Heaps and Priority Queues
CE 221 Data Structures and Algorithms
CSC1401 Input and Output (with Files)
Queues Lecture 30 Fri, Apr 2, /3/2019 Queues.
Building Java Programs
Java: Variables, Input and Arrays
CSCS-200 Data Structure and Algorithms
CS 11 – April 1 Review steps for lab #3
Presentation transcript:

Sit-in Lab 4 Orders Topic: Stack and Queue

Overview Simulate an order management system that:  Can take multiple orders and processing them according to their time of arrival  New order must be assigned to the lowest-numbered available processor  If no processor available, the order will wait for the next readily available processor  For each order, print its order number, the number of assigned processor, and the time that the order is processed 2

Input  First line: Two integers K (number of processors) and O (number of orders)  O rows follow: T (time of arrival of the order), X (time needed to process that order) 3

Main Method  Read K (number of processors) and O (number of orders)  Initialize the processors  For each order:  Read order information  Process the order accordingly 4

Main Method public void run() { Scanner sc = new Scanner(System.in); int K = sc.nextInt(); // number of processors int O = sc.nextInt(); // number of orders initProcessors(K); // initialize the processors for (int i = 1; i <= O; i++){ int arrTime = sc.nextInt(); // arrival time of the order int timeNeeded = sc.nextInt(); // time needed to process processOrder(i, arrTime, timeNeeded); } 5

Class Processor 6 Processor -number: int -finishTime: int + getNumber(): int + setFinishTime(): void + getFinishTime(): int Note: Finish time of a processor is the finish time of the last order assigned to it. The default value of finish time is -1 (when the processor is not assigned to any order)

Choosing better processor to assigned // return true if processor x is better to be assigned // than processor y for the specific order arrival time private boolean isBetter(Processor x, Processor y, int orderArrTime){ // If the last order in the processor finish before orderArrTime, // the ready time is orderArrTime. Otherwise, the ready time is // the finish time of the last order int xReady = (x.getFinishTime() < orderArrTime) ? orderArrTime : x.getFinishTime(); int yReady = (y.getFinishTime() < orderArrTime) ? orderArrTime : y.getFinishTime(); // return true only when processor x has earlier ready time // or has the same ready time with y but smaller number return (xReady < yReady) || (xReady == yReady && x.getNumber() < y.getNumber()); } 7

Processing orders  For each order  Loop through the processors list to find the best processor to be assigned  Update the assigned processor in the list  Print the order number, the number of assigned processor, and the time that the order is processed 8

Processors list  What data structure can we use to store the list?  Array, ArrayList, LinkedList? (ok but not recommended)  Stack?  Queue? 9

Process order – Using stack private Stack processors; private void initProcessors(int K){ processors = new Stack (); for (int i = 1; i <= K; i++){ // push new processor with specific number // and default finish time value processors.push(new Processor(i, -1)); } 10

Process order – Using stack private void processOrder(int orderNum, int arrTime, int timeNeeded){ Stack temp = new Stack (); // find the best processor to be assigned //... // compute the finish time of the order with the assigned // processor int readyTime = (best.getFinishTime() < arrTime) ? arrTime : best.getFinishTime(); int finishTime = readyTime + timeNeeded; // Push elements back to the original stack and update // the assigned processor //... // print the result System.out.println(orderNum + " " + best.getNumber() + " “ + finishTime); } 11

Process order – Using stack processors P1 (-1)P2 (-1) temp 12

Order 1: arrTime = 0, timeNeeded = 9 processors P1 (-1)P2 (-1) temp 13 Best = … Loop through all elements in the list to find the best processor to be assigned to the order

Order 1: arrTime = 0, timeNeeded = 9 processors P1 (-1) temp P2(-1) 14 Best = P2

Order 1: arrTime = 0, timeNeeded = 9 processors temp P2(-1)P1(-1) 15 Best = P1 readyTime = 0, finishTime = = 9 P1.setFinishTime(9)

Order 1: arrTime = 0, timeNeeded = 9 processors P1(9) temp P2(-1) 16 Best = P1 Push elements back to the original stack and update the assigned processor

Order 1: arrTime = 0, timeNeeded = 9 processors P1(9)P2(-1) temp 17 Best = P1 Print “1 1 9”

Order 2: arrTime = 5, timeNeeded = 5 processors P1(9)P2(-1) temp 18 Best = … Loop through all elements in the list to find the best processor to be assigned to the order

Order 2: arrTime = 5, timeNeeded = 5 processors P1(9) temp P2(-1) 19 Best = P2

Order 2: arrTime = 5, timeNeeded = 5 processors temp P2(-1)P1(9) 20 Best = P2 readyTime = 5, finishTime = 5 +5 = 10 P2.setFinishTime(10)

Order 2: arrTime = 5, timeNeeded = 5 processors P1(9) temp P2(-1) 21 Best = P2 Push elements back to the original stack and update the assigned processor

Order 2: arrTime = 5, timeNeeded = 5 processors P1(9)P2(10) temp 22 Best = P2 Print “2 2 10”

Order 3: arrTime = 10, timeNeeded = 1 processors P1(9)P2(10) temp 23 Best = … Loop through all elements in the list to find the best processor to be assigned to the order

Order 3: arrTime = 10, timeNeeded = 1 processors P1(9) temp P2(10) 24 Best = P1

Order 3: arrTime = 10, timeNeeded = 1 processors temp P2(10)P1(9) 25 Best = P1 readyTime = 10, finishTime = = 11 P1.setFinishTime(11)

Order 3: arrTime = 10, timeNeeded = 1 processors P1(11) temp P2(10) 26 Best = P1 Push elements back to the original stack and update the assigned processor

Order 3: arrTime = 10, timeNeeded = 1 processors P1(11)P2(10) temp 27 Best = P1 Print “3 1 11”

Process order – Using Queue  Similar to Stack  Using offer() and poll() instead of push() and pop() 28

END OF FILE 29