Presentation is loading. Please wait.

Presentation is loading. Please wait.

Methods Coding in Java Copyright © 1997 - 2009 Curt Hill.

Similar presentations


Presentation on theme: "Methods Coding in Java Copyright © 1997 - 2009 Curt Hill."— Presentation transcript:

1 Methods Coding in Java Copyright © Curt Hill

2 What is a Method? A chunk of executable code
It always has four things: A return type A name A parameter list A compound statement It may optionally have: Visibility Static or other attributes The main method is an example Copyright © Curt Hill

3 Main method Our main method is an example public static void main (String [] a) {…} public is visibility void means that no value is returned main is the method name (…) contains parameters {…} contains declarations and executables Copyright © Curt Hill

4 The Return Type May be any of the primitives May be any of the objects
int, double, char, boolean May be any of the objects Turtle, World, Color, even arrays May also be void Reserved word indicating no value is returned Copyright © Curt Hill

5 The Name Any Java name The convention is that it starts with a lower case letter Each letter that starts a word is then capitalized Examples: forward moveTo setPenColor This is just a convention, but a good thing for you to follow Copyright © Curt Hill

6 The Parameter List A list of values that needs to be made available to the method The parameter list is enclosed in parentheses The parentheses are always present, even if there are no parameters The parameters are denoted by a type followed by its local name Copyright © Curt Hill

7 Parameters A method may have multiple parameters
Each has same form: type localname These pairs are separated by commas A Turtle method signature: void forward(int steps) { … } Copyright © Curt Hill

8 The Compound Statement
The body of the method is a compound statement Starts with a brace { One or more statements Ends with other brace } Every opening brace { must be matched by a closing brace } The parameter is part of the compound statement for scope considerations Copyright © Curt Hill

9 Method header That part of the method excluding the compound statement is called the method header This is the part usually shown in documentation Such as the doc directory within the bookClasses directory Copyright © Curt Hill

10 Calling a Method Multiple parameters are separated by commas
The general form is: variable.method(parm list) This may be a stand-alone statement yertle.forward(150); It may be part of a statement: x = y + Math.sqrt(3.5); Multiple parameters are separated by commas The names do not have to match Copyright © Curt Hill

11 The end of a method There are two ends to consider The syntax
The execution As far as syntax is concerned a method ends when we find the closing brace of its compound statement In this sense a method is only processed once Copyright © Curt Hill

12 Ending Execution A method ends when one of two things happens:
A return is executed The last line of the method is executed This only works on void methods Return is the mechanism for identifying the value With void functions there is no value to identify Copyright © Curt Hill

13 The return statement Has two effects
Ends the function and returns to calling statement Determines the value that needs to be returned Form is: return expression; Expression should match return type Copyright © Curt Hill

14 Identifying Parameters
The parameter described in the method header is called the formal parameter The parameter used in the call is the actual parameter The names of the two do not need to match Copyright © Curt Hill

15 Formals and Actuals A formal parameter is i void print(int i);
Three actual parameters int j=5,k=2; … System.out.print(j); System.out.print(12); System.out.print(k*-j+1); Formals and actuals should match in type Copyright © Curt Hill

16 Example Method static void triangle( Turtle t){ t.forward(50);
t.turn(120); } Copyright © Curt Hill

17 Called by public static void main( String [] a){
World w = new World(400,240); Turtle yertle = new Turtle(w); yertle.setPenWidth(3); triangle(yertle); Turtle sam = new Turtle(100, 100, w); sam.setPenWidth(4); triangle(sam); } Copyright © Curt Hill

18 Gives Results Copyright © Curt Hill

19 Example commentary The name of the formal and actual parameters did not have to match This allows the method to make any turtle draw, not just one This or any method may be called as many times as desired With the same or different parameters Copyright © Curt Hill

20 Why is method static? static is a Java keyword
For methods it means that the method has no access to class instance data It must have access to local data Since they have no access to class data they may be called before the constructor They may only use their parameters and local data Copyright © Curt Hill

21 Static again The main function is a static function
It may actually instantiate a class of the type to which it belongs A static method may only call other static methods unless it instantiates an instance of its own class Thus triangle needed to be static Copyright © Curt Hill

22 Why use static? Suppose: public class Demo{ public static void main (String s[]){ … } } To use a non-static method we would need: Demo t = new Demo(); Copyright © Curt Hill

23 The Non-static equivalent
We can do this without static methods but it is slightly more work Consider the next three screens Copyright © Curt Hill

24 Notice class name public class TurtleMethod2 { /**
* Non-static methods */ void triangle(Turtle t){ t.forward(100); t.turn(120); } Copyright © Curt Hill

25 New main method public static void main(String[] args) {
TurtleMethod2 item = new TurtleMethod2(); World w = new World(850,500); Turtle yertle = new Turtle(w); Turtle sam = new Turtle(700,450,w); yertle.setPenWidth(3); item.triangle(yertle); sam.setPenWidth(4); item.triangle(sam); } Copyright © Curt Hill

26 Commentary The result is the same What are the differences?
Leave static off of triangle method Create a TurtleMethod2 item using new Prefix triangle with the TurtleMethod2 object, which is named item Copyright © Curt Hill

27 Parameters Again The method needs some refinement
It always draws the same size triangle It becomes more helpful if the length of a side can be changed This is a second parameter Copyright © Curt Hill

28 Second Example static void triangle( Turtle t, int len){
t.forward(len); t.turn(120); } Copyright © Curt Hill

29 Called by public static void main( String [] a){
World w = new World(400,240); Turtle yertle = new Turtle(w); yertle.setPenWidth(3); triangle(yertle,80); Turtle sam = new Turtle(100,100,w); sam.setPenWidth(4); triangle(sam,40); } Copyright © Curt Hill

30 Gives Results Copyright © Curt Hill

31 Method Calls Why does triangle not have an object in front?
What is difference between: yertle.forward(20); triangle(yertle,50); We do not need the object for triangle because the object is the program If we were in Turtle we could just say forward without the prefixed Turtle name Copyright © Curt Hill

32 Method Execution When a method call is encountered what happens?
The actual parameters are evaluated The values are connected to the formal parameters The calling method is suspended and the method is started All local variables are created The method is executed The return ends the method choosing the returned value The caller is activated with the returned value Copyright © Curt Hill

33 Name Overloading In many languages such as C, Pascal, VB a function is defined by one thing only: the function name C++ introduced function name overloading It determines the function to use by examining the signature Java does the same Copyright © Curt Hill

34 Method Signatures A method is uniquely determined by two things: the name the parameter list The contribution of the parameter list does not include the parameter names, only number, type and order These are collectively called the signature Two signatures within a class may not be the same Copyright © Curt Hill

35 Multiple Triangles There is no conflict with having both triangle methods in one program The compiler can tell the difference because one has two parameters and the other one Copyright © Curt Hill

36 Why use methods? Two good reasons
Several others besides Allows one instance of identical or similar code We may now draw a triangle as easily as we could draw a line before With any size, any turtle Allows simplification of main program Five or more statements replaced by one Copyright © Curt Hill

37 Finally There is plenty more to know
This will come in several subsequent presentations We should do a demo Copyright © Curt Hill


Download ppt "Methods Coding in Java Copyright © 1997 - 2009 Curt Hill."

Similar presentations


Ads by Google