0% found this document useful (0 votes)
18 views15 pages

METHODS

Uploaded by

Queenzlet Turao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views15 pages

METHODS

Uploaded by

Queenzlet Turao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

METHODS

INSRTRUCTION SPECIALIST: CARL DREDD ROSALES


Modularity
modularity: Writing code in smaller, more manageable components or modules. Then
combining the modules into a cohesive system.

– Modularity with methods. Break complex code into smaller tasks and organize it
using methods.

Methods define the behaviors or functions for objects.

An object’s behavior refers to what the object can do (or what can be done to it). A
method is simply a named group of statements.
– main is an example of a method
Example
Consider the following code which asks the user to enter two numbers
and print out the average.

Scanner console = new Scanner(System.in);


System.out.print("Enter a number: ");
int num1 = console.nextInt();
System.out.print("Enter a number: ");
int num2 = console.nextInt();
System.out.println("The average is " + (num1 + num2)/2.0);

What if we need to do this again?


We don't want to repeat this code by copying and pasting as shown in the next
slide.
Method
One way to organize code and to make it more readable and reusable is to factor
out useful pieces into reusable methods.

A method is a named group of programming instructions that accomplish a


specific task. If we want to perform the task, we simply "call" the method by its
name. A method may be called as many times as we wish to redo the task.

The "30 seconds" button on the microwave is an example of a method. If we press


it(call it by its name), it will run the microwave 30 seconds. Later, if we want to heat
something else, we can press it again to run the microwave another 30 seconds.

In other programming languages, methods are also called procedures or


functions.
Example
If we need to repeat this task, we do not want to simply copy and paste out code:

Scanner console = new Scanner(System.in);


int num1, num2; Let's factor out this piece
System.out.print("Enter a number: "); of code, convert it into a
num1 = console.nextInt(); method by giving it a
System.out.print("Enter a number: "); name!
num2 = console.nextInt();
System.out.println("The average is " + (num1 + num2)/2.0);
Then we can call it
System.out.print("Enter a number: "); repeatedly if we wish to
num1 = console.nextInt(); run the code.
System.out.print("Enter a number: ");
num2 = console.nextInt();
System.out.println("The average is " + (num1 + num2)/2.0)
Syntax
modifier static returnType nameOfMethod (parameter1, parameter2, ...) {

// method body

}
Method
A method is a group of code that has a name and can be called using parentheses.
public class Main{
public static void main(String[] args){ Enter a number: 4
// calling it the first time Enter a number: 6
average(); The average is 5.0
// calling it again to repeat the task
average();
}
public static void average(){ Enter a number: 10
Scanner console = new Scanner(System.in); Enter a number: 11
int num1, num2; The average is 10.5
System.out.print("Enter a number: ");
num1 = console.nextInt();
System.out.print("Enter a number: ");
num2 = console.nextInt();
System.out.println("The average is " + (num1 + num2)/2.0);
}}
Static Method Inside Driver Class
The driver class is the class with the main method. Note that the main method is the begin
point of a run of any program.

public class MyClass{


public static void main(String[] args){
method2();
method1();
}
public static void method1(){
System.out.println(“running method1”);
}
public static void method2(){
System.out.println(“running method2”);
}
}
Static Method Inside Driver Class
The order of the methods in the driver class does not matter and does not affect the
run or output of the program. The program below has the exact same output as the
program from the previous slide. The main method is always the starting point of the
run of any program.
public class MyClass{
public static void method1(){
System.out.println(“running method1”);
}
public static void main(String[] args){
method2();
method1();
}
public static void method2(){
System.out.println(“running method2”);
}
}
Control flow
When a method is called, the program's execution...
"jumps" into that method, executing its statements, then
"jumps" back to the point where the method was called.
What is the output?
public class MethodsExample {
public static void main(String[] args) {
public static void message1() {
message1(); System.out.println("This is message1.");
}

message2();
public static void message2() {
System.out.println("This is message2.");
message1();
Output:
System.out.println("Done with message2.");
} This is message1. }
This is message2.
This is message1. public static void message1() {
... Done with message2. System.out.println("This is message1.");
} }
Method Signature
A method signature for a method consists of the method name and the ordered, possibly empty,
list of parameter types.
public void name(parameters){
statements;
}
Examples:
public static void method1(){
… void: no value is no parameters
} returned when
method ends.
public static void method2(int x, double y){

}
The parameters in the method header are formal parameters.
Static Example
When calling a method with parameters, values provided in the parameter list
need to correspond to the order and type in the method signature.
public class MyProgram{
public static void main(String[] args){
mystery1(3, 4); // error, incompatible types!
mystery1(); // missing actual parameters
mystery1(3); // missing actual parameters
mystery1(3, true); // correct
mystery2(3.2, 3.0); // error, incompatible types!
double a = 2.5;
int b = 5;
mystery2(double a, int b); // error, no type in actual parameters
mystery2(a, b); // correct

}
public static void mystery1(int x, boolean y){

}
public static void mystery2(double x, int z){

}
}
Method Returns
Methods in Java can have return types. Such non-void methods return values
back that can be used by the program. A method can use the keyword “return” to
return a value.

public type methodName(type var1,…, type var2){



}
Examples: Note: Method
public static int method1(){ parameters are
… its inputs and
} return types method returns
are its outputs.
public static double method2(int x){

}
Example
public class Main {
public static void main(String[] args) {
int a = 5;
int b = 10;
int maximum = max(a, b);
System.out.println("Maximum of " + a + " and " + b + "
is: " + maximum);
}

public static int max(int x, int y) {


if (x > y)
return x
else
return y;
}
}
THANK YOU!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy