1 / 24

Method, Contruktor

Method, Contruktor. Oleh: Sri Herawati. Method. method adalah bagian-bagian kode yang dapat dipanggil oleh program utama atau dari method lainnya untuk menjalankan fungsi yang spesifik. Deklarasi Method : <modifier> <returnType> <name>(<parameter>*) { <statement>* }. Method.

hallam
Download Presentation

Method, Contruktor

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Method, Contruktor Oleh: Sri Herawati

  2. Method • method adalah bagian-bagian kode yang dapat dipanggil oleh program utama atau dari method lainnya untuk menjalankan fungsi yang spesifik. • Deklarasi Method : <modifier> <returnType> <name>(<parameter>*) { <statement>* }

  3. Method • Pemberian variabel pada method - Pass-by-Value - Pass-by-Reference

  4. Method – Pass-by-Value • Berlaku pada tipe data primitif (byte,short,int,long,float,double, dan char). • Hanya bisa memodifikasi nilai di dalam fungsi, tidak bisa diluar fungsi.

  5. Contoh : • public class Tukar1 { • public static void menukar(int a) { • int c; • c =5; • } • public static void main(String[] arg) { • int a; • a=4; • System.out.println("Nilai x = " +a); • menukar(a); • System.out.println("Nilai x = "+a); • } • }

  6. Method– Pass-by-Reference • Berlaku pada tipe data array dan objek. • Bisa memodifikasi nilai baik di dalam maupun di luar fungsi.

  7. Contoh : • public class Tukar2 { • int a; • public static void menukar(Tukar2 t) { • int c; • c=t.a; • t.a = 5; • } • public static void main(String[] arg) { • int a =4; • System.out.println("Nilai a "+ a); • Tukar2 tkr=new Tukar2(); • menukar(tkr); • System.out.println("Nilai a = "+tkr.a); • } • }

  8. Array • Array adalah sebuah variabel/sebuah lokasi tertentu yang memiliki satu nama sebagai identifier, namun identifier ini dapat menyimpan lebih dari sebuah nilai. • Deklarasi Array int []ages; atau sepasang tanda kurung [] sesudah nama identifier. int ages[];

  9. Penulisan Array • int []ages = new int[100]; atau • String nama[]={"Andi","Ani"}; • boolean cek[]={true,false};

  10. Contoh • public class CobaArray { • public static void main(String[] args){ • int ages[] = {10, 20, 30}; • for (int i=0; i<ages.length; i++){ • System.out.println(ages[i]); • } • test(ages); • for (int i=0; i<ages.length; i++){ • System.out.println(ages[i]); • } • } • public static void test(int arr[]){ • for (int i=0; i<arr.length; i++) • arr[i] = i + 100; • } • }

  11. Array multidimensi • Array Multidimensi adalah array yang terletak di dlm array. • Contoh : • Elemen 512 x 128 dari integer array int[][] twoD = new int[512][128]; • Karakter array 8 x 16 x 24 char[][][] threeD = new char[8][16][24]; • String array 4 baris x 2 kolom String[][] dogs = {{ "terry", "brown" }, { "Kristin","white" },{ "toby", "gray"},{ "fido", "black"}};

  12. Contoh : • public class ArrayMulti { • public static void main(String[] arg) { • String [][]mhs={{"123","Budi Susanto","Jakarta"},{"124","Geni Handayani","Surabaya"}}; • // get value of elements • for(int i=0;i<2;i++){ • for(int j=0;j<3;j++){ • System.out.println(mhs[i][j]); • } • System.out.println(); • } • } • }

  13. Method • Accessor • Mutator

  14. Accessor Method • Accessor Method digunakan untuk membaca nilai variabel pada class, • Penulisannya : get<namaInstanceVariable> • Method ini juga mempunyai sebuah return value. • Accessor ini digunakan untuk membaca atribut class yg mempunyai hak akses private

  15. Contoh : public class StudentRecord { private String name; : : public String getName(){ return name; } }

  16. Mutator Method • Mutator method merupakan method yang dapat memberi atau mengubah nilai variable dalam class • Penulisannya set<namaInstanceVariabel>. • Tidak mempunyai return value

  17. Contoh : public class StudentRecord { private String name; : : public void setName( String temp ){ name = temp; } }

  18. Contoh– Accessor, mutator class StudentRecord { private String name; public String getName(){ return name; } public void setName( String temp ){ name = temp; } } public class AssMut { public static void main( String[] args ){ StudentRecord haniRecord = new StudentRecord(); haniRecord.setName("Hani"); Menampilkan nama siswa “hani” System.out.println( haniRecord.getName() ); haniRecord.setName("Guntur"); System.out.println( haniRecord.getName() ); } }

  19. Reference this • this digunakan untuk mengakses instance variable yang dibiaskan oleh parameter. • Contoh : public void setName( String name){ name = name; } • Yang benar ... public void setName( String name){ this.name = name; } Salah

  20. Overloading • Overloading mengijinkan sebuah method dengan nama yang sama namun memiliki parameter yang berbeda sehingga mempunyai implementasi dan return value yang berbeda pula.

  21. Contoh : public void print( String temp ){ System.out.println("Name:" + name); System.out.println("Address:" + address); } public void print(double eGrade, double mGrade, double sGrade){ System.out.println("Name:" + name); System.out.println("Math Grade:" + mGrade); System.out.println("English Grade:" + eGrade); }

  22. Construktor • Constructor adalah method dimana seluruh inisialisasi object ditempatkan. • Karakteristik : - Constructor memiliki nama yang sama dengan class - Constructor tidak memiliki return value - Constructor tidak dapat dipanggil secara langsung, namun harus dipanggil dengan menggunakan operator new pada pembentukan sebuah class.

  23. Contoh : • public StudentRecord(){ //area inisialisasi kode; } • public StudentRecord(String temp){ this.name = temp; } • public StudentRecord(String name, String address){ this.name = name; this.address = address; } • public StudentRecord(double mGrade, double eGrade, double sGrade){ mathGrade = mGrade; englishGrade = eGrade; cienceGrade = sGrade; }

  24. public static void main( String[] args ){ //membuat 3 objek • StudentRecord annaRecord=new StudentRecord("Anna"); • StudentRecord beahRecord=new StudentRecord("Beah",“India"); • StudentRecord crisRecord=new StudentRecord(80,90,100); }

More Related