Presentation is loading. Please wait.

Presentation is loading. Please wait.

Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)

Similar presentations


Presentation on theme: "Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)"— Presentation transcript:

1 Methods Matthew Harrison

2 Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4) Body of the Method ● 5) Return Type

3 Method Name ● The first letter of a method name must begin with a lower-case letter ● Can be public or private ● Can be static or non-static ● Can return values ● Ex) public static void main(String args) or ● public static int calculateArea(int x, int y)

4 Types of Methods ● If a method does not return a value, it is void ● public static void calculateArea(int x, int y) ● If a method has a return type, it is not void ● public static int calculateArea(int x, int y) ● A method can have many return types, such as int, double, String, etc.

5 Return Statements ● When a method is not void, it must return a value at the end of the method syntax. ● Public static int calculateArea(int x, int y) ● { int area = x * y; return(area); ● }

6 Method Parameters ● Following along with the previous code... ● Public static int calculateArea(int x, int y) ● { int area = x * y; return(area); ● } ● The parameters for this method are located inside the parenthesis of the method name (int x, int y)

7 Method Parameters ● These parameter values are input values found somewhere else in the program and they are required for the method to work correctly ● System.out.println(“Enter the length of the box: “); ● x = keyboard.nextInt(); ● System.out.println(“Enter the width of the box: “); ● y = keyboard.nextInt(); ● Public static int calculateArea(int x, int y) ● { int area = x * y; return(area); ● }

8 Calling Methods ● To call a method, type the method name as well as any required parameters ● calculateArea(); //void method with no parameters ● calculateArea(int x, int y); //void method with params ● int area = calculateArea(); ● //method with return type int...no params ● int area = calculateArea(int x, int y); ● //method with return type int... also has params


Download ppt "Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)"

Similar presentations


Ads by Google