Presentation is loading. Please wait.

Presentation is loading. Please wait.

More arrays Primitive vs. reference parameters. Arrays as parameters to functions.

Similar presentations


Presentation on theme: "More arrays Primitive vs. reference parameters. Arrays as parameters to functions."— Presentation transcript:

1 More arrays Primitive vs. reference parameters. Arrays as parameters to functions.

2 Recall from our room reservation problem how we initialized our hotel array. final intN = 100; //# of rooms //note: there are N rooms which are subscripted 0..N-1. booleanrooms[] = new boolean[ N ]; //init all rooms to unoccupied for (int i=0; i<N; i++) { rooms[i] = false; } Can we put this code in a function that we call from our main function? Can we use arrays as function parameters?

3 Warning!Technicaldefinitionsahead.

4 Java types “There are two kinds of types in the Java programming language: 1. primitive types and 2. reference types.” from http://docs.oracle.com/javase/specs/jls/se7/jl s7.pdf

5 Java values “There are, correspondingly, two kinds of data values that can be stored in variables, passed as arguments, returned by methods, and operated on: 1. primitive values and 2. reference values.” from http://docs.oracle.com/javase/specs/jls/se7/jl s7.pdf

6 Primitive types boolean boolean byte byte short short int int long long char char float float double double

7 Reference types “There are four kinds of reference types: class types, interface types, type variables, and array types.” from http://docs.oracle.com/javase/specs/jls/se7/jl s7.pdf

8 Practical example int i = 10;// i and j are primitives int j; int[] A;// A and B are references Int[] B;

9 Practical example int i = 10;// i and j are primitives int j = i; j = 5; System.out.println( i ); int[] A = new int [ 10 ];// A and B are references A[0] = 5; int[] B = A; B[0] = 9; System.out.println( A[0] );

10 Practical example int i = 10;// i and j are primitives int j = i; j = 5; System.out.println( i ); int[] A = new int [ 10 ];// A and B are references A[0] = 5; int[] B = A; B[0] = 9; System.out.println( A[0] ); i ----- 10

11 Practical example int i = 10;// i and j are primitives int j = i; j = 5; System.out.println( i ); int[] A = new int [ 10 ];// A and B are references A[0] = 5; int[] B = A; B[0] = 9; System.out.println( A[0] ); i ----- 10 j ----- 10

12 Practical example int i = 10;// i and j are primitives int j = i; j = 5; System.out.println( i ); int[] A = new int [ 10 ];// A and B are references A[0] = 5; int[] B = A; B[0] = 9; System.out.println( A[0] ); i ----- 10 j ----- 10 5 X

13 Practical example int i = 10;// i and j are primitives int j = i; j = 5; System.out.println( i );10 int[] A = new int [ 10 ];// A and B are references A[0] = 5; int[] B = A; B[0] = 9; System.out.println( A[0] ); i ----- 10 j ----- 10 5 X

14 Practical example int i = 10;// i and j are primitives int j = i; j = 5; System.out.println( i );10 int[] A = new int [ 10 ];// A and B are references A[0] = 5; int[] B = A; B[0] = 9; System.out.println( A[0] ); i ----- 10 j ----- 10 5 X A -----. ------- … -------

15 Practical example int i = 10;// i and j are primitives int j = i; j = 5; System.out.println( i );10 int[] A = new int [ 10 ];// A and B are references A[0] = 5; int[] B = A; B[0] = 9; System.out.println( A[0] ); i ----- 10 j ----- 10 5 X A -----. ------- 5 ------- … -------

16 Practical example int i = 10;// i and j are primitives int j = i; j = 5; System.out.println( i );10 int[] A = new int [ 10 ];// A and B are references A[0] = 5; int[] B = A; B[0] = 9; System.out.println( A[0] ); i ----- 10 j ----- 10 5 X A -----. ------- 5 ------- … ------- B -----.

17 Practical example int i = 10;// i and j are primitives int j = i; j = 5; System.out.println( i );10 int[] A = new int [ 10 ];// A and B are references A[0] = 5; int[] B = A; B[0] = 9; System.out.println( A[0] ); i ----- 10 j ----- 10 5 X A -----. ------- 5 9 ------- … ------- B -----. X

18 Practical example int i = 10;// i and j are primitives int j = i; j = 5; System.out.println( i );10 int[] A = new int [ 10 ];// A and B are references A[0] = 5; int[] B = A; B[0] = 9; System.out.println( A[0] );9 i ----- 10 j ----- 10 5 X A -----. ------- 5 9 ------- … ------- B -----. X

19 Recall from our room reservation problem how we initialized our hotel array. final intN = 100; //# of rooms booleanrooms[] = new boolean[ N ]; //init all rooms to unoccupied for (int i=0; i<N; i++) { rooms[i] = false; } Can we put this code in a function that we call from our main function?Yes! Can we use arrays as function parameters? Yes!

