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.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Collections Chapter Java Collection Frameworks The Java collection framework is a set of utility classes and interfaces. Designed for working with.
Chapter 10 Introduction to Arrays
CS 171: Introduction to Computer Science II Hashing and Priority Queues.
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
Programming with Collections Collections in Java Using Arrays Week 9.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 19 Java Data Structures
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
1.7 Arrays academy.zariba.com 1. Lecture Content 1.Basic Operations with Arrays 2.Console Input & Output of Arrays 3.Iterating Over Arrays 4.List 5.Cloning.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Writing Methods Using if, loops, and variables to implement algorithms Copyright © 2012 Pearson Education, Inc.
Data Structures Using C++ 2E
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Lists in Python.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 11 Arrays Continued
ICS 145B -- L. Bic1 Project: Main Memory Management Textbook: pages ICS 145B L. Bic.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Generic Programming Using the C++ Standard Template Library.
Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
Chapter 18 Java Collections Framework
Computer Science 209 Software Development Java Collections.
Data structures Abstract data types Java classes for Data structures and ADTs.
Data structures and algorithms in the collection framework 1.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
ALGORITHMS.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
Problem 1 Bank.  Manage customers’ bank account using the following operations: Create a new account given a customer’s name and initial account. Deposit.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
List Interface and Linked List Mrs. Furman March 25, 2010.
Processing Sequences of Elements Technical Trainer Telerik Corporation Doncho Minkov.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
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.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
Arrays Chapter 7. 2 Declaring and Creating Arrays Recall that an array is a collection of elements all of the _____________ Array objects in Java must.
1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Take-Home Lab #02 CS1020 – DATA STRUCTURES AND ALGORITHMS 1 AY SEMESTER 2 1.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
CSCE Do Loops Simplest form DO j = 1,100 PRINT *,j END DO Variable j runs from 1 to 100 counting by ones.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
Keys and adding, deleting and modifying records in an array ● Record Keys ● Reading and Adding Records ● Partition or Sentinels Marking Space in Use ●
Processing Sequences of Elements
Chapter 5 Ordered List.
Chapter 19 Java Data Structures
Containers and Lists CIS 40 – Introduction to Programming in Python
Chapter 20 Lists, Stacks, Queues, and Priority Queues
ENEE150 Discussion 09 Section 0101 Adam Wang.
Hash Tables.
Dynamic Data Structures and Generics
The Generic List<> Collection class
Data Structures & Algorithms
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Arrays.
Presentation transcript:

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 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 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;

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)

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_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;

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!

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!

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!

Planning the Classes Cinema Group Row

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

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 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..)

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

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

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;

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 GroupsCinema : All Rows Search/Access Append Search/Access Get next in order In RowIn Group Count Ordered Iteration Append

Group (data fields) What default data do Groups have? Name, Size What operations are we supporting? Ordered Iteration Append 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 _rows;

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.

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;

Optimise Queries USING ADDITIONAL INFORMATION FROM THE PROBLEM STATEMENT

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

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

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

END NEVER TUNNEL VISION WHEN PLANNING