Download presentation
Presentation is loading. Please wait.
1
Metode di Java Risanuri Hidayat, Ir., M.Sc.
2
Pendahuluan Classes usually consist of two things: instance variables and methods. The topic of methods is a large one because Java gives them so much power and flexibility.
3
general form This is the general form of a method: type name(parameter-list) { // body of method return value; } type specifies the type of data returned by the method. If the method does not return a value, its return type must be void. The name of the method is specified by name. The parameter-list is a sequence of type and identifier pairs separated by commas. Parameters are essentially variables that receive the value of the arguments passed to the method when it is called. value is the value returned.
4
Example // This program includes a method (Metode.java) class Metode { static void volume(double x, double y, double z) { System.out.println(x*y*z); } public static void main(String args[]) { double vol; double width = 10; double height = 20; double depth = 15; System.out.print("Volume is "); volume(width, height, depth); }
5
Example 2: Returning value // This program includes a method (Metode1.java) class Metode1 { static double volume(double x, double y, double z) { double v; v =x*y*z; return v; } public static void main(String args[]) { double vol; double width = 10; double height = 20; double depth = 15; vol = volume(width, height, depth); System.out.print("Volume is "); System.out.println(vol); }
6
Example 2: Returning value // This program includes a method (Metode2.java) class Metode2 { static double volume(double x, double y, double z) { return x*y*z; } public static void main(String args[]) { double vol; double width = 10; double height = 20; double depth = 15; System.out.print("Volume is "); System.out.println(volume(width, height, depth)); }
7
Method overloading In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different. When this is the case, the methods are said to be overloaded, and the process is referred to as method overloading. When an overloaded method is invoked, Java uses the type and/or number of arguments as its guide to determine which version of the overloaded method to actually call.
8
Example : Overloading // Demonstrate method overloading (Overload01.java). class Overload01 { static void test() { System.out.println("No parameters"); } // Overload test for one integer parameter. static void test(int a) { System.out.println("a: " + a); } // Overload test for two integer parameters. static void test(int a, int b) { System.out.println("a and b: " + a + " " + b); } // overload test for a double parameter
9
Example : Overloading static double test(double a) { System.out.println("double a: " + a); return a*a; } public static void main(String args[]) { double result; // call all versions of test() test(); test(10); test(10, 20); result = test(123.2); System.out.println("Result of test(123.2): " + result); } //main } // class
10
Type Conversion When an overloaded method is called, Java looks for a match between the arguments used to call the method and the method's parameters. However, this match need not always be exact. In some cases Java's automatic type conversions can play a role in overload resolution.
11
Type Conversion // Demonstrate method overloading (Overload01.java). class Overload02 { static void test() { System.out.println("No parameters"); } static void test(int a, int b) { System.out.println("a and b: " + a + " " + b); }
12
Type Conversion static void test(double a) { System.out.println("double a: " + a*a); } public static void main(String args[]) { int x=10; test(); test(10, 20); test(x); test(123.5); }
13
recursion Java supports recursion. Recursion is the process of defining something in terms of itself. recursion is the attribute that allows a method to call itself. A method that calls itself is said to be recursive.
14
Example: Recursion // Demo recursion (Recursion01.java) class Recursion01 { static int fact(int n) { int result; if(n==1) return 1; result = fact(n-1) * n; return result; }
15
Example : Recursion public static void main(String args[]) { System.out.println("Factorial of 3 is " + fact(3)); System.out.println("Factorial of 4 is " + fact(4)); System.out.println("Factorial of 5 is " + fact(5)); } //main } // class
16
PR Kerjakan semua contoh di atas Kerjakan juga untuk C Dikumpulkan minggu depan
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.