20 Our hotel reservation system. class Hotel { static void initializeHotel ( boolean occupied[] ) { //init all rooms to unoccupied for (int i=0; i<occupied.length; i++) { occupied[i] = false; }} public static void main ( String args[] ) { //How do we use this new function from main? }}

21 Our hotel reservation system. class Hotel { static void initializeHotel ( boolean occupied[] ) { //init all rooms to unoccupied for (int i=0; i<occupied.length; i++) { occupied[i] = false; }} public static void main ( String args[] ) { final int N = 100; //# of rooms //var to indicate occupied/unoccupied rooms boolean rooms[] = new boolean[ N ]; //How do we use initializeHotel from main? }}

22 Our hotel reservation system. class Hotel { static void initializeHotel ( boolean occupied[] ) { //init all rooms to unoccupied for (int i=0; i<occupied.length; i++) { occupied[i] = false; }} public static void main ( String args[] ) { final int N = 100; //# of rooms //var to indicate occupied/unoccupied rooms boolean rooms[] = new boolean[ N ]; initializeHotel( rooms ); }}

23 Our hotel reservation system. class Hotel { //init our hotel to all empty static void initializeHotel ( boolean occupied[] ) { //init all rooms to unoccupied for (int i=0; i<occupied.length; i++) { occupied[i] = false; }} public static void main ( String args[] ) { final int N = 100; //# of rooms //var to indicate occupied/unoccupied rooms boolean rooms[] = new boolean[ N ]; initializeHotel( rooms ); }} This is an example of a parameter with a reference value. Changes made in initializeHotel to occupied array entries change rooms array entries in main.

24 Our hotel reservation system. How about another function to find the first free (available/unoccupied) room? How about another function to find the first free (available/unoccupied) room? It should return the room # of the first available room. It should return the room # of the first available room. If no rooms are available, it should return -1. If no rooms are available, it should return -1. We will also specify a range of room numbers to check (we won’t always check the entire hotel). We will also specify a range of room numbers to check (we won’t always check the entire hotel).

25 So far we have… class Hotel { static void initializeHotel ( boolean occupied[] ) { …} public static void main ( String args[] ) { …}}

26 So far we have… class Hotel { static void initializeHotel ( boolean occupied[] ) { …} static int firstFree ( int first, int last, boolean occupied[] ) { …} public static void main ( String args[] ) { …}}

27 Our hotel reservation system: first free room //This function returns the number of the first available // in the range of [first,last] inclusive. // If no room can be found, -1 is returned. static int firstFree ( int first, int last, boolean occupied[] ) { int found = -1; for (int i=first; i<=last; i++) { //find the first room that is not occupied if ( !occupied[i] ) { found = i;//remember the free room # break;//terminate the loop }} return found; }

28 How can we use this new function in main to check is a room between # 10 and # 20 is available?

29 public static void main ( String args[] ) { final int N = 100; //# of rooms //var to indicate occupied/unoccupied rooms boolean rooms[] = new boolean[ N ]; initializeHotel( rooms ); //check for a room between 10 and 20 int rm = firstFree( 10, 20, rooms ); System.out.println( "room # " + rm + " is available" ); } In the call to firstFree, 10 and 20 are primitive values, and rooms is a reference value.

30 How can we use this new function in main to check our entire hotel for the first free room?

31 How can we use this new function in main to check our entire hotel? //check for a room in the entire hotel int r = firstFree( 0, N-1, rooms ); System.out.println( "room # " + r + " is available" ); In this call to firstFree, 0 and N- 1 are primitive values, and rooms is a reference value.

32 Passed by value vs. passed by reference So what’s the difference? So what’s the difference? For a primitive value, any changes to the variable in the function DO NOT affect the caller. For a primitive value, any changes to the variable in the function DO NOT affect the caller. For a reference value, any changes in the function to the data to which the reference refers DO affect the caller. For a reference value, any changes in the function to the data to which the reference refers DO affect the caller.

33 Passed by value vs. passed by reference example. static void test ( int A, int B[] ) { A = A + 12; B[0] = B[0] + 12; }… In main: int A = 5; int D[] = new int [2]; D[0] = 5; D[1] = 5; test( A, D ); System.out.println( "A is " + A + “, D[0] is " + D[0] ); What’s the output? Remember, A is a primitive value, and D is a reference value.

34 Passed by value vs. passed by reference example. static void test ( int A, int B[] ) { A = A + 12; B[0] = B[0] + 12; }… In main: int A = 5; int D[] = new int [2]; D[0] = 5; D[1] = 5; test( A, D ); System.out.println( "A is " + A + “, D[0] is " + D[0] ); What’s the output? A is 5, D[0] is 17 test made changes to both A and B[0] but since A is a primitive value, those changes don’t affect A in main. Since B is a reference value, the changes made by test to B[0] do affect D[0] in main.

35 Back to our hotel reservation system… Write a function that, given a room number and our rooms array, indicates that that room is occupied. Write a function that, given a room number and our rooms array, indicates that that room is occupied.

36 So far we have… class Hotel { static void initializeHotel ( boolean occupied[] ) { …} static int firstFree ( int first, int last, boolean occupied[] ) { …} public static void main ( String args[] ) { …}}

37 So far we have… class Hotel { static void bookARoom ( int rm, boolean rooms[] ) { …} static void initializeHotel ( boolean occupied[] ) { …} static int firstFree ( int first, int last, boolean occupied[] ) { …} public static void main ( String args[] ) { …}}

38 Back to our hotel reservation system… Write a function that, given a room number and our rooms array, indicates that that room is occupied. Write a function that, given a room number and our rooms array, indicates that that room is occupied. static void bookARoom ( int rm, boolean rooms[] ) { … //what do we need to do here? }

39 Back to our hotel reservation system… Write a function that, given a room number and our rooms array, indicates that that room is occupied. Write a function that, given a room number and our rooms array, indicates that that room is occupied. static void bookARoom ( int rm, boolean rooms[] ) { rooms[ rm ] = true; }

40 Back to our hotel reservation system… //given a room number and our rooms array, // indicate that that room is occupied. static void bookARoom ( int rm, boolean rooms[] ) { //it would be wise to check rm here first to determine if // it is between 1..# of rooms. rooms[ rm ] = true; }… In main: bookARoom( 1, rooms ); bookARoom( 2, rooms ); bookARoom( 10, rooms );

41 More hotel reservation system… Wouldn’t it be useful to know how many rooms are occupied in our hotel? Wouldn’t it be useful to know how many rooms are occupied in our hotel? We can write function to do that! We can write function to do that!

42 So far we have… class Hotel { static void bookARoom ( int rm, boolean rooms[] ) { …} static void initializeHotel ( boolean occupied[] ) { …} static int firstFree ( int first, int last, boolean occupied[] ) { …} public static void main ( String args[] ) { …} static int occupiedCount ( boolean rooms[] ) { …}}

43 More hotel reservation system… //function that determines how many rooms // are occupied in our hotel static int occupiedCount ( boolean rooms[] ) { int count = 0; for (int i=0; i<rooms.length; i++) { //check here needed. }}

44 More hotel reservation system… //function that determines how many rooms // are occupied in our hotel static int occupiedCount ( boolean rooms[] ) { int count = 0; for (int i=0; i<rooms.length; i++) { if ( rooms[i] ) ++count;} return count; }

45 More hotel reservation system… //function that determines how many rooms // are occupied in our hotel static int occupiedCount ( boolean rooms[] ) { int count = 0; for (int i=0; i<rooms.length; i++) { if ( rooms[i] ) ++count;} return count; } Now, how can we use this new function from main?

46 Room in use. In main: bookARoom( 1, rooms ); bookARoom( 2, rooms ); bookARoom( 10, rooms ); System.out.println( occupiedCount(rooms) + " in use." );

47 Hotel reservation system. Other useful functions include: Other useful functions include: Write a function that returns the percentage of free rooms in the hotel. (Your function should call occupiedCount.) Write a function that returns the percentage of free rooms in the hotel. (Your function should call occupiedCount.)

48 Hotel reservation system. Other useful functions include: Other useful functions include: Write a function to allow someone to check out. Given a room number, this function should indicate that the room is no longer occupied. Write a function to allow someone to check out. Given a room number, this function should indicate that the room is no longer occupied.

49 Hotel reservation system. Other useful functions include: Other useful functions include: Write a function to find a block (i.e., contiguous) of 3 rooms. Write a function to find a block (i.e., contiguous) of 3 rooms. Return the room # of the first room in the block. Return the room # of the first room in the block. If no such block is available, return -1. If no such block is available, return -1.

50 Hotel reservation system. Other useful functions include: Other useful functions include: Write a function called findARoom that finds an available room. Write a function called findARoom that finds an available room. But we don’t want to simply use firstFree because we’ll wear out some rooms in our hotel and not use other rooms at all. But we don’t want to simply use firstFree because we’ll wear out some rooms in our hotel and not use other rooms at all. So findARoom should generate a random number and see if that room is free. If it’s not free, findARoom can then use firstFree. So findARoom should generate a random number and see if that room is free. If it’s not free, findARoom can then use firstFree.

51 RECAP

52 Java types “There are two kinds of types in the Java programming language: 1. primitive types and 2. reference types.” from http://docs.oracle.com/javase/specs/jls/se7/jl s7.pdf

53 Java values “There are, correspondingly, two kinds of data values that can be stored in variables, passed as arguments, returned by methods, and operated on: 1. primitive values and 2. reference values.” from http://docs.oracle.com/javase/specs/jls/se7/jl s7.pdf

54 Function parameters, and primitive and reference values. Changes to primitives do not affect the calling function. Changes to primitives do not affect the calling function. Changes to references (more specifically, changes to what the reference refers) do affect the calling function. Changes to references (more specifically, changes to what the reference refers) do affect the calling function.


Download ppt "More arrays Primitive vs. reference parameters. Arrays as parameters to functions."

Similar presentations


Ads by Google