University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design III, Parameter/Scope Review Lecture.

Slides:



Advertisements
Similar presentations
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Advertisements

Written by: Dr. JJ Shepherd
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Interfaces, Polymorphism Lecture 20, Tue Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Inheritance II Lecture 34, Mon Apr
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Inheritance II Lecture 23, Thu Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Mathematical Operations, Static Methods Lecture 9, Thu Feb 2.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Interfaces, Polymorphism II Lecture 21, Thu Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Interfaces, Inheritance Lecture 22, Tue Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Advanced Class Design Lecture 19, Thu Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Parameter/Scope Review II, Inheritance Lecture 33,
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 11, Fri Jan
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Arrays II Lecture 15, Thu Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Mathematical Operators, Static Methods Lecture 14,
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design Lecture 6, Tue Jan
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 2D Arrays, Sorting Lecture 16, Tue Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Static Methods, Conditionals Lecture 15, Mon Feb 8.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!
Writing methods and Java Statements. Java program import package; // comments and /* … */ and /** javadoc here */ public class Name { // instance variables.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 20, Fri Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops I Lecture 17, Fri Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Arrays II Lecture 22, Wed Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops II Lecture 13, Thu Feb
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 30, Mon Mar
Terms and Rules Professor Evan Korth New York University (All rights reserved)
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Interfaces, Polymorphism Lecture 29, Fri Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 2D Arrays, Sorting Lecture 23, Fri Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Arrays and Class Design Lecture 18, Tue Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Arrays Lecture 21, Mon Mar
CSE 115 Week 4 February 4 - 8, Monday Announcements Software installation fest Tuesday & Wednesday 4-7 in Baldy 21. Software installation fest Tuesday.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 12, Mon Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design II Lecture 31, Wed Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Conditionals II Lecture 11, Thu Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Yet More Array Practice Lecture 24, Mon Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Interfaces, Polymorphism Lecture 28, Wed Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 13, Wed Feb
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops II Lecture 18, Mon Mar
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9.
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
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.
CS 121 – Intro to Programming:Java - Lecture 7 Announcements A new Owl assignment is available. Programming assignment 4 is due on Thursday - hand in on.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
More Methods and Arrays Material from Chapters 5 to 7 that we didn’t cover in 1226.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Written by: Dr. JJ Shepherd
1 Introduction to Computer Science for Majors II CPSC 233, Winter 2013 CPSC 233, winter 2013 Tutorial 7, Feb 6/7, 2013.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design II Lecture 7, Thu Jan
Recap Week 2 and 3.
SPL – PS3 C++ Classes.
Presentation transcript:

University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design III, Parameter/Scope Review Lecture 32, Wed Apr borrowing from slides by Kurt Eiselt

2 News n you should already have a good start on A3 n don't wait until the last minute, it's substantial n reminder that pair programming can only be groups of 2 (not 3 or more) n make sure to check your ugrad account (or forward it) to see your detailed marking report for assignments n inform me ASAP, by end of this week at the lastest, if you have a final exam conflict/hardship

3 News II n update for the 20% assignment mark breakdown n three main assignments are each worth 6%, not 4% as the writeups say n all the weekly reading questions combined are worth 2%. n tutorials now over for the term, except Friday Apr 9 makeup sessions for Apr 2 holiday cancellation. n final review session will be Mon Apr 26 10am-12pm, room TBA.

4 News: Midterm Correction Lab n you can earn up to 10% of marks that you missed back by working through what you got wrong to find correct answers n do your new version on separate sheets of paper n don't mark up the original midterm n as with all labs, if you don't finish during the time slot you can turn in at beginning of next week's lab n pick up your midterm after class or in lab

5 Reading n Last week was Chap 8 n This week is Chap 11, except n 2nd edition: Chap 13, except n Weeklies due for last week either last Wed 3/31 or this Wed 4/7 (since no class Fri, Mon) n This week's weekly due Fri as usual

6 Recap: Bunnies n Bunny.java n NamedBunny.java - int x - int y - int numCarrots +Bunny() +hop(int direction) +displayInfo() +Bunny(int x, int y, int numCarrots, String name)

