290 likes | 427 Views
An introduction to arrays, continued. Recall from last time…. 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. public static void main ( String args[] ) { //define number of rooms
E N D
Recall from last time… 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. 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; } … }
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?
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… • 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; } } What happens if the hotel is full except for the last room - array bounds exception! How can we fix it?
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 15..25 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. 1330 is 1:30 PM.
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 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 }