0% found this document useful (0 votes)
35 views39 pages

4.5.3 (B)

Uploaded by

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

4.5.3 (B)

Uploaded by

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

4.

0 Java Language

4.5 Method
4.5.3 Static User-defined Method

Learning Outcomes :

At the end of this topic, you should be able to:


(b) Explain the general structure of a static user-defined
method :method header, method body
(1 Hour)
The general structure of a static user-defined method
Consists of :
modifier

return type
method header
method name

parameter

method body
The general structure of a static user-defined method
Consists of : In Java, how does it look like ? (syntax)
modifier

return type

method name

parameter

method body
The general structure of a static user-defined method
Consists of : In Java, how does it look like ? (syntax)
method header
modifier

return type modifier return type method name ( parameter )


{
method name
method body

parameter }
How to remember???..
method body
* mo re ty me nam (par) Mu ReTi MeNamPar..

The general structure of a static user-defined method
Consists of : In Java, how does it look like ? (syntax)
method header
modifier

return type modifier return type method name ( parameter )


{
method name
method body

parameter }

method body From previous example :


The general structure of a static user-defined method
In Java, how does it look like ? (syntax)
method header

modifier return type method name ( parameter )


{
method body

From previous example :


1.
static void multiply(int a, int b){
System.out.print ( a * b );
}
The general structure of a static user-defined method
In Java, how does it look like ? (syntax)
method header modifier = static

modifier return type method name ( parameter )


{
method body

From previous example :


1.
static void multiply(int a, int b){
System.out.print ( a * b );
}
The general structure of a static user-defined method
In Java, how does it look like ? (syntax)
method header modifier = static

return type = void

modifier return type method name ( parameter )


{
method body

From previous example :


1.
static void multiply(int a, int b){
System.out.print ( a * b );
}
The general structure of a static user-defined method
In Java, how does it look like ? (syntax)
method header modifier = static

return type = void

modifier return type method name ( parameter ) method name = multiply

{
method body

From previous example :


1.
static void multiply(int a, int b){
System.out.print ( a * b );
}
The general structure of a static user-defined method
In Java, how does it look like ? (syntax)
method header modifier = static

return type = void

modifier return type method name ( parameter ) method name = multiply

{ parameter = int a, int b

method body

From previous example :


1.
static void multiply(int a, int b){
System.out.print ( a * b );
}
The general structure of a static user-defined method
In Java, how does it look like ? (syntax)
method header modifier = static

return type = void

modifier return type method name ( parameter ) method name = multiply

{ parameter = int a, int b

method body method body = System.out.print


(a*b);
}

From previous example :


1.
static void multiply(int a, int b){
System.out.print ( a * b );
}
The general structure of a static user-defined method
In Java, how does it look like ? (syntax)
method header

modifier return type method name ( parameter )


{
method body

From previous example : Or other example :


1.
static void multiply(int a, int b){
System.out.print ( a * b );
}
The general structure of a static user-defined method
In Java, how does it look like ? (syntax)
method header

modifier return type method name ( parameter )


{
method body

Or other example :
From previous example :
2.
1. static String displayText( ){
static void multiply(int a, int b){
System.out.print ( a * b ); return “Message is printed”;
} }
The general structure of a static user-defined method
In Java, how does it look like ? (syntax)
method header

modifier return type method name ( parameter )


{
method body

Or other example :
From previous example :
2.
1. static String displayText( ){
static void multiply(int a, int b){
System.out.print ( a * b ); return “Message is printed”;
} }
The general structure of a static user-defined method
In Java, how does it look like ? (syntax)
method header

modifier return type method name ( parameter )


{
method body
Can you spot any
} difference between these
two method examples ?

From previous example : Or other example :


2.
1. static String displayText( ){
static void multiply(int a, int b){
System.out.print ( a * b ); return “Message is printed”;
} }
The general structure of a static user-defined method
In Java, how does it look like ? (syntax)
method header

modifier return type method name ( parameter )


{
method body

From previous example : Or other example :


2.
1. static String displayText( ){
static void multiply(int a, int b){
System.out.print ( a * b ); return “Message is printed”;
} }
The general structure of a static user-defined method
1. 2.
static void multiply(int a, int b){ static String displayText( ){
System.out.print ( a * b );
} return “Message is printed”;
}

could have 4 cases :


return
parameter explanation
value

1 x x no return value, does not accept parameters


2 x / no return value, accept parameters
3 / x return value, does not accept parameters
4 / / return value, accept parameters
Explain method return types based on return values

1.
static void adam ( ){
adam method body could have 4 cases :
}
return value parameter
1 x x
Explain method return types based on return values

1.
static void adam ( ){
adam method body could have 4 cases :
}
return value parameter
1 x x
2 x /

2.
static void adam ( int a ){
adam method body
}
Explain method return types based on return values

1. 3.
static void adam ( ){ static int mark ( ){
// adam method body could have 4 cases : // method body
} return…;
return value parameter }
1 x x
2 x /
3 / x
2.
static void adam ( int a ){
// adam method body
}
Explain method return types based on return values

1. 3.
static void adam ( ){ static int mark ( ){
// adam method body could have 4 cases : // method body
} return…;
return value parameter }
1 x x
2 x /
3 / x
4.
2. 4 / / static int mark ( int a , int b){
static void adam ( int a ){ // method body
// adam method body return…;
} }
Explain method return types based on return values
1.
static void adam ( ){
// body of adam method
//must have System.out.print
could have 4 cases :
}
Return
Return type Parameter
value
1 x x
void
2 x /

2.
static void adam ( int a ){
// body of adam method
//must have System.out.print
}
Explain method return types based on return values 3.
1. static int mark ( ){
static void adam ( ){ // body of method
// body of adam method // must have return
//must have System.out.print }
could have 4 cases :
} Return
Return type Parameter
value
1 x x
void
2 x /
3 int, double, boolen, float, / x
4 char, String / /
2. 4.
static void adam ( int a ){ static int mark ( int a , int b){
// body of adam method // body of method
//must have System.out.print // must have return
} }
Let’s open up our mind a little bit
bigger ….
And take a look at a complete source code. (Please copy this source code down)
Main Method

Method 1

Method 2

Method 3

Method 4
Still remember this? :P
return
parameter explanation
value

1 x x no return value, does not accept parameters


2 x / no return value, accept parameters
3 / x return value, does not accept parameters
4 / / return value, accept parameters
Case 1 : without return value without parameter
Components Java Code
returnType
methodName
parameters
Method body
Case 1 : without return value without parameter

Components Java Code


returnType void
methodName bookRoom
parameters No parameter
Method body
System.out.println("You can book room 223");
Case 2 : without return value with parameter
Components Java Code
returnType
methodName
parameters
Method body
Case 2 : without return value with parameter

Components Java Code

returnType void

methodName borrowBank

parameters int dateBorrow

Method body
int dueDate = dateBorrow + 7;

if (dueDate >= 30)


dueDate = dateBorrow - 30;

System.out.println("The due date to return the book is " + dueDate);


Case 3 : with return value without parameter
Components Java Code
returnType
methodName
parameters
Method body
Case 3 : with return value without parameter
Components Java Code
returnType boolean
methodName replaceCard
parameters No parameters
Method body
return true;
Case 4 : with return value with parameter
Components Java Code
returnType
methodName
parameters
Method body
Case 4 : with return value with parameter
Components Java Code
returnType double
methodName fineLateBorrow
parameters int daysLate
Method body
double fine;

if(daysLate < 3)
fine = daysLate * 0.50;
else
fine = 1.00 + ((daysLate - 2) * 0.20);

return fine;
EXERCISE
Still remember this? :P
return
parameter explanation
value

1 x x no return value, does not accept parameters


2 x / no return value, accept parameters
3 / x return value, does not accept parameters
4 / / return value, accept parameters
Method definition
1. Write the first line of a method named myMethod that takes three
parameters; an int and two Strings.
2. Write a method called square that takes an integer and returns an
approximation of the square of the parameter.
3. Write the method named isNotDivisible that takes two
integers, p and q, and that return true if p is not divisible by q, and
false otherwise.
Method definition.
4. Define a method named highestMark() that accepts two
integer values named mark and highest. This method will return the
highest mark if the mark is greater than the highest mark.
5. Write a method names calcAverage() that accepts two integer
values named sum and numPlayer. This method will calculate and
return the average mark.
6. Define a method named printResult() that takes two values
named highest and average. Lastly, display both of them.

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