Methods and Parameters. Slide 2 Internal “Convenience” Methods External Methods ways to respond to requests from external “users” e.g., deposit( int ),

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Looking inside classes Fields, Constructors & Methods Week 3.
Objects & Methods Defining Classes. Slide 2 Reference Variables Revisited Remember: Object variables are references (aka pointers) Point to “null” by.
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
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.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four: Defining Your Own Classes *Instantiable.
Functions:Passing Parameters by Value Programming.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Chapter Four Defining Your Own Classes continued.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
CMP-MX21: Lecture 6 Objects & Methods 1 Steve Hordley.
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.
Local Variables A local variable is a variable that is declared within a method declaration. Local variables are accessible only from the method in which.
Introduction to Object-Oriented Programming
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CSC Programming I Lecture 8 September 9, 2002.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Loops and Iteration for Statements, while Statements and do-while Statements.
Classes CS 21a: Introduction to Computing I First Semester,
CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
C++ Classes and Data Structures Jeffrey S. Childs
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Arrays and Lists: Handling Infinite Data CS 21a: Introduction to Computing I First Semester,
1 Week 8 l Methods l Parameter passing Methods. 2 Using Methods l Methods are actions that an object can perform. l To use a method you invoke or call.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Methods.
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
Arrays and Array Lists CS 21a. Problem 1: Reversing Input Problem: Read in three numbers and then print out the numbers in reverse order Straightforward.
CSH Intro. to Java. The Big Ideas in Computer Science Beyond programming Solving tough problems Creating extensible solutions Teams of “Computational.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
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.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
1 C++ Classes and Data Structures Course link…..
User-Written Functions
Chapter 7: User-Defined Functions II
More Object Oriented Programming
Introduction to Object-Oriented Programming with Java--Wu
Functions Pass By Value Pass by Reference
Arrays and Array Lists CS 21a.
JAVA CLASSES.
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
References Revisted (Ch 5)
ITM 352 Functions.
Previous Lecture: Today’s Lecture: Reading (JV):
C Parameter Passing.
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Methods and Parameters

Slide 2 Internal “Convenience” Methods External Methods ways to respond to requests from external “users” e.g., deposit( int ), withdraw( int ), getBalance() Internal Methods methods called within another method in the same object Puts commonly used code in one place aka “convenience method” Allows use to vary common code only once (so we don’t have to change it in many places) Leads to more flexibility Generally, declare these private (for now) since it’s internal

