Metode di Java Risanuri Hidayat, Ir., M.Sc.. Pendahuluan Classes usually consist of two things: instance variables and methods. The topic of methods is.

Slides:



Advertisements
Similar presentations
1 CLASSES. 2 Class Fundamentals It defines a new data type Once defined, this new type can be used to create objects of that type A class is a template.
Advertisements

Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Fungsi Risanuri Hidayat, Ir., M.Sc.. Functions C usually consist of two things: instance variables and functions. All C programs consist of one or more.
Lecture 2 Classes and objects, Constructors, Arrays and vectors.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Recursion!. Can a method call another method? YES.
Introduction to Methods
Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
A Closer Look at Methods and Classes. Overloading Methods In Java it is possible to define two or more methods within the same class that share the same.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Overloading There is another aspect to polymorphism: Overloading Overloading is not overriding. In Turkish: Overridding: eskisini (geçersiz kılmak) Overloading:
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
Cleaning up! Miscellaneous things of some import.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
What is an Object? Real world objects are things that have: 1) state 2) behavior Example: your dog: 1) state – name, color, breed, sits?, barks?, wages.
Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods in a class can have the same name provided – they take.
Programming with methods and classes. Methods  Instance method Operates on a object (i.e., and instance of the class) String s = new String("Help every.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
CSC 143F 1 CSC 143 Constructors Revisited. CSC 143F 2 Constructors In C++, the constructor is a special function automatically called when a class instance.
Catie Welsh February 23,  Lab 4 due on Friday  Lab 5 will be assigned on Friday 2.
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
L EC. 06: C LASS D ETAILS (2/2) S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length.
CSI 3125, Preliminaries, page 1 Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name,
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
Overloading Methods 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.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
1 CLASSES. 2 Class Fundamentals It defines a new data type Once defined, this new type can be used to create objects of that type A class is a template.
Introduction To Java Programming 1.0 Basic Concepts of Java Programming 2.0 Classes, Polymorphism and Inheritance 3.0 Exception handling 4.0.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Classes - Intermediate
Methods.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
05 Method Calling. 2 What is a Method? Declaring a Method Method Calling Method Call Stack Parameter Passing Pass by Value Outline.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Methods and Classes. Method Overloading Two or more methods within the same class that share the same name but different parameters class OverloadDemo.
Chapter 7: Programming with methods and classes..
OOP Features Object Oriented Programming Main issues in software engineering – –maintainability, reusability, portability, security, integrity, user.
Functions.
Review Session.
Constructor Overloading
Chapter 4 (part 2).
OBJECT ORIENTED PROGRAMMING
Method Overloading in JAVA
Classes Lecture 7 from Chapter /1/11.
Parameters and Overloading
Recursive GCD Demo public class Euclid {
CS2011 Introduction to Programming I Methods (II)
class PrintOnetoTen { public static void main(String args[]) {
JAVA 22 February 2019 DEPARTMENT OF CSE.
Method of Classes Chapter 7, page 155 Lecture /4/6.
Classes, Objects and Methods
Unit-1 Introduction to Java
Corresponds with Chapter 5
class Box { double width; double height; double depth; }
Presentation transcript:

Metode di Java Risanuri Hidayat, Ir., M.Sc.

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.

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.

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); }

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); }

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)); }

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.

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

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

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.

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); }

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); }

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.

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; }

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

PR Kerjakan semua contoh di atas Kerjakan juga untuk C Dikumpulkan minggu depan