an introduction to arrays, continued. recall from last time… public static void main ( string...

29
An introduction to arrays, continued

Upload: neil-daniels

Post on 17-Jan-2018

223 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

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

An introduction to arrays,continued

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

Recall from last time…public static void main ( String args[] ) {

//define number of roomsfinal int N = 100;//define hotel roomsboolean rooms[] = new boolean[ N ];//define occupied (or not)final boolean occupied = true;//initialize to all unoccupiedfor (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.

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

Recall from last time… How do we determine if a given room k

is occupied?

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

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

elseSystem.out.println( “room “ + k + “ is not occupied.” );

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

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

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

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

Recall from last time… How can we find an unoccupied

room?

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

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

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

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

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

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

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

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?

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

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?

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

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;

}

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

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?

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

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;

}

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

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.

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

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?

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

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.

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

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?

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

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?

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

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

}

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

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!

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

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?

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

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?

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

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!

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

Use another array to indicate whether or not a room allows smoking or not.

//define number of roomsfinal int N = 100;//define hotel roomsboolean rooms[] = new boolean[ N ];boolean allowsSmoking[] = new boolean[ N ];//define occupied (or not)final boolean occupied = true;//initialize to all unoccupiedfor (int i=0; i<N; i++) {

rooms[i] = !occupied;allowsSmoking[i] = false;

}//rooms 15..25 allow smokingfor (int i=15; i<=25; i++) {

allowsSmoking[i] = true;}

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

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

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

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

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

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. 1330 is 1:30 PM.

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

Use another array for wakeup calls. Let’s make it an integer like military time. Ex. 700 (0700) is 7AM. 1330 is 1:30

PM.//define number of roomsfinal int N = 100;//define hotel roomsboolean[] 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 unoccupiedfor (int i=0; i<N; i++) {

rooms[i] = !occupied;allowsSmoking[i] = false;wakeupTime[i] = -1; //indicating no wakup time yet

}