1 / 48

CS1020

CS1020. Week 10: 27 th March 2014. Contents. Part 1: Discussion on Sit-in Lab #3 Part 2: Discussion on Take-home Lab #4 Part 3: Discussion on Take-home Lab # 5. Part 1. Discussion on Sit-in Lab #3. Set A: Messaging Application. Set A : Messaging Application.

rosina
Download Presentation

CS1020

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1020 Week 10: 27thMarch 2014

  2. Contents • Part 1: Discussion on Sit-in Lab #3 • Part 2: Discussion on Take-home Lab #4 • Part 3: Discussion on Take-home Lab #5 Week 10

  3. Part 1 Discussion on Sit-in Lab #3 Week 10

  4. Set A: Messaging Application Set A: Messaging Application • Simulate a messaging application • Consists of N users • Messages sent out and received are stored in message list • Can only store up to 10 messages • To store new message when message list is full, need to remove earliest message in the list. • Two operations • Send • Reply Week 10

  5. Requirements Set A: Messaging Application • Usage of Scanner class • Usage of LinkedList class • Ability to design classes to solve the problem • Ability to come up with simple algorithm quickly to perform send and reply Week 10

  6. Designing the classes Set A: Messaging Application • Requires 2 classes • User class • Encapsulates the user name and the message list that stores the messages • Encapsulates the methods to access and modify the message list • MessageApp class • Encapsulate the list of users (has-a relationship with User class). • Encapsulates the methods to send and reply messages. Week 10

  7. Member Variables Set A: Messaging Application User MessageApp • name: String • messageList: LinkedList of Integer • userList: LinkedList of User + User(aName: String) • … // basic accessors + MessageApp(Scanner sc) … Week 10

  8. Message app simulates a sort of queue Set A: Messaging Application • Latest messages added to the tail of the LinkedList • If require to delete a message before adding a new message, the message at the head is deleted. • This is FIFO principle • LinkedList methods that simulate a queue (read up on these) • peek() • poll() • add(E e) addLast(E e) or offer(E e) /* all add to tail/end of the list */ Week 10

  9. Algorithm for send operation Set A: Messaging Application • If message list of receiver is not full (< 10 messages) • Enqueue message to receiver’s message list • Else if message list of receiver is full • Dequeue first message in receiver’s message list • Enqueuemessage to receiver’s message list • Do the same thing for sender (message sent is stored in both receiver and sender’s message list) Week 10

  10. Methods for implementing send operation Set A: Messaging Application User MessageApp • name: String • messageList: LinkedList of Integer • userList: LinkedList of User • . • . • + addMessage(msg: int) • // add message to tail of messageList • +removeLeastRecentMessage() • // remove the least recent message in // messageList . . + message(userName: String,msg: int) // add msg to message list of user // with userName • - findUser(userName: String): User • //find & return the user with userName Week 10

  11. Methods for performing send operation Set A: Messaging Application public void message(String userName, intmsg) { User user = findUser(userName); if (user.getNumMessages() >= 10) user.removeLeastRecentMessage(); user.addMessage(msg); } Why private? private User findUser(String userName) { for (inti=0; i < userList.size(); i++) { if (userList.get(i).getName().equals(userName)) return userList.get(i); } return null; } Week 10

  12. Methods for performing send operation Set A: Messaging Application • findUser() can be performed in the code for message(), however this is also required for the reply operation, thus this code can be extracted out into a method by itself. public void addMessage(intmsg) { messageList.add(msg); } public void removeLeastRecentMessage() { messageList.poll(); } Week 10

  13. Algorithm for reply operation Set A: Messaging Application * Here is where the message list deviates from a simple queue, as the message being replied to can change its ordering in the queue. • Find the message being replied to in receiver’s message list and make it the most recent message • Add reply to receiver’s message list in the same way as send message operation • Perform the same thing for the sender. Week 10

  14. Methods for implementing reply operation Set A: Messaging Application User MessageApp • name: String • messageList: LinkedList of Integer • userList: LinkedList of User . . + makeMessageMostRecent(msg: int) // make msg the most recent Message // in messageList . . + reply (userName: String, reply: int, msg: int) // change ordering of msg in message // list of user with userName and add // reply to message list Week 10

  15. Methods for performing replyoperation Set A: Messaging Application public void reply(String userName, int reply, intmsg) { User user = findUser(userName); // make msg most recent message user.makeMessageMostRecent(msg); // add reply which now becomes most recent message(userName,reply); } public void makeMessageMostRecent(intmsg) { // find index of msg intindex = messageList.indexOf(msg); messageList.remove(index); messageList.addLast(msg); } Week 10

  16. Output each operation Set A: Messaging Application • As usual override the toString method in User and MessageApp class to print out the message list for each user • Here we also print out the operation being performed before the message list for each user is printed Week 10

  17. Set B: Employee Management Set B: Employee Management • Simulate an employee management system • Employees belong to different ranks • Within each rank, employees are “ordered” from least recently to most recently hired in terms of increasing seniority. • Three operations • Hire • Fire • Promote Week 10

  18. Requirements Set B: Employee Management • Usage of Scanner class • Usage of Stack and/or LinkedList class • Ability to design classes to solve the problem • Ability to come up with simple algorithm quickly to perform hire, fire and promote Week 10

  19. Designing the classes Set B: Employee Management • Requires 2 classes • Employee class • Encapsulates the employee name • Encapsulates the employee rank • EmpManagement class • Encapsulate the employees in the company (has-a relationship with Employee class). • Encapsulates the methods to hire, fire and promote employees. Week 10

  20. EmpManagement uses the LIFO principle Set B: Employee Management • LIFO principle • Among employees of the same rank, the most recently hired (last in) are the ones to be fired first (first out) • Stack methods • peek() • pop() • push (E element) • What are the equivalent LinkedList methods? Week 10

  21. Using multiple stacks vs one stack Set B: Employee Management • Employee management can be implemented using - • Multiple stacks • One stack for each rank • 1 stack - employees to be stored in a certain order • Larger ranks stored at the bottom of the stack • Within each rank, least recently hired at the bottom, while most recently hired at the top Mark, rank 1 Most recently hired in rank 1 Tom, rank 1 Least recently hired in rank 1 Only one in rank 3 Cindy, rank 3 Most recently hired in rank 4 Hank, rank 4 Brad, rank 4 Least recently hired in rank 4 Kate, rank 4 Week 10

  22. Using multiple stacks vs one stack Set B: Employee Management • Using multiple stacks • Hiring is easily implemented • Firing is a bit more complicated • Promotion is about the same as using 1 stack • Using 1 stack • Hiring is a bit more complicated • Firing is easy • Promotion is about the same as using multiple stacks • The 1 stack solution (+ some temporary stack) will be presented here. Week 10

  23. Member Variables Set B: Employee Management Employee EmpManagement • name: String • rank: int • empList: Stack of Employee + Employee (aName: String, aRank: int) • … // basic accessors + EmpManagement(Scanner sc) … Week 10

  24. Algorithm for hire operation Set B: Employee Management • Pop items in empList onto a temp stack until • empList is empty or employee at top of empList has rank > new employee • Push new employee onto empList (it is at the correct position) • Pop all items on the temp stack back onto empList * Popping consecutive items from stack A onto stack B and back onto stack A again preserves their ordering in stack A. Week 10

  25. Algorithm for hire operation Set B: Employee Management Tim, rank 3 Mark, rank 1 Tim, rank 3 Tom, rank 1 Cindy, rank 3 Cindy, rank 3 Hank, rank 4 Temp stack Hank, rank 4 Brad, rank 4 Brad, rank 4 Tom, rank 1 Kate, rank 4 Kate, rank 4 Mark, rank 1 Mark, rank 1 Tom, rank 1 Tim, rank 3 Tim, rank 3 Cindy, rank 3 Cindy, rank 3 Temp stack Hank, rank 4 Hank, rank 4 Tom, rank 1 Brad, rank 4 Brad, rank 4 Mark, rank 1 Kate, rank 4 Kate, rank 4 Week 10

  26. Methods for implementing hire operation Set B: Employee Management Employee EmpManagement • name: String • rank: int • empList: Stack of Employee . . . . + hire(emp: Employee) // add emp to correct position in // empList Week 10

  27. Method for performing hireoperation Set B: Employee Management public void hire(Employee emp) { Stack<Employee> temp = new Stack<Employee>(); // pop all employees before emp onto temp while (!empList.empty() && (empList.peek().getRank() < emp.getRank())) temp.push(empList.pop()); // push employess into its correct position in empList empList.push(emp); // pop back all employess in temp into empList while (!temp.empty()) empList.push(temp.pop()); } Week 10

  28. Algorithm for fire operation Set B: Employee Management • Simply pop empList n times, where n is the number of employees to fire • This will fire employees according to the criteria • From smallest rank to largest rank • Within the same rank, from most recently hire to least recently hired Week 10

  29. Methods for implementing fireoperation Set B: Employee Management Employee EmpManagement • name: String • rank: int • empList: Stack of Employee . . . + hire(emp: Employee) • + fire(n: int) • // fire n number of employees • // according to firing criteria Week 10

  30. Algorithm for promote operation Set B: Employee Management • Pop items in empList onto temp stack until employee to be promoted is found at top of stack • Pop out employee • Push employee now with new rank back into empList using hire operation • Pop back all items in temp stack onto empList Week 10

  31. Algorithm for promote operation Set B: Employee Management Tom, rank 4 Mark, rank 1 Tom, rank 4 Tom, rank 1 Tom, rank 1 Cindy, rank 3 New rank Cindy, rank 3 Hank, rank 4 Temp stack Hank, rank 4 Brad, rank 4 Brad, rank 4 Kate, rank 4 Kate, rank 4 Mark, rank 1 Cindy, rank 3 Tom, rank 4 Cindy, rank 3 Tom, rank 4 Temp stack Temp stack Hank, rank 4 Hank, rank 4 Brad, rank 4 Brad, rank 4 Hire operation Kate, rank 4 Mark, rank 1 Kate, rank 4 Mark, rank 1 Week 10

  32. Algorithm for promote operation Set B: Employee Management Mark, rank 1 Cindy, rank 3 Tom, rank 4 Hank, rank 4 Brad, rank 4 Kate, rank 4 Week 10

  33. Methods for implementing promote operation Set B: Employee Management Employee EmpManagement • name: String • rank: int • empList: Stack of Employee . . . . + hire (emp: Employee) • + fire (n: int) • + promote (Employee emp) • // Find emp in empList and promote to // new rank Week 10

  34. Method for performing promoteoperation Set B: Employee Management public void promote(Employee emp) { Stack<Employee> temp = new Stack<Employee>(); // pop all employees before emp onto temp while (!empList.peek().getName().equals(emp.getName())) temp.push(empList.pop()); // remove employee to be promoted empList.pop(); // treat employee as newly hired employee at the new rank hire(emp); // pop back employees in temp onto empList while (!temp.empty()) empList.push(temp.pop()); } Week 10

  35. Output each operation Set B: Employee Management • As usual override the toString method in User and MessageApp class to print out the message list for each user • Here we also print out the operation performed before the message list for each user is printed • To print the employees in the company • Cannot access each employee to be printed by popping entire empList • Subsequent operations still require the empList ! • Instead, access all employees by popping them from empList but push them onto temp stack and after that pop all employees on temp stack back onto empList Week 10

  36. Part 2 Discussion on Take-Home Lab #4 Week 10

  37. Cargo Optimization Take-home Lab #4 • Problem: • 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) • Output: • Print the minimum number of stacks required for efficient loading i.e. All destination ships has its cargo at the top when it arrives Week 10

  38. Main Method Take-home Lab #4 • For each alphabet read, create a new Container • Find the best stack for this new Container (findBestStack method) • Insert it into the best stack • Create if not available • Print size of ArrayList that contains all the Stacks Week 10

  39. Take-home Lab #4 1 2 3 3a 4 Week 10

  40. Container’s calculateFitWith Method Take-home Lab #4 • calculateFitWith(Stack<Container> candidate) • Compare the compatibility of this container with candidate container • Compatibility = the character distance between this container’s destination and candidate’s destination • Smaller value is better and -1 indicates a misfit Week 10

  41. findBestStack Method Take-home Lab #4 • findBestStack(Container newArrival, ArrayList<Stack<Container>> stacks) • Go through the List to look at the top container of each Stack • Calculate compatibility of newArrival of each of the top containers using calculateFitWith • Compare and return the most compatible stack index Week 10

  42. Take-home Lab #4 1 2 3 Same destination, hence there is no better solution Week 10

  43. Part 3 Discussion on Take-Home Lab #5 Week 10

  44. CD Filling (1/4) Take-home Lab #5 • Objective: • Using recursion • Problem: • Given capacity of CD and a number of songs with their sizes, find the subset of songs whose sum of sizes is the maximum and <= CD capacity • Example: 504 10 20 30 40 CD capacity Sizes of songs Number of songs Week 10

  45. CD Filling (2/4) Take-home Lab #5 • Example: 504 10 20 30 40 • In this example, the answer is either { 10, 40 } or {20, 30 } as you cannot get a larger sum that is still <= 50. CD capacity Sizes of songs Number of songs Week 10

  46. CD Filling (3/4) Take-home Lab #5 • A very big hint: • If we try all possible combinations, then we are bound to get the answer. • What information do we need in the recursion? • CD capacity • Remaining CD capacity (after filling it with selected songs) • Number of songs • Current song • A list of songs • A list of selected songs Week 10

  47. CD Filling (4/4) Take-home Lab #5 • For those who have finished and like a challenge • Try running your program using the following test case: • 1000 30 5 10 … 145 150 • How long does it take to run? • Can you make it run in under a second? • Try looking up dynamic programming if you are interested Week 10

  48. END OF FILE

More Related