Slide 3 Example public class BankApplet2 extends IOApplet { BankAccount aliceAccount; BankAccount bobAccount; public void setup() { aliceAccount = new BankAccount(); bobAccount = new BankAccount(); addInput( "Alice\'s Deposit" ); addInput( "Bob\'s Deposit" ); addButton( "Deposit" ); addOutput(); clearOutput(); println( "Alice's balance is P" + aliceAccount.getBalance() + "." ); println( "Bob's balance is P" + bobAccount.getBalance() + "." ); } public void onButtonPressed() { int aliceDeposit = getInt( "Alice\'s Deposit" ); int bobDeposit = getInt( "Bob\'s Deposit" ); aliceAccount.deposit( aliceDeposit ); bobAccount.deposit( bobDeposit ); clearOutput(); println( "Alice's balance is P" + aliceAccount.getBalance() + "." ); println( "Bob's balance is P" + bobAccount.getBalance() + "." ); } Note common code

Slide 4 Example public class BankApplet2 extends IOApplet { BankAccount aliceAccount; BankAccount bobAccount; public void setup() { aliceAccount = new BankAccount(); bobAccount = new BankAccount(); addInput( "Alice\'s Deposit" ); addInput( "Bob\'s Deposit" ); addButton( "Deposit" ); addOutput(); printBalances(); } public void onButtonPressed() { int aliceDeposit = getInt( "Alice\'s Deposit" ); int bobDeposit = getInt( "Bob\'s Deposit" ); aliceAccount.deposit( aliceDeposit ); bobAccount.deposit( bobDeposit ); printBalances(); } private void printBalances() { clearOutput(); println( "Alice's balance is P" + aliceAccount.getBalance() + "." ); println( "Bob's balance is P" + bobAccount.getBalance() + "." ); } Merge into printBalances()

Slide 5 Example public class BankApplet2 extends IOApplet { BankAccount aliceAccount; BankAccount bobAccount; public void setup() { aliceAccount = new BankAccount(“Alice”); bobAccount = new BankAccount(“Bob”); … printBalances(); } … other methods not shown … private void printBalances() { clearOutput(); printBalance( aliceAccount ); printBalance( bobAccount ); } private void printBalance( BankAccount b ) { println( b.getOwner()+“\'s balance is P" + b.getBalance() + "." ); } We can write a “generic” printBalance method that takes a BankAccount and prints its info. (This assumes BankAccount has an owner field, getOwner() method, and the right constructor.)

Slide 6 Example public class BankApplet2 extends IOApplet { BankAccount aliceAccount; BankAccount bobAccount; public void setup() { aliceAccount = new BankAccount(“Alice”); bobAccount = new BankAccount(“Bob”); … printBalances(); } … other methods not shown … private void printBalances() { clearOutput(); printBalance( aliceAccount ); printBalance( bobAccount ); } private void printBalance( BankAccount b ) { println( “The balance in “ + b.getOwner() + “\'s account is P" + b.getBalance() + "." ); } Furthermore, if we want to change the code, we only need to do it in one place!

Slide 7 Passing Parameters Arguments are passed to a method using the pass-by-value scheme Parameters and arguments do not have to have the same name Whether or not they have the same name, parameters are separate copies of the arguments Parameters are local to the method, i.e., they only exist while inside the method. Changes made to the parameters will not affect the value of corresponding arguments * Note: the argument is the variable or value given in the method call, while the parameter is the variable in the method definition that copies that value)

Slide 8 Passing Primitive Types State of Memory public void myMethod( int one, float two ) { one = 25; two = 35.4f; } A A A. A. Local variables do not exist before the method execution At before myMethod A A Code x = 10; y = 20; tester.myMethod( x, y ); xx 10 y 20

Slide 9 State of Memory B. B. The values of arguments are copied to the parameters. Values are copied at B B public void myMethod( int one, float two ) { one = 25; two = 35.4f; } Code x = 10; y = 20; tester.myMethod( x, y ); B B xx 10 y 20 one 10 two f Passing Primitive Types

Slide 10 C C State of Memory C. C. The values of parameters are changed. After is executed C C public void myMethod( int one, float two ) { one = 25; two = 35.4f; } Code x = 10; y = 20; tester.myMethod( x, y ); xx 10 y 20 one 1025 two f Passing Primitive Types

Slide 11 Code D D State of Memory D. D. Parameters are erased. Arguments remain unchanged. At after myMethod D D public void myMethod( int one, float two ) { one = 25; two = 35.4f; } x = 10; y = 20; tester.myMethod( x, y ); xx 10 y 20 Passing Primitive Types

Slide 12 Passing Objects as Parameters Object-type parameters copy the reference to the passed object calls to methods of the parameter does affect the original object’s state sometimes known as “pass-by-reference” but, if we assign parameter variable to another object, then original object is not affected anymore * Note: the argument is the variable or value given in the method call, while the parameter is the variable in the method definition that copies that value)

Slide 13 Passing Objects State of Memory public void myMethod( BankAccount one, BankAccount two ) { one.deposit( 50 ); two.withdraw( 10 ); } A A A. A. Local variables do not exist before the method execution At before myMethod A A in BankApplet BankAccount x = new BankAccount( 100 ); BankAccount y = new BankAccount( 20 ); tester.myMethod( x, y ); xx 10 y BankAccount 100 int balance BankAccount 20 int balance

Slide 14 Passing Objects State of Memory public void myMethod( BankAccount one, BankAccount two ) { one.deposit( 50 ); two.withdraw( 10 ); } B. B. Parameters one and two copy the references to the objects pointed to by x and y At entering myMethod B B in BankApplet BankAccount x = new BankAccount( 100 ); BankAccount y = new BankAccount( 20 ); tester.myMethod( x, y ); xx 10 y BankAccount 100 int balance BankAccount 20 int balance B B one 10 two 10

Slide 15 Passing Objects State of Memory public void myMethod( BankAccount one, BankAccount two ) { one.deposit( 50 ); two.withdraw( 10 ); } C. C. Method calls to one and two affect the same objects referred to by x and y At after running code C C in BankApplet BankAccount x = new BankAccount( 100 ); BankAccount y = new BankAccount( 20 ); tester.myMethod( x, y ); xx 10 y BankAccount 150 int balance BankAccount 10 int balance C C one 10 two 10

Slide 16 Passing Objects State of Memory public void myMethod( BankAccount one, BankAccount two ) { one.deposit( 50 ); two.withdraw( 10 ); } D. D. When myMethod returns, parameters one and two disappear. But objects x and y have been changed. D. D. When myMethod returns, parameters one and two disappear. But objects x and y have been changed. At after running code D D in BankApplet BankAccount x = new BankAccount( 100 ); BankAccount y = new BankAccount( 20 ); tester.myMethod( x, y ); xx 10 y BankAccount 150 int balance BankAccount 10 int balance D D

Slide 17 Reassigning Parameters State of Memory public void myMethod( BankAccount one, BankAccount two ) { one = two; one.deposit( 50 ); two.withdraw( 10 ); } Suppose we reassign one to something else within the method body At entering myMethod B B in BankApplet BankAccount x = new BankAccount( 100 ); BankAccount y = new BankAccount( 20 ); tester.myMethod( x, y ); xx 10 y BankAccount 100 int balance BankAccount 20 int balance B B one 10 two 10

Slide 18 State of Memory public void myMethod( BankAccount one, BankAccount two ) { one = two; one.deposit( 50 ); two.withdraw( 10 ); } C. C. Method calls to one and two affect the same object (same as pointed to by y) x is not affected. C. C. Method calls to one and two affect the same object (same as pointed to by y) x is not affected. At after running code C C in BankApplet BankAccount x = new BankAccount( 100 ); BankAccount y = new BankAccount( 20 ); tester.myMethod( x, y ); xx 10 y BankAccount 100 int balance BankAccount 60 int balance C C one 10 two 10 Reassigning Parameters

Slide 19 Passing Objects State of Memory D. D. When myMethod returns, parameters one and two disappear. Objects y has been changed. D. D. When myMethod returns, parameters one and two disappear. Objects y has been changed. At after running code D D in BankApplet BankAccount x = new BankAccount( 100 ); BankAccount y = new BankAccount( 20 ); tester.myMethod( x, y ); xx 10 y BankAccount 100 int balance BankAccount 60 int balance D D public void myMethod( BankAccount one, BankAccount two ) { one = two; one.deposit( 50 ); two.withdraw( 10 ); }