Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1 Week 8 l Methods l Parameter passing Methods

2 2 Using Methods l Methods are actions that an object can perform. l To use a method you invoke or call it. Example of a method call: speciesOfTheMonth.writeOutput() l Two basic kinds of methods: »methods that return a single value »void methods that do some action other than returning a value calling object— tells which object will do the action method name—tells which action the object will perform parameter list in parentheses—parameters give info to the method, but in this example there are no parameters

3 3 Return Type of Methods l All methods require that the return type be specified l Return types may be: »a primitive data type, such as char, int, double, etc. »a class, such as String, Species, etc. »void if no value is returned You can use a method any place where it is legal to use its return type, for example Integer.parseInt(field.getText().trim()); returns an integer, so this is legal: int next = Integer.parseInt(field.getText().trim());

4 4 void Method Example Assuming instance variables name, population, and growthRate have been defined and assigned values, this method performs an action (writes values to the applet) but does not return a value public void writeOutput() { field1.setText("Name = " + name); field2.setText("Population = " + population); field3.setText("Growth = " + growthRate + "%"); }

5 5 Return Statement Methods that return a value must execute a return statement that includes the value to return l For example: public int getCount() { int count = Integer.parseInt(field.getText().trim()); … return count; }

6 6 Method and Class Naming Conventions Good Programming Practice l Use verbs to name void methods »they perform an action l Use nouns to name methods that return a value »they create (return) a piece of data, a thing l Start class names with a capital letter l Start method names with a lower case letter

7 7 Passing Values to a Method: Parameters l Some methods can be more flexible (therefore useful) if we pass them input values l Input values for methods are called passed values or parameters l Parameters and their data types must be specified inside the parentheses of the heading in the method definition »these are called formal parameters l The calling object must put values of the same data type, in the same order, inside the parentheses of the method invocation »these are called arguments, or actual parameters

8 8 Parameter Passing Example l What is the formal parameter in the method definition? »numberIn l What is the argument in the method invocation? »next //Definition of method to double an integer public int doubleValue(int numberIn) { return 2 * numberIn; } //Invocation of the method...... int next = Integer.parseInt(field.getText().trim())(); field.setText("Twice next = " + doubleValue(next));

9 9 Pass-By-Value: Primitive Data Types as Parameters l When the method is called, the value of each argument is copied (assigned) to its corresponding formal parameter l The number of arguments must be the same as the number of formal parameters l The data types of the arguments must be the same as the formal parameters and in the same order l Formal parameters are initialized to the values passed l Formal parameters are local to their method l Variables used as arguments cannot be changed by the method »the method only gets a copy of the variable's value

10 10 Pass the Address: Class Types as Method Parameters l In the same way, class variable names used as parameters in a method call copy the argument's address (not the value) to the formal parameter l So the formal parameter name also contains the address of the argument l It is as if the formal parameter name is an alias for the argument name Any action taken on the formal parameter is actually taken on the original argument! l Unlike the situation with primitive types, the original argument is not protected for class types!

11 11 Example: Class Type as a Method Parameter The method call makes otherObject an alias for s2, therefore the method acts on s2, the Species object passed to the method! l This is unlike primitive types, where the passed variable cannot be changed. //Method definition with a Species class parameter public void makeEqual(Species otherObject) { otherObject.name = this.name; otherObject.population = this.population; otherObject.growthRate = this.growthRate; } //Method invocation Species s1 = new Species("Crepek", 10, 20); Species s2 = new Species(); s1.makeEqual(s2);


Download ppt "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."

Similar presentations


Ads by Google