7 Even More Bunnies Question 5: [16 marks] The world desperately needs better bunny management software, so please help by writing a BunnyHerd class. A BunnyHerd object holds an array of Bunny objects. Your BunnyHerd class definition should include the following four methods: constructor Expects two parameters, an integer representing the maximum number of bunnies in the herd, and a String for the name of the herd. addBunny(int xPos, int yPos, int carrots,String name) Expects four parameters, the X- and Y-coordinates of the bunny, the number of carrots, and the name. This method creates a new Bunny object and stores the reference to the object in the next available location in the BunnyHerd object. deleteBunny(String name) Expects one parameter, the name of the bunny. This method removes from the BunnyHerd object all references to bunnies with the given name by overwriting those references with the null pointer. This method does not change the pointer to the next available location in the BunnyHerd object. printHerd() This method uses the toString() method of the Bunny object to print information about every Bunny in the herd.

8 Even More Bunnies n BunnyHerd.java

9 Bunnies and Interfaces public interface Bunnies { public void moveBunny(int direction); }

10 Bunnies and Interfaces public class BigBunny implements Bunnies { private int x, y; private int carrots; public BigBunny() { x = 5; y = 5; carrots = 10; } public void moveBunny(int direction) { if (direction == 12) { y = y + 3; carrots = carrots - 2; }

11 Bunnies and Interfaces else if (direction == 3) { x = x + 3; carrots = carrots - 2; } else if (direction == 6) { y = y - 3; carrots = carrots - 2; } else if (direction == 9) { x = x - 3; carrots = carrots - 2; } else { System.out.println("Invalid direction"); }

12 Bunnies and Interfaces public class LittleBunny implements Bunnies { private int x, y; private int carrots; public LittleBunny() { x = 5; y = 5; carrots = 10; } public void moveBunny(int direction) { if (direction == 12) { y = y + 1; carrots = carrots - 1; }

13 Bunnies and Interfaces else if (direction == 3) { x = x + 1; carrots = carrots - 1; } else if (direction == 6) { y = y - 1; carrots = carrots - 1; } else if (direction == 9) { x = x - 1; carrots = carrots - 1; } else { System.out.println("Invalid direction"); }

14 Parameter Passing Consider the following program: public class ParamTest1 { public static void main (String[] args) { int number = 4; System.out.println("main: number is " + number); method1(number); System.out.println("main: number is now " + number); } public static void method1(int x) { System.out.println("method1: x is " + x); x = x * x; System.out.println("method1: x is now " + x); }

15 Parameter Passing Consider the following program: public class ParamTest1 { public static void main (String[] args) { int number = 4; System.out.println("main: number is " + number); method1(number); System.out.println("main: number is now " + number); } public static void method1(int x) { System.out.println("method1: x is " + x); x = x * x; System.out.println("method1: x is now " + x); } What's the flow of control?

16 Parameter Passing Consider the following program: public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; System.out.println("main: number is " + number); method1(number); System.out.println("main: number is now " + number); } public static void method1(int x) { System.out.println("method1: x is " + x); x = x * x; System.out.println("method1: x is now " + x); } What's the flow of control?

17 Parameter Passing Consider the following program: public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); method1(number); System.out.println("main: number is now " + number); } public static void method1(int x) { System.out.println("method1: x is " + x); x = x * x; System.out.println("method1: x is now " + x); } What's the flow of control?

18 Parameter Passing Consider the following program: public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); System.out.println("main: number is now " + number); } public static void method1(int x) { System.out.println("method1: x is " + x); x = x * x; System.out.println("method1: x is now " + x); } What's the flow of control?

19 Parameter Passing Consider the following program: public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); System.out.println("main: number is now " + number); } public static void method1(int x) { 4 System.out.println("method1: x is " + x); x = x * x; System.out.println("method1: x is now " + x); } What's the flow of control?

20 Parameter Passing Consider the following program: public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); System.out.println("main: number is now " + number); } public static void method1(int x) { 4 System.out.println("method1: x is " + x); 5 x = x * x; System.out.println("method1: x is now " + x); } What's the flow of control?

21 Parameter Passing Consider the following program: public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); System.out.println("main: number is now " + number); } public static void method1(int x) { 4 System.out.println("method1: x is " + x); 5 x = x * x; 6 System.out.println("method1: x is now " + x); } What's the flow of control?

