Chapter 4 Procedural Methods
Learning Java through Alice © Daly and Wrigley Objectives Identify classes, objects, and methods. Identify the difference between a procedure method and a function method. Differentiate between parameters and arguments. Write procedure methods with parameters. Explain the purpose of the Java API. 2
Learning Java through Alice © Daly and Wrigley Procedure Methods A procedural method is a coordinated sequence of instructions that will be carried out when requested. 3
Learning Java through Alice © Daly and Wrigley Java Programming Interface (API) 4
Learning Java through Alice © Daly and Wrigley Method Syntax The syntax for a method is: modifier static returntype methodname ( list of parameters ) {.... statements to do inside of method } Modifiers determine the scope of a method. ▫public means that the method is available in the current class (program) and other classes. ▫private means that you intend to use the variable or method in the current program. Return Type ▫Data type: int, double, float, etc. ▫No return type: void 5
Learning Java through Alice © Daly and Wrigley Naming Methods Consists of letters, digits, underscores (_) or $ (no spaces allowed). First character can't be a digit. Cannot be keywords. Stylistic Rule: all method names begin with a lowercase letter with all other words beginning with uppercase letter such as displayIt, sumNumbers, etc. Method names should be meaningful. 6
Learning Java through Alice © Daly and Wrigley Methods with No Parameters 7
Learning Java through Alice © Daly and Wrigley Methods with No Parameters 8
Learning Java through Alice © Daly and Wrigley Methods with Some Parameters 9
Learning Java through Alice © Daly and Wrigley Methods with Some Parameters 10
Learning Java through Alice © Daly and Wrigley Object Methods 11
Learning Java through Alice © Daly and Wrigley Static Methods 12