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.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

1 Repetition structures Overview while statement for statement do while statement.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Information Hiding and Encapsulation
Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
1)Never start coding unless you understand the task! 2)Gather requirements first. This means identify the problem and ask questions about it. Now you kind.
1 Genericity Parameterizing by Type. 2 Generic Class One that is parameterized by type  Works when feature semantics is common to a set of types On object.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Chapter 6 Iteration.  Executes a block of code repeatedly  A condition controls how often the loop is executed while (condition) statement  Most commonly,
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Stacks and Queues Pepper. Why History Simplicity Operating Systems – Function Call Stack.
Collection 105 Yola. To store data in RAM Variables (name all the types with their length) Arrays (one, two or more) Collections and Maps.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Unit Testing CSIS 3701: Advanced Object Oriented Programming.
Lab 1 Logbook ADT. OVERVIEW A monthly logbook consists of a set of entries, one for each day of the month.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
Problem 1 Bank.  Manage customers’ bank account using the following operations: Create a new account given a customer’s name and initial account. Deposit.
Chemistry CS1020.  Given a set of mappings and chemical formula: Calculate the molecule mass of the formula Problem Description C12 H1 N14 O16 (NH 4.
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.
Jeopardy Print Me Which loop? Call Me Name Me My Mistake Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400 Q $500 Final Jeopardy.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Take-Home Lab #02 CS1020 – DATA STRUCTURES AND ALGORITHMS 1 AY SEMESTER 2 1.
OOP Basics Classes & Methods (c) IDMS/SQL News
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
CPSC 233 Tutorial Xin Liu Feb 14, Tips on keyboard input How to detect that the user just hit enter when prompted for a string import java.util.Scanner;
1 Iterator Pattern (A Behavioral Pattern) Prepared by: Neha Tomar.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Contest Algorithms January 2016 Describe the maxflow problem, explain the Ford-Fulkerson, Edmonds-Karp algorithms. Look at the mincut problem, bipartite.
Sit-in Lab 4 Orders Topic: Stack and Queue. Overview Simulate an order management system that:  Can take multiple orders and processing them according.
CS1020 – Data Structures And Algorithms 1 AY Semester 2
The need for Programming Languages
CS1020 – Data Structures And Algorithms 1 AY Semester 2
Chapter 8 – Arrays and Array Lists
Java Programming by Ramiro Rocha.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Coming up Constructors Overloading With one parameter
User input We’ve seen how to use the standard output buffer
Introduction to Methods in java
Repetition.
Week 6 Discussion Word Cloud.
COMPUTER 2430 Object Oriented Programming and Data Structures I
While Statement.
Unit 3 - The while Loop - Extending the Vic class - Examples
Assignment 7 User Defined Classes Part 2
Lec 4: while loop and do-while loop
The Basics of Recursion
COMPUTER 2430 Object Oriented Programming and Data Structures I
Array Lists CSE 1310 – Introduction to Computers and Programming
Outline Software Development Activities
Local variables and how to recognize them
Web Design & Development Lecture 6
Objects with ArrayLists as Attributes
More on iterations using
Validation We have covered one way to get user input, using Scanner to get it from the console We will soon cover at least one additional way to get.
Presentation transcript:

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 available table to a specific group A specific group leaves the restaurant Prints the name of the table that a specific group is currently in Prints the name of the group of a specific table Problem Description

Restaurant Group Table Group Problem Illustration

Restaurant GroupTable Contains Has Uses Relationship Between Entities

Class Overview Restaurant Keeps a list of all tables and groups currently in the restaurant Reads and executes the queries Table Contains information about: Capacity of the table The group that is assigned to it The status of the table Its name Group Contains information about: The number of people The assigned table The group’s name

 Class Restaurant Class Overview Restaurant -tables : ArrayList -groups : ArrayList +//methods  Class Table Table -name : String -capacity : int -isOccupied : Boolean -currentGroup : Group +getName() : String +reset() : void -toggle() : void …  Class Group Group -name : String -capacity : int -currentTable : Table +getName() : String +getCapacity() : int +getTable() : Table … No need getter-setter for Restaurant; Restaurant acts as a “Controller”

public void setGroup(Group g) { this.currentGroup = g; this.toggle(); } public void reset() { this.currentGroup = null; this.toggle(); } private void toggle() { this.isOccupied = !this.isOccupied; } Private Helper in Table

Constructors Restaurant: public class Restaurant() Group: class Group(String name, int numOfPeople, Table assignedTable) Table: class Table(String name, int capacity) Some questions to ponder: When do we create a new group?  When we can assign it a table. A group cannot “queue”, i.e. if it could not be assigned a table, it leaves the restaurant. When do we add tables (or groups) to the list in Restaurant?  After the creation of each object. When do we remove tables (or groups) from the list in Restaurant?  You remove a group from the list when it leaves the restaurant. You do not remove tables from the list.

 Some tips for processing the queries:  Performs all logical operations in the Restaurant class  Avoid the use of static variables and methods  The methods in the table and group class are only for operations that modifies or returns their own attributes. These operations are called by the Restaurant class. E.g:  getName(): returns the name of the table (or group)  reset(): resets a table back to vacant (and toggle the status of the table)  We define helper methods in Restaurant as we go along, e.g:  getTableBasedOnName(String tableName): returns the Table with the name tableName, returns null if not found  getValidLexiSmallestTable(int numOfPeople): returns the lexicographically smallest Table that fits the group with numOfPeople. Useful Tips

How to avoid static In the Restaurant Class  What most of you did: public static void main(String[] args) { //your code } public static Table getTableBasedOnName(String name) { //implementation }  How to avoid static: public static void main(String[] args) { Restaurant myRestaurant = new Restaurant(); myRestaurant.run(); } public void run() { //your code (treat this as “main”) } public Table getTableBasedOnName(String name) { //implementation }

The Run Method (in Restaurant) public void run() { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); sc.nextLine(); readAndStoreTables(sc, N); int Q = sc.nextInt(); sc.nextLine(); executeQueries(sc, Q); }

The Run Method (in Restaurant) public void run() { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); sc.nextLine(); readAndStoreTables(sc, N); int Q = sc.nextInt(); sc.nextLine(); executeQueries(sc, Q); }