22 Parameter Passing Consider the following program: public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); 7 System.out.println("main: number is now " + number); } public static void method1(int x) { 4 System.out.println("method1: x is " + x); 5 x = x * x; 6 System.out.println("method1: x is now " + x); } What's the flow of control?

23 Parameter Passing Consider the following program: public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); 7 System.out.println("main: number is now " + number); } public static void method1(int x) { 4 System.out.println("method1: x is " + x); 5 x = x * x; 6 System.out.println("method1: x is now " + x); } What's printed?

24 Parameter Passing Consider the following program: public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); 7 System.out.println("main: number is now " + number); } public static void method1(int x) { 4 System.out.println("method1: x is " + x); 5 x = x * x; 6 System.out.println("method1: x is now " + x); } What's printed? main: number is 4

25 Parameter Passing Consider the following program: public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); 7 System.out.println("main: number is now " + number); } public static void method1(int x) { 4 System.out.println("method1: x is " + x); 5 x = x * x; 6 System.out.println("method1: x is now " + x); } What's printed? main: number is 4 method1: x is 4

26 Parameter Passing Consider the following program: public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); 7 System.out.println("main: number is now " + number); } public static void method1(int x) { 4 System.out.println("method1: x is " + x); 5 x = x * x; 6 System.out.println("method1: x is now " + x); } What's printed? main: number is 4 method1: x is 4 method1: x is now 16

27 Parameter Passing Consider the following program: public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); 7 System.out.println("main: number is now " + number); } public static void method1(int x) { 4 System.out.println("method1: x is " + x); 5 x = x * x; 6 System.out.println("method1: x is now " + x); } What's printed? main: number is 4 method1: x is 4 method1: x is now 16 ?????????????????????

28 Parameter Passing Consider the following program: public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); 7 System.out.println("main: number is now " + number); } public static void method1(int x) { 4 System.out.println("method1: x is " + x); 5 x = x * x; 6 System.out.println("method1: x is now " + x); } What's printed? main: number is 4 method1: x is 4 method1: x is now 16 main: number is now 4

29 Parameter Passing Consider the following program: public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); 7 System.out.println("main: number is now " + number); } public static void method1(int x) { 4 System.out.println("method1: x is " + x); 5 x = x * x; 6 System.out.println("method1: x is now " + x); } Why not 16? main: number is 4 method1: x is 4 method1: x is now 16 main: number is now 4

30 Parameter Passing Consider the following program: public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); 7 System.out.println("main: number is now " + number); } public static void method1(int x) { 4 System.out.println("method1: x is " + x); 5 x = x * x; 6 System.out.println("method1: x is now " + x); } Because when the value in the int variable number is passed to method1,

31 Parameter Passing Consider the following program: public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); 7 System.out.println("main: number is now " + number); } public static void method1(int x) { 4 System.out.println("method1: x is " + x); 5 x = x * x; 6 System.out.println("method1: x is now " + x); } Because when the value in the int variable number is passed to method1, what really happens is that a copy of the value (4) in number is assigned to the parameter x.

32 Parameter Passing Consider the following program: public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); 7 System.out.println("main: number is now " + number); } public static void method1(int x) { 4 System.out.println("method1: x is " + x); 5 x = x * x; 6 System.out.println("method1: x is now " + x); } Because when the value in the int variable number is passed to method1, what really happens is that a copy of the value (4) in number is assigned to the parameter x. It's the value in x that's being modified here -- a copy of the value in number. The original value in number is not affected.

