An introduction to arrays, continued. Recall from last time… public static void main ( String args[] ) { //define number of rooms final int N = 100; //define.

Slides:



Advertisements
Similar presentations
Classes And Objects II. Recall the LightSwitch Class class LightSwitch { boolean on = true; boolean isOn() { return on; } void switch() { on = !on; }
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
An introduction to arrays
Introduction to C# Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
1 More About Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 3 Wanda M. Kunkle.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Loops – While, Do, For Repetition Statements Introduction to Arrays
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Grouping Objects 2 Collections and the for-each loop Collections and the while loop.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
Java File Structure.  File class which is defined by java.io does not operate on streams  deals directly with files and the file system  File class.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Puzzle 3 1  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[]
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
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.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Arrays Construct array: new double[10] Store in variable of type double[] double[] data = new double[10];
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Indentation & Readability. What does this program do? public class Hello { public static void main ( String[] args ) { //display initial message System.out.println(
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
A: A: double “4” A: “34” 4.
Application development with Java Lecture 6 Rina Zviel-Girshin.
ITI Introduction to Computing II Lab-5 Dewan Tanvir Ahmed University of Ottawa.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
CSE 143 Lecture 9 Recursion slides created by Alyssa Harding
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Midterm 2 Review Notes on the CS 5 midterm Take-home exam due by 5:00 pm Sunday evening (11/14) Hand in your solutions under the door of my office, Olin.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Two dimensional arrays.
Introduction to programming in java
Multiple variables can be created in one declaration
An introduction to arrays
Control Statement Examples
Review Operation Bingo
Exceptions & exception handling
Exceptions & exception handling
An Introduction to Java – Part I, language basics
SELECTION STATEMENTS (2)
AP Java Warm-up Boolean Array.
CS139 October 11, 2004.
Lecture Notes – Week 2 Lecture-2
CSC 1051 – Data Structures and Algorithms I
Two dimensional arrays.
Question 1a) What is printed by the following Java program? int s;
CprE 185: Intro to Problem Solving (using C)
Two dimensional arrays.
Presentation transcript:

An introduction to arrays, continued

Recall from last time… public static void main ( String args[] ) { //define number of rooms final int N = 100; //define hotel rooms boolean rooms[] = new boolean[ N ]; //define occupied (or not) final boolean occupied = true; //initialize to all unoccupied for (int i=0; i<N; i++) { rooms[i] = !occupied; } … } In the code that follows, we will simply deal with rooms numbered 0..N-1. Also, we’ll define a variable called occupied to make things easier to read.

Recall from last time…  How do we determine if a given room k is occupied?

Recall from last time…  How do we determine if a given room k is occupied? if (rooms[k] == occupied) System.out.println( “room “ + k + “ is occupied.” ); else System.out.println( “room “ + k + “ is not occupied.” );

Recall from last time…  How do we determine if a given room k is occupied? if (rooms[k] == occupied) System.out.println( “room “ + (k+1) + “ is occupied.” ); else System.out.println( “room “ + (k+1) + “ is not occupied.” ); A third alternative to the arrays-starting-at-0-but-people- starting-at-1 problem is to represent them internally as 0..N-1 and externally as 1..N by adding 1. But we’ll stick with 0..N-1 both inside and out in these examples.

Recall from last time…  How can we find an unoccupied room?

Recall from last time…  How can we find an unoccupied room? int where = -1; for (int i=0; i<N && where==-1; i++) { if (rooms[i] == !occupied) { where = i; } if (where != -1) System.out.println( “room “ + where + “ is unoccupied.” ); else System.out.println( “Sorry. No vacancy.” );

How can we determine the percentage of rooms that are occupied?

int count = 0; for (int i=0; i<N; i++) { if (rooms[i] == occupied) ++count; } double pcnt = ((double)count) / N * 100.0; System.out.println( pcnt + “% occupied.” );

Recall from last time…  Write a piece of code to randomly find a free room. It should determine the free room #. It should yield -1 if no rooms are free.  Why is this a better approach?  Can you write this code?

Recall from last time…  Write a piece of code to randomly find a free room. It should determine the free room #. It should yield -1 if no rooms are free.  Why is this a better approach? Because we will spread the guest throughout the hotel And because we will equally use each room.  Can you write this code?

Recall from last time…  Write a piece of code to randomly find a free room. It should determine the free room #. It should yield -1 if no rooms are free. int where = -1; while (where==-1) { int k = r.nextInt( N ); if (rooms[k] == !occupied) where = k; }

Recall from last time…  Write a piece of code to randomly find a free room. It should determine the free room #. It should yield -1 if no rooms are free. int where = -1; while (where==-1) { int k = r.nextInt( N ); if (rooms[k] == !occupied) where = k; } What happens when the hotel is full?

Recall from last time…  Write a piece of code to randomly find a free room. It should determine the free room #. It should yield -1 if no rooms are free. int where = -1; for (int attempt=0; attempt<1000 && where==-1; attempt++) { int k = r.nextInt( N ); if (rooms[k] == !occupied) where = k; }

Recall from last time…  Write a piece of code to randomly find a free room. It should determine the free room #. It should yield -1 if no rooms are free. int where = -1; for (int attempt=0; attempt<1000; attempt++) { int k = r.nextInt( N ); if (rooms[k] == !occupied) { where = k; break; } Similar, but with a break.

Recall from last time…  Write a piece of code to randomly find a free room. It should determine the free room #. It should yield -1 if no rooms are free. int where = -1; for (int attempt=0; attempt<1000 && where==-1; attempt++) { int k = r.nextInt( N ); if (rooms[k] == !occupied) where = k; } But there is still a small chance that the hotel has one room. Yet we were unlucky and didn’t find it. What can we do?

Recall from last time…  Write a piece of code to randomly find a free room. It should determine the free room #. It should yield -1 if no rooms are free. int where = -1; for (int attempt=0; attempt<1000 && where==-1; attempt++) { int k = r.nextInt( N ); if (rooms[k] == !occupied) where = k; } for (int i=0; i<N && where==-1; i++) { if (rooms[i] == !occupied)where = i; } if (where != -1)… Same as before.

Recall from last time…  Write a piece of code to find a block of 3 free rooms.  It should determine the lowest room number of the 3 free rooms (otherwise, -1).  Can you write this code?

Recall from last time…  Write a piece of code to find a block of 3 free rooms. It should determine the lowest room number of the 3 free rooms (otherwise, -1). int where = -1; for (int i=0; i<N && where==-1; i++) { if (rooms[i] == !occupied && rooms[i+1] == !occupied && rooms[i+2] == !occupied ) { where = i; } This works well except for one problem. What happens if the hotel is full except for the last room?

Recall from last time… What happens if the hotel is full except for the last room - array bounds exception! How can we fix it?  Write a piece of code to find a block of 3 free rooms. It should determine the lowest room number of the 3 free rooms (otherwise, -1). int where = -1; for (int i=0; i<N && where==-1; i++) { if (rooms[i] == !occupied && rooms[i+1] == !occupied && rooms[i+2] == !occupied ) { where = i; }

Recall from last time…  Write a piece of code to find a block of 3 free rooms. It should determine the lowest room number of the 3 free rooms (otherwise, -1). int where = -1; for (int i=0; i<N-2 && where==-1; i++) { if (rooms[i] == !occupied && rooms[i+1] == !occupied && rooms[i+2] == !occupied ) { where = i; } What happens if the hotel is full except for the last room - array bounds exception! Problem solved!

Recall from last time…  Use another array for wakeup calls.  Use another array to indicate whether or not a room allows smoking or not.  Can you write this code?

Use another array to indicate whether or not a room allows smoking or not.  What data type should we use?  How many do we need?

Use another array to indicate whether or not a room allows smoking or not.  What data type should we use? boolean  How many do we need? One for each room. Another array!

Use another array to indicate whether or not a room allows smoking or not. //define number of rooms final int N = 100; //define hotel rooms boolean rooms[] = new boolean[ N ]; boolean allowsSmoking[] = new boolean[ N ]; //define occupied (or not) final boolean occupied = true; //initialize to all unoccupied for (int i=0; i<N; i++) { rooms[i] = !occupied; allowsSmoking[i] = false; } //rooms allow smoking for (int i=15; i<=25; i++) { allowsSmoking[i] = true; }

How can we modify this to find a free non-smoking room? //Code to randomly find a free room. //It determines the free room #. //It should yield -1 if no rooms are free. int where = -1; for (int attempt=0; attempt<1000 && where==-1; attempt++) { int k = r.nextInt( N ); if (rooms[k] == !occupied) where = k; } for (int i=0; i<N && where==-1; i++) { if (rooms[i] == !occupied)where = i; } if (where != -1)…

How can we modify this to find a free non-smoking room? //Code to randomly find a free room. //It determines the free room #. //It should yield -1 if no rooms are free. int where = -1; for (int attempt=0; attempt<1000 && where==-1; attempt++) { int k = r.nextInt( N ); if (rooms[k] == !occupied && !allowsSmoking[k]) where = k; } for (int i=0; i<N && where==-1; i++) { if (rooms[i] == !occupied && !allowsSmoking[i]) where = i; } if (where != -1)…

Use another array for wakeup calls.  What data type should we use? Let’s make it an integer like military time. Ex. 700 (0700) is 7AM is 1:30 PM.

Use another array for wakeup calls.  Let’s make it an integer like military time. Ex. 700 (0700) is 7AM is 1:30 PM. //define number of rooms final int N = 100; //define hotel rooms boolean[]rooms= new boolean[ N ]; boolean[]allowsSmoking= new boolean[ N ]; int[]wakeupTime= new int[ N ]; //define occupied (or not) final boolean occupied = true; //initialize to all unoccupied for (int i=0; i<N; i++) { rooms[i] = !occupied; allowsSmoking[i] = false; wakeupTime[i] = -1; //indicating no wakup time yet }