Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Methods and Parameters. Slide 2 Internal “Convenience” Methods External Methods ways to respond to requests from external “users” e.g., deposit( int ),"— Presentation transcript:

1 Methods and Parameters

2 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

3 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

4 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()

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

6 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!

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

8 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

9 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 1020.0f Passing Primitive Types

10 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 1035.4f Passing Primitive Types

11 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

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

13 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

14 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

15 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

16 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

17 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

18 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

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


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

Similar presentations


Ads by Google