33 Parameter Passing Will this program behave differently? Why or why not? public class ParamTest2 { public static void main (String[] args) { int number = 4; System.out.println("main: number is " + number); method1(number); System.out.println("main: number is now " + number); } public static void method1(int number) { System.out.println("method1: number is " + number); number = number * number; System.out.println("method1: number is now " + number); } What's printed?

34 Parameter Passing Will this program behave differently? Why or why not? public class ParamTest2 { public static void main (String[] args) { int number = 4; System.out.println("main: number is " + number); method1(number); System.out.println("main: number is now " + number); } public static void method1(int number) { System.out.println("method1: number is " + number); number = number * number; System.out.println("method1: number is now " + number); } What's printed? main: number is 4 method1: number is 4 method1: number is now 16 ?????????????????????????

35 Parameter Passing Will this program behave differently? Why or why not? public class ParamTest2 { public static void main (String[] args) { int number = 4; System.out.println("main: number is " + number); method1(number); System.out.println("main: number is now " + number); } public static void method1(int number) { System.out.println("method1: number is " + number); number = number * number; System.out.println("method1: number is now " + number); } What's printed? main: number is 4 method1: number is 4 method1: number is now 16 main: number is now 4

36 Parameter Passing Will this program behave differently? Why or why not? public class ParamTest2 { public static void main (String[] args) { int number = 4; System.out.println("main: number is " + number); method1(number); System.out.println("main: number is now " + number); } public static void method1(int number) { System.out.println("method1: number is " + number); number = number * number; System.out.println("method1: number is now " + number); } Remember that a parameter declared in a method header has local scope, just like a variable declared within that method. As far as Java is concerned, number inside of method1 is unrelated to number outside of method1. They are not the same variable.

37 Parameter Passing Now consider this program. public class Ptest { public static void main(String[] args) { int[] foo = new int[1]; foo[0] = 4; System.out.println("main: foo is now: " + foo[0]); method1(foo); System.out.println("main: foo is now: " + foo[0]); } public static void method1(int[] x) { System.out.println("method1: x is now: " + x[0]); x[0] = x[0] * x[0]; System.out.println("method1: x is now: " + x[0]); } What's printed?

38 Parameter Passing Now consider this program. public class Ptest { public static void main(String[] args) { int[] foo = new int[1]; foo[0] = 4; System.out.println("main: foo is now: " + foo[0]); method1(foo); System.out.println("main: foo is now: " + foo[0]); } public static void method1(int[] x) { System.out.println("method1: x is now: " + x[0]); x[0] = x[0] * x[0]; System.out.println("method1: x is now: " + x[0]); } What's printed? main: foo is now: 4

39 Parameter Passing Now consider this program. public class Ptest { public static void main(String[] args) { int[] foo = new int[1]; foo[0] = 4; System.out.println("main: foo is now: " + foo[0]); method1(foo); System.out.println("main: foo is now: " + foo[0]); } public static void method1(int[] x) { System.out.println("method1: x is now: " + x[0]); x[0] = x[0] * x[0]; System.out.println("method1: x is now: " + x[0]); } What's printed? main: foo is now: 4 method1: x is now: 4

40 Parameter Passing Now consider this program. public class Ptest { public static void main(String[] args) { int[] foo = new int[1]; foo[0] = 4; System.out.println("main: foo is now: " + foo[0]); method1(foo); System.out.println("main: foo is now: " + foo[0]); } public static void method1(int[] x) { System.out.println("method1: x is now: " + x[0]); x[0] = x[0] * x[0]; System.out.println("method1: x is now: " + x[0]); } What's printed? main: foo is now: 4 method1: x is now: 4 method1: x is now: 16

41 Parameter Passing Now consider this program. public class Ptest { public static void main(String[] args) { int[] foo = new int[1]; foo[0] = 4; System.out.println("main: foo is now: " + foo[0]); method1(foo); System.out.println("main: foo is now: " + foo[0]); } public static void method1(int[] x) { System.out.println("method1: x is now: " + x[0]); x[0] = x[0] * x[0]; System.out.println("method1: x is now: " + x[0]); } What's printed? main: foo is now: 4 method1: x is now: 4 method1: x is now: 16 ?????????????????????

42 Parameter Passing Now consider this program. public class Ptest { public static void main(String[] args) { int[] foo = new int[1]; foo[0] = 4; System.out.println("main: foo is now: " + foo[0]); method1(foo); System.out.println("main: foo is now: " + foo[0]); } public static void method1(int[] x) { System.out.println("method1: x is now: " + x[0]); x[0] = x[0] * x[0]; System.out.println("method1: x is now: " + x[0]); } What's printed? main: foo is now: 4 method1: x is now: 4 method1: x is now: 16 main: foo is now: 16

43 Parameter Passing Now consider this program. public class Ptest { public static void main(String[] args) { int[] foo = new int[1]; foo[0] = 4; System.out.println("main: foo is now: " + foo[0]); method1(foo); System.out.println("main: foo is now: " + foo[0]); } public static void method1(int[] x) { System.out.println("method1: x is now: " + x[0]); x[0] = x[0] * x[0]; System.out.println("method1: x is now: " + x[0]); } Why not 4? main: foo is now: 4 method1: x is now: 4 method1: x is now: 16 main: foo is now: 16

44 Parameter Passing Now consider this program. public class Ptest { public static void main(String[] args) { int[] foo = new int[1]; foo[0] = 4; System.out.println("main: foo is now: " + foo[0]); method1(foo); System.out.println("main: foo is now: " + foo[0]); } public static void method1(int[] x) { System.out.println("method1: x is now: " + x[0]); x[0] = x[0] * x[0]; System.out.println("method1: x is now: " + x[0]); } What's in foo? Is it the int[] array object?

45 Parameter Passing Now consider this program. public class Ptest { public static void main(String[] args) { int[] foo = new int[1]; foo[0] = 4; System.out.println("main: foo is now: " + foo[0]); method1(foo); System.out.println("main: foo is now: " + foo[0]); } public static void method1(int[] x) { System.out.println("method1: x is now: " + x[0]); x[0] = x[0] * x[0]; System.out.println("method1: x is now: " + x[0]); } What's in foo? Is it the int[] array object? No, it's the reference, or pointer, to the object.

46 Parameter Passing Now consider this program. public class Ptest { public static void main(String[] args) { int[] foo = new int[1]; foo[0] = 4; System.out.println("main: foo is now: " + foo[0]); method1(foo); System.out.println("main: foo is now: " + foo[0]); } public static void method1(int[] x) { System.out.println("method1: x is now: " + x[0]); x[0] = x[0] * x[0]; System.out.println("method1: x is now: " + x[0]); } What's in foo? Is it the int[] array object? No, it's the reference, or pointer, to the object. A copy of that reference is passed to method1 and assigned to x.

47 Parameter Passing Now consider this program. public class Ptest { public static void main(String[] args) { int[] foo = new int[1]; foo[0] = 4; System.out.println("main: foo is now: " + foo[0]); method1(foo); System.out.println("main: foo is now: " + foo[0]); } public static void method1(int[] x) { System.out.println("method1: x is now: " + x[0]); x[0] = x[0] * x[0]; System.out.println("method1: x is now: " + x[0]); } What's in foo? Is it the int[] array object? No, it's the reference, or pointer, to the object. A copy of that reference is passed to method1 and assigned to x. The reference in foo and the reference in x both point to the same object.

48 Parameter Passing Now consider this program. public class Ptest { public static void main(String[] args) { int[] foo = new int[1]; foo[0] = 4; System.out.println("main: foo is now: " + foo[0]); method1(foo); System.out.println("main: foo is now: " + foo[0]); } public static void method1(int[] x) { System.out.println("method1: x is now: " + x[0]); x[0] = x[0] * x[0]; System.out.println("method1: x is now: " + x[0]); } When the object pointed at by x is updated, it's the same as updating the object pointed at by foo. We changed the object that was pointed at by both x and foo.

49 Parameter Passing n Passing primitive types (int, double, boolean) as parameter in Java n "pass by value" n value in variable is copied n copy is passed to method n modifying copy of value inside called method has no effect on original value outside called method n modifying aka mutating

50 Parameter Passing n Passing object as parameter in Java n "pass by reference" n objects could be huge, so do not pass copies around n pass copy of the object reference n object reference aka pointer n modifying object pointed to by reference inside calling method does affect object pointed to by reference outside calling method n both references point to same object

51 Parameter Passing Pictures main method1 4 object as parameter: copy of pointer made x foo main method1 prim as parameter: copy of value x number 4 4

52 Midterm Q4 from 04W2 public void process( int[][] arrA, int[][] arrB ) { int row; int col; int[][] arrC = { { 1, 1, 1 }, { 1, 1, 1 } }; arrA = arrC; for( row = 0; row < arrB.length; row++ ) { for( col = 0; col < arrB[ row ].length; col++ ) { arrB[ row ][ col ] = row + col; } int[][] dataA = { { 0, 0 }, { 0, 0 } }; int[][] dataB = { { 0, 0 }, { 0, 0 } }; process( dataA, dataB ); dataBdataA

53 Midterm Q4 from 04W2 public void process( int[][] arrA, int[][] arrB ) { int row; int col; int[][] arrC = { { 1, 1, 1 }, { 1, 1, 1 } }; arrA = arrC; for( row = 0; row < arrB.length; row++ ) { for( col = 0; col < arrB[ row ].length; col++ ) { arrB[ row ][ col ] = row + col; } int[][] dataA = { { 0, 0 }, { 0, 0 } }; int[][] dataB = { { 0, 0 }, { 0, 0 } }; process( dataA, dataB ); dataBdataAarrAarrB

54 Midterm Q4 from 04W2 public void process( int[][] arrA, int[][] arrB ) { int row; int col; int[][] arrC = { { 1, 1, 1 }, { 1, 1, 1 } }; arrA = arrC; for( row = 0; row < arrB.length; row++ ) { for( col = 0; col < arrB[ row ].length; col++ ) { arrB[ row ][ col ] = row + col; } int[][] dataA = { { 0, 0 }, { 0, 0 } }; int[][] dataB = { { 0, 0 }, { 0, 0 } }; process( dataA, dataB ); dataBdataAarrAarrB arrC

55 Midterm Q4 from 04W2 public void process( int[][] arrA, int[][] arrB ) { int row; int col; int[][] arrC = { { 1, 1, 1 }, { 1, 1, 1 } }; arrA = arrC; for( row = 0; row < arrB.length; row++ ) { for( col = 0; col < arrB[ row ].length; col++ ) { arrB[ row ][ col ] = row + col; } int[][] dataA = { { 0, 0 }, { 0, 0 } }; int[][] dataB = { { 0, 0 }, { 0, 0 } }; process( dataA, dataB ); dataBdataAarrAarrB arrC

56 Review: Static Fields/Methods n Static fields belong to whole class n nonstatic fields belong to instantiated object n Static methods can only use static fields n nonstatic methods can use either nonstatic or static fields class: Giraffe getGiraffeCount() numGiraffes object: Giraffe1 sayHowTall() neckLength object: Giraffe2 sayHowTall() neckLength

57 Review: Variable Scope n Scope of a variable (or constant) is that part of a program in which value of that variable can be accessed

58 Variable Scope public class CokeMachine4 { private int numberOfCans; public CokeMachine4() { numberOfCans = 2; System.out.println("Adding another machine to your empire"); } public int getNumberOfCans() { return numberOfCans; } public void reloadMachine(int loadedCans) { numberOfCans = loadedCans; } n numberOfCans variable declared inside class but not inside particular method n scope is entire class: can be accessed from anywhere in class

59 Variable Scope public class CokeMachine4 { private int numberOfCans; public CokeMachine4() { numberOfCans = 2; System.out.println("Adding another machine to your empire"); } public double getVolumeOfCoke() { double totalLitres = numberOfCans * 0.355; return totalLitres; } public void reloadMachine(int loadedCans) { numberOfCans = loadedCans; } n totalLitres declared within a method n scope is method: can only be accessed from within method n variable is local data: has local scope

60 Variable Scope public class CokeMachine4 { private int numberOfCans; public CokeMachine4() { numberOfCans = 2; System.out.println("Adding another machine to your empire"); } public int getNumberOfCans() { return numberOfCans; } public void reloadMachine(int loadedCans) { numberOfCans = loadedCans; } n loadedCans is method parameter n scope is method: also local scope n just like variable declared within parameter n accessed only within that method

61 Variable Types n Static variables n declared within class n associated with class, not instance n Instance variables n declared within class n associated with instance n accessible throughout object, lifetime of object n Local variables n declared within method n accessible throughout method, lifetime of method n Parameters n declared in parameter list of method n accessible throughout method, lifetime of method

62 Variable Types class: Giraffe getGiraffeCount() int numGiraffes object: Giraffe1 sayHowTall() int neckLength yell(String message) int volume object: Giraffe2 int neckLength sayHowTall() yell(String message) int volume n Static? Instance? Local? Parameters?

63 Questions?