cinema sit-in lab 2 (java). problem 1.chairs in the cinema are arranged in rows 2.each row has a...
TRANSCRIPT
CinemaSIT-IN LAB 2 (JAVA)
Problem1. 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
Object Classes
Cinema
Group
Row
Setup
Constraints
1) Group and Row names are unique;
2) Search queries provide existing keys (name);
Group – Row allocation1) 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;
Setupo Provided with all Rows at start
o The collection of Rows will never change:o No new Rowso No removal of existing Rowso No renaming of Rowso No resizing of existing Rows
o Rows are already provided in lexicographical order (names)
Queries
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_NAMEo 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_NAMEo Output names of all rows this group occupies, separated by spaces, o In lexicographical order;
Query 1o 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!
Query 2o 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!
Query 3o 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!
Planning the Classes
Cinema
Group
Row
All query operations
Cinema : All Groups Cinema : All Rows
• Search/Access• Append
• Search/Access• Get next in order
In Group In Row
• Count <free seats>• Count <allocated Rows>
• Ordered Iteration <allocated Groups>• Append <allocated Groups>
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)
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;
Cinema (data fields)
What collection type / interface to use?
List<Row> allRows; List<Group> allGroups;
All Rows All 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..)
Cinema (data fields)
What data structure implementation to use?
List<Row> allRows;allRows = new ArrayList<Row>();
List<Group> allGroups;allGroups = new
ArrayList<Group>();
LinkedList ArrayList
• Fast insert, delete, append, prepend• Slow search/access
• Fast search/access, append• Slow insert, delete, prepend
All Rows All Groups
ArrayList for fast search/access ArrayList for fast search/access, append
All query operations
Cinema : All Groups Cinema : All Rows
• Search/Access• Append
• Search/Access• Get next in order
In Row In Group
• Count <free seats>• Count <allocated Groups>
• Ordered Iteration <allocated Rows>• Append <allocated Rows>
Row (data fields)
What default data do Rows have?Name, Capacity
What operations are we supporting?Count <allocated Groups>
Count <free seats>
Count of allocated Groups;
Count of free seats;
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;
All query operations
Cinema : All Groups Cinema : All Rows
• Search/Access• Append
• Search/Access• Get next in order
In Row In Group
• Count <free seats>• Count <allocated Groups>
• Ordered Iteration <allocated Rows>• Append <allocated Rows>
Group (data fields)
What default data do Groups have?Name, Size
What operations are we supporting?Ordered Iteration <allocated Rows>
Append <allocated Rows>
Collection of allocated Rows;
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<Row> _rows;
Group (data fields)
What data structure implementation to use?
List<Row> _rows;_rows = new LinkedList<Row>();
LinkedList ArrayList
• Fast insert, delete, append, prepend• Slow search/access
• Fast search/access, append• Slow insert, delete, prepend
Allocated Rows
Either will do.
Planning the Classespublic class Cinema
List<Row> allRows = new ArrayList<>();List<Group> allGroups = new ArrayList<>();
class Group
String _name;int _size;
List<Row> allRows = new LinkedList<>();
class Row
String _name;int _capacity;
int _numFreeSeats;int _numGroups;
Optimise QueriesUSING ADDITIONAL INFORMATION FROM THE PROBLEM STATEMENT
Optimise – Query 1o 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
Optimise – Query 2o 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<Row>• Exercise: read up on java Comparable and Comparator
• O(log(n)) time vs old O(n) time
Optimise – Query 3o 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 lasto No need to sort / search for next smallest
ENDNEVER TUNNEL VISION WHEN PLANNING