CS1020 Week 9: 19th March 2015 Contents Practice Exercise 33 Practice Exercise 34 Take-home Lab #4 2 Week 9 Practice Exercise #33 QuickEat – Queue Implementation 3 Week 9 QuickEat (1/3) Problem: 4 The food is to be served to customers on first-come-firstserve basis The dishes are served only when ready There are a limited amount of dishes Dishes Ready but No Order Throw Away !! Week 9 QuickEat (2/3) Each Dish Has Own Queue Maintain ArrayList Of Dish Queues Dish1 Tag Tag Dish 2 Tag Tag Dish 3 Tag Tag Add More Dishes 5 Week 9 QuickEat (3/3) Solution : 6 For Each Dish – maintain a queue for the Orders Process the Input correctly : Order (#Tag) (#DishOrdered) (#DishId) ….. Ready (#DishId) For every order operation add to queue For every ready operation process the queue and serve to customer Week 9 Understand Question Things to take note: 7 Queue is an interface, not a class cannot be instantiated Maintain an arraylist for queue of all dishes Use LinkedList class which has implementation of methods in Queue interface dishQueues.add(new LinkedList<Integer>()); Queue Operation Use only methods in LinkedList class that are legal for Queue Eg – isEmpty(), offer(), poll() Week 9 Practice Exercise #34 WebBrowser – Stack Implementation 8 Week 9 WebBrowser (1/4) Problem: 9 Maintain the browsing history of User User can move through the list websites viewed by “BACKWRD” and “FORWARD” commands Store every new website visited in the browsing history Print the final browsing history earliest latest page as well as the current page Week 9 WebBrowser (2/4) Back Operation – Pop back stack – modify current Forward Operation – Pop forward stack – modify current Current Page Page Page Page Current Page changes to recently visited URL Page Page Backward Stack 10 Page Forward Stack Week 9 WebBrowser (3/4) Solution : 11 Maintain two separate stacks – Back And Forward Check for corner case – when first page loads Empty Forward Stack for each new page load “BACKWARD” – Pop from back stack move current forward “FORWARD” – Pop from forward stack move current backward Print – Earliest to Latest page history Also print the current page Week 9 WebBrowser (4/4) Things to take note: 12 Make use of Java Stack API Alternatively, solution can be achieved using LinkedList ADT Can try it to have practice on LinkedList Familiarize with Stack API to prepare better for SitIn Lab 3 Week 9 Take-home Lab #4 Exercise 1 – Cargo Optimization 13 Week 9 Cargo Optimization (1/3) Problem: 14 Containers arrives at the terminal in a random order, addressed to different destination ships (A-Z) They need to be stacked at the terminal The destination ships arrives at the terminal in a fixed sequence (A-Z) Week 9 Cargo Optimization (2/3) To: B Random order To: D To: A Terminal To: C 15 Week 9 Cargo Optimization (3/3) Fixed order Terminal 16 Week 9 Required Solution Minimally, how many stacks are needed, such that destination ship has all its cargo is at the top when it arrives? Hint: 17 You should stack container #1 on top of another container #2 only if Container #1’s ship arrive before or at the same time as container #2 You should always choose the stack whose top container’s ship’s arrival time is closest with the new container’s ship’s arrival time Week 9 Take-home Lab #4 Exercise 2 – Queue Simulation 18 Week 9 Queue Simulation Queue Simulation Objective is to find the final queue resulting from VIP customers and Regular Customers in Queue and find the number of customers before the specified customer Solution Always insert a VIP customer before a regular customer in the queue Redirect the links during insertion is a pre-requisite Queue is always first come first serve. This questions requires a modified priority queue. 19 Week 9 head Reg 1 Reg 2 Reg 3 First VIP Customer Arrived: 20 VIP 1 Week 9 Queue Simulation Queue Visualization (VIP customer enters the Queue) (1/4) Reg 1 head Reg 2 Reg 3 VIP 1 21 Week 9 Queue Simulation Queue Visualization (VIP customer enters the Queue) (2/4) Reg 1 head Reg 2 Reg 3 VIP 1 VIP 2 customers arrived: 22 VIP 2 Week 9 Queue Simulation Queue Visualization (VIP customer enters the Queue) (3/4) Reg 1 head VIP 1 23 Reg 2 VIP 2 Week 9 Reg 3 Queue Simulation Queue Visualization (VIP customer enters the Queue) (4/4) Recap Take Home Lab 4 Due: 10pm - 20th March Familiarize with Stack and Queue API Question?? 24 Week 9 END OF FILE 25 Week 9