Download presentation
Presentation is loading. Please wait.
Published byMarshall Dennis Dennis Modified over 6 years ago
1
Object Oriented Systems Lecture 03 Method
Jaeki Song
2
Method A method is a program module that contains a series of statements that carry out a task Execute method Invoke or call from another method
3
Methods Commonly used to bread a problem down into small manageable pieces. Divide and conquer Instead of writing one long method that contains all of the statements necessary to solve a problem, several small methods which each solve a specific part of the problem can be written
4
Methods Java API provides a rich collection of classes and methods
Build-in method Math class method User-defined method
5
Built-in Methods Methods already written and provided by Java
Organized as a collection of classes (class libraries) To use: import package Package: java.lang Math class Method type: data type of value returned by method
6
Commonly Used Math Class Methods
Description abs (x) Absolute value of x exp (x) Exponential method ex log (x) Natural logarithm of x (base e) max (x, y) Larger value of x and y min (x, y) Smaller value of x and y pow (x, y) X raised to power y (xy) sqrt (x) Square root of x
7
Method A method must include the following:
A declaration (or header) An opening curly bracket A body A closing curly bracket The method declaration contains the following: Optional access modifiers The return type for the method The method name An opening parenthesis An optional list of method argument A closing parenthesis
8
Creating Method Structure
modifier returnValueType methodName (list of parameters) modifiers Return value Type Method header public static int max (int num1, int num2) { ….. } parameters methodName Method body
9
Creating Methods Access modifiers
public private protected package Place entire method within class that will use it Not within any other method
10
Return Type Void Nonvoid (value-returning method)
does not return a value to the calling program Nonvoid (value-returning method) returns a single value to the calling program.
11
Method Name A verb or a verb phrase.
Capitalize the first letter of the second word Never mention the method name inside of the function definition except if the function is recursive.
12
Parameter Listings Are variables sent to the method to perform its designated tasks. Must determine and list the data type of each parameter before listing its name.
13
Method Body Includes the statements that will perform the task
Must be framed with curly braces. Start by defining variables (only the ones outside of the class and not listed within the parameter listing)
14
Calling a Method A method is invoked by a method call
Specifies the method name and provides information (argument) Exception: The main method is automatically called when a program starts Two ways to call a method Based on whether the method returns a value or not Return value int larger = max (3,4); System.out.println( max (3,4)); Return void A call to the method must be a statement System.out.println (“ Welcome”); Use the method name as a statement
15
Creating Methods: Single Argument
Values that are sent into a method are called arguments Local variable Known only within boundaries of method Each time method executes Variable redeclared New memory location large enough to hold type set up and named e.g: int a = 5 displayValue (a) public void displayValue(int num) { System.out.println(num);} The argument 5 is copied into The parameter variable num
16
Creating Methods: Multiple Arguments
Method can require more than one argument List arguments within call to method Separate with commas Call a method Arguments sent to method must match arguments listed in method declaration by: Number Type
17
Example public class ComputeCommission {
public static void main (String args[] ) int value = 23000; double commRate = 0.08; computeComm(value, commRate) } public static void computeComm (int v, double cr) double commission; commission = v * cr;
18
Creating Methods: Return Values
Return type can be any type used in Java Primitive types Class types void Returns nothing Method’s type return statement Causes value to be sent from called method back to calling method
19
Exercise EX 1: Swap EX 2: Car loan System
20
Overloading Methods Create another method with the same name, but different parameters Overloading Rules Every method must have a different number of formal parameters OR If the number of formal parameters is the same, then the data type of the formal parameter (in the order listed), must differ in at least one position Types of parameters determine which method executes EX 3: Overloading the Max method
21
Output Format Pakage from java.text: import java.text.DecimalFormat;
DecimalFormat precisionTwo = new Decimal Format (“0.00”) Pakage from javax.swing Number of columns import javax.swing.JTextArea; JTextArea outputTextArea = new JTextArea (11,20) Number of rows
22
Common Errors Putting semicolon at the end of a method header
Writing modifiers or return types in a method call statement Forgetting to pass arguments to methods that require them Attempting to access a parameter variable with code outside the method where the variable is declared
23
Common Errors Changing the contents of a method’s parameter variable and expecting the argument that was passed into the parameter to change as well Using a variable to receive a method’s return value when the variable’s data type is incompatible with the data type of the return value Not writing a return statement in a non-void method
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.