java method present by showrov ahamed
Post on 21-Jan-2018
56 Views
Preview:
TRANSCRIPT
Welcome To My Presention
• INTRODUCTION
Showrov AhamedEmail:mdshowrovahmed@gmail.comDept. of SWEDaffodil International University
Today’s Topic
• A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name. Think of a method as a subprogram that acts on data and often returns a value.
• Method In Java
4
1
2
3
4
5
6
Table ofContents
• Introduction• About Method
• Example
• Problem
• Method Call
• Parameters
• Usegs Of Class Library
5
public class HugeProgram {
public static void main(String [] args) {
1. Codeline2. Codeline3. Codeline…….10000. Codeline
}
}
Problem
Howare you Going
to manage
this Huge
code???
6
It's possible that your whole program is implemented
inside the main – method
Result: No one will understand the code!
You have to create the code that is
– Reusable
– You and others can manage it
Well how?
Problem
7
How To Divide Your Code
• Best Practics• Use Object Oriented Programming
• Classes, Method, Inheritence
• Lot Of Possiblitics With OOP!
• Concentrate On Methods !• But first let’s concentrate on method !
8
What is Method
- A Java method is a collection of statements that are grouped together to perform an operation. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console.
- Good programmers write in a modular fashion which allows for several programmers to work independently on separate concepts which can be assembled at a later date to create the entire project. The use of methods will be our first step in the direction of modular programming.
9
public static − modifier.
Int…. − return type.
methodName − name of the method.
a, b... − formal parameters.
int a, int b….. − list of parameters.
Creating Method
10
Built-in: Build-in methods are part of the compiler package,
such as System.out.println( ) and System.exit(0).
User-defined: User-defined methods are created by you, the
programmer. These methods take-on names that you assign to
them and perform tasks that you create.
How to call a method (method calling):
When a method is called, a request is made to perform some action, such as setting a value, printing
statements, returning an answer, etc. The code to call the method contains the name of the method to be
executed and any needed data that the receiving method requires. The required data for a method are
specified in the method's parameter list.
There are two basic types of methods:
You are using methods when you use
System.out.print( ) and System.out.println( ).
11
The method name is "readInt" which is defined in the class "Console". Since the method is
defined in the class Console, the word Console becomes the calling object. This particular
method returns an integer value which is assigned to an integer variable named number.
You call a method by writing down the calling object followed by a dot, then the name of the
method, and finally a set of parentheses that may (or may not) have information for the method.
When a method is called, a request is made to perform some action,
such as setting a value, printing statements, returning an answer,
etc. The code to call the method contains the name of the method to be
executed and any needed data that the receiving method requires. The
required data for a method are specified in the method's parameter list.
How to call a method (method calling):
12
public class HugeProgram {
public static void main(String [] args) {
doSomething1();
doSomething2();
}
Public static void doSomething1(){
System.out.println(“Hello”);
}
Public static void doSomething2(){
System.out.println(“Java”);
}
}
Methods: Divide Your Code?
Hello Java
13
Real Life Example:
I really want to buy gift for my
children. The doll costs 5 euros
and the toy car costs 6 euros. I
have 12 euros, is that enough?
Why didn't I listen to the math
teacher at school? Why didn't I
learn sum – calculations?
Why oh why?
14
Real Life Example:
Hello Hello! My name is Mr.Sum and I can sum everything!
Please use my knowledge!
7
Real Life Example:
Well Mr Sum. I have a problem Idon't know what is 5 plus 6.
16
Don't understand your problem. I only do simple calculations,
I really need to have two integer values to be able to do the sum.
Please just give my two numbers!
17
Real Life Example:
Parameters!
56
18
In Java, parameters sent to methods are passed-by-
value: Definition clarification: What is passed "to" a
method is referred to as an "argument". The "type" of
data that a method can receive is referred to as a
"parameter".
Return!
11
19
A method is just a chunk of code that does a particular
job. But methods are set out in a certain way. You have a
method header, and a method body. The header is where
you tell Java what value type, if any, the method
will return (an int value, a double value, a string value,
etc).
Well thanks,
at least I know my numbers and 11 is less than
12,
so great! My children will be very happy!
9
Real Life Example:
Mr. Sum?
Parameter Return
11
5
6
21
LET'S START CODING
The usage of Class Library
Import java.lang.Math;
Public class Example{
Public static void main(String[]args)
Int maxNumber=Math.max(5,6);
System.out.println(maxNumber);
}
}
Import the class
class
Method
parameter
Mr. Max In Code
22
Parameters
public class Example {
public static void main(String [] args) { MyMath.max(5, 10);
}
}
class MyMath {
public static void max(int a, int b) { if(a > b)
System.out.println(a);
else
System.out.println(b);
}
}
23
Returnpublic class Example {
public static void main(String [] args) { int
maxNumber = MyMath.max(5, 10); System.out.println(maxNumber);
}
}
class MyMath {
public static int max(int a, int b) { if(a
> b)
return a;
else
return b;
}
}
24
Thank You
Source: Google.com
Slideshare.com
• Any Questions?
top related