Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


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

1 Methods Awesomeness!!!

2 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 in programming: Methods have a number of important uses in programming: –They enable you to break up a class into “ modules ” which each perform a distinct task –They enable code for a repetitive task to be written once, given a name and used many times –In an “object oriented” language like Java they provide the means of using the information that is stored in an object & enable you to use the features of another class –They localise errors which helps with debugging

3 Writing your own methods Method definitions have four basic parts Method definitions have four basic parts –The name of the method –The type of primitive or object the method returns (if it returns anything) –A list of parameters (things required for the processing of the body of the method) –The body of the method

4 Writing your own methods public int square(int x) { int result; int result; result = x*x; result = x*x; return result; return result;} What the method returns Method name Parameters required to process the method body The value that is actually returned (has to be same type as what is declared)

5 Method Call To actually call/ use the method you have defined, you need to call it from your code. Eg. From inside the method ‘ paint ’ To actually call/ use the method you have defined, you need to call it from your code. Eg. From inside the method ‘ paint ’ Public void paint(Graphics g) { int methodResult; methodResult = square(4); g.drawString(“”+methodResult, 20, 20); }

6 No input and/or no output A method that has no input A method that has no input –has empty brackets –even though the brackets aren’t used, they indicate that it is a method A method that has no output A method that has no output –returns “void”

7 Example: stars() Public class stars1 extends Applet { String output=“”; public void stars() { for(int i=0; i<=10; i++) { output=output+”*”; }} public void paint(Graphics g) { stars(); g.drawString(“”+output, 20, 20); }} Output: **********

8 Formal and actual parameters To change the program to accept input we need to add… To change the program to accept input we need to add… –A formal parameter to the method definition, and –An actual parameter to the method call. For example, let’s modify the stars method to display any number of stars. For example, let’s modify the stars method to display any number of stars.

9 Public class stars1 extends Applet { String output=“”; public void stars(int x) { for(int i=0; i<=x; i++) { output=output+”*”; output=output+”*”;}} public void paint(Graphics g) { stars(5); g.drawString(“”+output, 20, 20); }} Output: ***** To call the method, the method is followed by the brackets containing the actual parameter. Instead of looping a set number of times, the loop depends on the number specified. To be able to vary the number of stars displayed, we’ll take an integer as input, this is known as a formal parameter. Note also that the variable x is a local variable, and only accessible within the stars method.

10 Methods – multiple inputs A method can have multiple inputs (formal parameters). A method can have multiple inputs (formal parameters). For example, let’s modify the stars() method so that in addition to being able to change the number of * ’ s added to the string, you can also change the character to whatever you like. For example, let’s modify the stars() method so that in addition to being able to change the number of * ’ s added to the string, you can also change the character to whatever you like.

11 Public class stars1 extends Applet { String output=“”; public void stars(int x, char char1) { for(int i=0; i<=x; i++) {output=output+char1; }} public void paint(Graphics g) {stars(6,‘%’); g.drawString(“”+output, 20, 20); }} Output: %%% The formal parameters‘int x’ and ‘char char1’means it takes an integer and a char as input. Instead of appending (adding) stars to the output String, we will append the character provided. To call the method, we have to provide a number and a character.

12 Methods – input and output The previous program is not very good. Methods should be self- contained so that one method does not interfere with another. The previous program is not very good. Methods should be self- contained so that one method does not interfere with another. Instead of modifying the global variable ‘ output ’ directly we should instead return a String. Instead of modifying the global variable ‘ output ’ directly we should instead return a String.

13 Public class stars1 extends Applet { String output= “” ; public String stars(int x, char char1) { String charString; for(int i=0; i<=x; i++) { charString = charString +char1; charString = charString +char1;} return charString; } public void paint(Graphics g) { output = stars(6, ‘%’); g.drawString( “” +output, 20, 20); }} Output: %%% This method returns a String. charString is a local variable, i.e. only accessible within this method. This is where we return the output from the method. We can set the output equal to the value returned by the method.

14 Parameter Passing Parameters are passed using Call-By- Value. Parameters are passed using Call-By- Value. A copy of the actual parameter is passed to the formal parameter, without changing the original actual parameter. A copy of the actual parameter is passed to the formal parameter, without changing the original actual parameter.

15 public int square(int x) { x = x*x; return x; } public void paint (Graphics g) { int four = 4; int result; result = square(four); g.drawString(four + “squared is ” + result, 20, 20); } The actual parameter, four, equals 4 The actual parameter, four, equals 4 When the square() method is called, the value 4 is copied and given to the formal parameter x. When the square() method is called, the value 4 is copied and given to the formal parameter x. Whatever happens in the square() method, when the method is finished, the actual parameter, four, remains 4. Whatever happens in the square() method, when the method is finished, the actual parameter, four, remains 4.

16 Parameter Passing Primitives Primitives –As in the previous example, in the case of a primitive type, since the variable contains a value, it is the value that is copied to the formal parameter, and it is not copied back.

17 Parameter Passing Objects Objects –In the case of an object, the variable contains a pointer to the instantiated object, and it is the pointer that is copied to the formal parameter. –This means that the actual and formal parameters are both pointing to the same object. –If we change the object in the method, it will of course still have that value after exiting the method.

18 Parameter Passing Strings Strings –Strings work similarly to other objects, except that each time you modify a String a new String object is created. –The formal parameter does not point to the original object, so changes made in the method will not change the original.

19 Method Overloading We know that when we write a method and we provide it with formal parameters, a call to that method requires the use of actual parameters of the same type and in the same order as the method definition. We know that when we write a method and we provide it with formal parameters, a call to that method requires the use of actual parameters of the same type and in the same order as the method definition. Eg. Eg. –Method definition: public int myMethod(int x) –Method call: int result = myMethod(5);

20 Method Overloading Sometimes we want to be able to do similar things to different types or objects. Sometimes we want to be able to do similar things to different types or objects. Java allows us to write methods that are different, but have the same name – as long as they have different parameters. Java allows us to write methods that are different, but have the same name – as long as they have different parameters.

21 public void paint (Graphics g) { g.drawString(“”+multiplyMethod(6), 10, 20); g.drawString(“”+multiplyMethod(4,6), 10, 40); } public int multiplyMethod (int x) { return x*x; } public int multiplyMethod (int x, int y) { return x*y; } When a method is called it will see what parameters have been provided and will match it to the method with the matching definition.


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

Similar presentations


Ads by Google