Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cinema SIT-IN LAB 2 (JAVA). Problem 1.Chairs in the cinema are arranged in rows 2.Each row has a name, and a capacity 3.Allocate groups of people to the.

Similar presentations


Presentation on theme: "Cinema SIT-IN LAB 2 (JAVA). Problem 1.Chairs in the cinema are arranged in rows 2.Each row has a name, and a capacity 3.Allocate groups of people to the."— Presentation transcript:

1 Cinema SIT-IN LAB 2 (JAVA)

2 Problem 1.Chairs in the cinema are arranged in rows 2.Each row has a name, and a capacity 3.Allocate groups of people to the rows 4.When a group arrive, we’ll allocate as many of them as possible to the row with the lexicographically smallest label, and then the next one, and so on. 5.So, a group might need to split to a few different groups if needed. 6.Print number of available seats and number of groups in rows 7.Print all rows group occupied

3 Object Classes Cinema Group Row

4 Setup

5 Constraints 1)Group and Row names are unique; 2)Search queries provide existing keys (name);

6 Group – Row allocation 1)Groups are processed one at a time; 2)Fill the lexicographically smallest unfilled Row with the Group’s members; 3)If the Group has more members than can fit into that row: do the same for the next lexicographically smallest Row; 4)Repeat until you run out of Rows or Group members; 5)Success: If you ran out of Group members before Rows; 6)Failure: If you ran out of Rows, then cancel all prior Row allocations;

7 Setup o Provided with all Rows at start o The collection of Rows will never change: o No new Rows o No removal of existing Rows o No renaming of Rows o No resizing of existing Rows o Rows are already provided in lexicographical order (names)

8 Queries

9 Queries (overview) o 1 GROUP_NAME NUM_PEOPLE o Allocate Group to Rows using previous rules; o If successful, output number of Rows the Group split into; o If failed, output “not possible”; o 2 ROW_NAME o Output number of remaining available seats in the Row; o Output a space; o Output number of different groups currently in the Row; o 3 GROUP_NAME o Output names of all rows this group occupies, separated by spaces, o In lexicographical order;

10 Query 1 o 1 GROUP_NAME NUM_PEOPLE 1)Create new Group; 2)Find next lexicographically smallest unfilled Row;(Get next in order from all Rows) (Count free seats in Row) 3)Assign Row to Group and decrement unseated members;(Append to Groups in Row) 4)Loop to 1 till out of Group members or Rows. 5)Add new group to collection of all groups if success;(Append to all Groups) 6)Output number of Rows the Group splits into if not enough members; Or “not possible” if not enough rows; ◦Plan class fields based on query types!

11 Query 2 o 2 ROW_NAME 1)Find Row using name; (Search,Access from all Rows) 2)Output number of available seats;(Count free seats in Row) 3)Output space; 4)Output number of different Groups in it;(Count allocated Groups in Row) o Plan class fields based on query types!

12 Query 3 o 3 GROUP_NAME 1)Find Group using name; (Search, Access from all Groups) 2)Output names of allocated Rows;(Ordered iteration of Rows in Group) Separated by spaces; In lexicographical order; o Plan class fields based on query types!

13 Planning the Classes Cinema Group Row

14 All query operations Cinema : All GroupsCinema : All Rows Search/Access Append Search/Access Get next in order In GroupIn Row Count Ordered Iteration Append

15 Cinema (purpose) Main class, program entry point; Jobs (common among all main classes): Parse setup input; (if any) Perform setup; (if any) Parse query input; Perform queries; Track all needed objects (maintain master collection of free floating objects)

16 Cinema (data fields) What floating objects are there? Rows, Groups What operations are we supporting? Search/Access (both) Append (Groups) Next in order (Rows) Collection of all Rows; Collection of all Groups;

17 Cinema (data fields) What collection type / interface to use? List allRows; List allGroups; All RowsAll Groups Operations: search/access, next in order There are no duplicates. Operations : search/access, append There are no duplicates. What type of collection maintains order and supports search/access? List What type of collection optimizes for search/access/append, and excludes duplicates? Set (not learnt) Ok, what’s the next best collection you know? List (for now..)

18 Cinema (data fields) What data structure implementation to use? List allRows; allRows = new ArrayList (); List allGroups; allGroups = new ArrayList (); LinkedListArrayList Fast insert, delete, append, prepend Slow search/access Fast search/access, append Slow insert, delete, prepend All RowsAll Groups ArrayList for fast search/access ArrayList for fast search/access, append

19 All query operations Cinema : All GroupsCinema : All Rows Search/Access Append Search/Access Get next in order In RowIn Group Count Ordered Iteration Append

20 Row (data fields) What default data do Rows have? Name, Capacity What operations are we supporting? Count Count of allocated Groups; Count of free seats;

21 Row (data fields) What collection types / interface to use? You don’t need special collections / data structures for counts. Just use an integer please. int _numGroups; int _numFreeSeats;

22 All query operations Cinema : All GroupsCinema : All Rows Search/Access Append Search/Access Get next in order In RowIn Group Count Ordered Iteration Append

23 Group (data fields) What default data do Groups have? Name, Size What operations are we supporting? Ordered Iteration Append Collection of allocated Rows;

24 Group (data fields) What collection type / interface to use? Allocated Rows Operations : ordered iteration, append There are no duplicates. What type of collection supports iteration of all elements in a specific order and appending? List List _rows;

25 Group (data fields) What data structure implementation to use? List _rows; _rows = new LinkedList (); LinkedListArrayList Fast insert, delete, append, prepend Slow search/access Fast search/access, append Slow insert, delete, prepend Allocated Rows Either will do.

26 Planning the Classes public class Cinema List allRows = new ArrayList<>(); List allGroups = new ArrayList<>(); class Group String _name; int _size; List allRows = new LinkedList<>(); class Row String _name; int _capacity; int _numFreeSeats; int _numGroups;

27 Optimise Queries USING ADDITIONAL INFORMATION FROM THE PROBLEM STATEMENT

28 Optimise – Query 1 o Looking for next lexicographically smallest unfilled Row Rows already provided in order Rows already filled in order Maintain an index/pointer to the smallest unfilled Row from the last Query 1 Increment index/pointer every time you fill a Row during Query 1 No need to check all Rows to find next smallest unfilled o Checking if Group can fit in Cinema Maintain a count of the remaining seats across all Rows Decrement as you accommodate groups from Query 1 No need to keep checking all Rows

29 Optimise – Query 2 o Searching for Row by name Rows already provided in order Perform binary search! Collections.binarySearch Requires that Row implements Comparable interface, Or you implement a Comparator Exercise: read up on java Comparable and Comparator O(log(n)) time vs old O(n) time

30 Optimise – Query 3 o Get all allocated Rows in lexicographical order o Already in lexicographical order (rows had to be allocated in order) o Just iterate through List from first to last o No need to sort / search for next smallest

31 END NEVER TUNNEL VISION WHEN PLANNING


Download ppt "Cinema SIT-IN LAB 2 (JAVA). Problem 1.Chairs in the cinema are arranged in rows 2.Each row has a name, and a capacity 3.Allocate groups of people to the."

Similar presentations


Ads by Google