Read and Store Tables private void readAndStoreTables(Scanner sc, int N) { for (int i = 0; i < N; i++) { //reads the input and split it if //a blank space (" ") is found String[] split = sc.nextLine().split(" "); //store table information into local variables String tableName = split[0]; int capacity = Integer.parseInt(split[1]); //create new table object and store it into //the list of tables Table newTable = new Table(tableName, capacity); tables.add(newTable);} }

Execute Queries private void executeQueries(Scanner sc, int Q) { for (int i = 0; i < Q; i++) { String[] splittedInput = sc.nextLine().split(" "); int input = Integer.parseInt(splittedInput[0]); //execute queries based on type if (input == 1) { assignFavoriteTable(splittedInput); } else if (input == 2) { assignLexiSmallestTable(splittedInput); } else if (input == 3) { leaveRestaurant(splittedInput); } else if (input == 4) { printOccupiedTable(splittedInput); } else { printCurrentGroupName(splittedInput); }

 Query 1 Query 1: Assign group to favorite table private void assignFavoriteTable(String[] splittedInput) { String groupName = splittedInput[1]; int numOfPeople = Integer.parseInt(splittedInput[2]); String tableName = splittedInput[3]; Table favTable = getTableBasedOnName(tableName); } If table is not null and can assign*  create group and assign table If not  print “not possible” *Can assign if: 1. The capacity of favTable is at least numOfPeople 2. favTable is currently vacant

 Helper Method (Reusable for query 1 and 5) /* * pre-condition: The arraylist of tables has been initialized and tables * have been stored properly * * post-condition: Returns the table with name tableName and null if there * is no such table * * description: iterates through the list of tables and return the most * appropriate table based on tableName. Returns null if no such table is * found */ private Table getTableBasedOnName(String tableName) { } Helper Method: getTableBasedOnName -Loop through the list of tables -If the current table’s name matches tableName, return that Table and stop the loop. Why can we stop? -After finish iterating and no table is found: return null

private void assignLexiSmallestTable(String[] splittedInput) { String groupName = splittedInput[1]; int numOfPeople = Integer.parseInt(splittedInput[2]); Table assigned = getValidTable(numOfPeople); }  Query 2 Query 2: Assign group to a table If table is not null*  create group and assign table If null (we don’t find a table that fits)  print “not possible” *We don’t need to check if it is assignable as in query 1 since we do the checking inside getValidTable(). Why?  we will iterate through all tables and find the lexicographically smallest valid table. Hence, as we iterate, we also check its validity.  Why do we need to iterate through all tables? Think!

/* * pre-condition: ??? * post-condition: ??? * description: ??? */ private Table getValidTable(int numOfPeople) { }  Helper Method Helper Method: getValidTable -Loop through the list of tables while keeping the current lexi- smallest and valid table in a local variable -For each table: check its validity. If it’s invalid, skip. If it’s valid, compare with the current lexi-smallest table. If this new table is lexicographically smaller, update the local variable that keeps the table. -Return the lexi-smallest available table (if it exists) or null otherwise

 Query 3 Query 3: A group leaves the restaurant private void leaveRestaurant(String[] splittedInput) { String groupName = splittedInput[1]; Group currentGroup = getGroupBasedOnName(groupName); } If currentGroup is not null  reset its table and assign null as currentGroup ’s table If null (there is no such group)  do nothing The implementation of getGroupBasedOnName is similar to getTableBasedOnName. This helper is useful for Query 3 and 4.

 Query 4 Query 4 : Prints the current group’s table Why?  we only add groups to the list when we assign it a table  when a group leaves, we remove it from the list of groups private void printOccupiedTable(String[] splittedInput) { String groupName = splittedInput[1]; Group currentGroup = getGroupBasedOnName(groupName); } If currentGroup is not null  print its table name. We do not need to check if it has a table or not. Why*? If null  print “invalid”

 Query 5 private void printCurrentGroupName(String[] splittedInput) { String tableName = splittedInput[1]; Table curTable = getTableBasedOnName(tableName); } Query 5 : Prints the current group of the table If curTable is not null and it is occupied  print the occupying group’s name. Else  print “invalid”

The End