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

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.
Chapter 8: Arrays.
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
An introduction to arrays
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
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.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Introduction to Java Java Translation Program Structure
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
An introduction to arrays, continued. Recall from last time… public static void main ( String args[] ) { //define number of rooms final int N = 100; //define.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
1 Arrays and Methods Java always passes arguments by value – that is a copy of the value is made in the called method and this is modified in the method.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Data Types Always data types will decide which type of information we are storing into variables In C programming language we are having 3 types of basic.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Chapter 9 Introduction to Arrays Fundamentals of Java.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Java Programming Language Lecture27- An Introduction.
Two dimensional arrays.
AKA the birth, life, and death of variables.
An introduction to arrays
CMSC 202 Static Methods.
null, true, and false are also reserved.
An Introduction to Java – Part I, language basics
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Classes and Objects 5th Lecture
Chapter 2: Java Fundamentals
CS2011 Introduction to Programming I Arrays (II)
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
AKA the birth, life, and death of variables.
Scope of variables class scopeofvars {
Classes, Objects and Methods
Two dimensional arrays.
Names of variables, functions, classes
Two dimensional arrays.
Presentation transcript:

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

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?

Warning!Technicaldefinitionsahead.

Java types “There are two kinds of types in the Java programming language: 1. primitive types and 2. reference types.” from s7.pdf

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 s7.pdf

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

Reference types “There are four kinds of reference types: class types, interface types, type variables, and array types.” from s7.pdf

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

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] );

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

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 j

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 j X

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 j X

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 j X A …

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 j X A …

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 j X A … B

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 j X A … B X

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 j X A … B X

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!

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

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

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

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.

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

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

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[] ) { …}}

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

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

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.

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

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.

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.

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.

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.

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.

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[] ) { …}}

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[] ) { …}}

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

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

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

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!

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[] ) { …}}

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

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

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?

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

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

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.

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.

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.

RECAP

Java types “There are two kinds of types in the Java programming language: 1. primitive types and 2. reference types.” from s7.pdf

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 s7.pdf

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.