1 / 19

Java class

Bahasa pemrograman Pertemuan #9. Java class. Bahasa Pemrogramam. Tujuan. Dapat mendefinisikan kelas sendiri Dapat medeklarasikan method dan atribut Menggunakan referensi this Mengimplementasikan overload method Melakukan kustomisasi access modifier Mengimport dan membuat package.

lali
Download Presentation

Java class

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. BahasapemrogramanPertemuan #9 Java class BahasaPemrogramam

  2. Tujuan • Dapatmendefinisikankelassendiri • Dapatmedeklarasikan method danatribut • Menggunakanreferensi this • Mengimplementasikan overload method • Melakukankustomisasi access modifier • Mengimportdanmembuat package

  3. Konsep CLASS • Apakah Class didalam OOP ? • UntukApa class dibuat? • Bagimana class Digunakan ? • Format Penulisan Class dalam Java

  4. CLASS • Format Umum <modifier> class <name> { <attributeDeclaration>* <constructorDeclaration>* <methodDeclaration>* } public class StudentRecord { //area penulisankodeselanjutnya }

  5. DeklarasiAtibut class <modifier> <type> <name> [= <default_value>]; Instance Variable public class StudentRecord { private String name; private String address; private int age; private double mathGrade; private double englishGrade; private double scienceGrade; private double average; } Private : hanyabisadigunakanpada class tersebut. Objek lain tidakdapatmenggunakanvariabeltersebutsecaralangsung

  6. Class Variable / Static Variable • Variabel yang memilikinilai yang samadisemuaobjekpadakelas yang sama.

  7. Contoh class variabel public class StaticVariable{static intnoOfInstances;StaticVariable(){noOfInstances++;  }public static void main(String[] args){StaticVariable sv1 = new StaticVariable();System.out.println("No. of instances for sv1 : " + sv1.noOfInstances);StaticVariable sv2 = new StaticVariable();System.out.println("No. of instances for sv1 : "  + sv1.noOfInstances);System.out.println("No. of instances for st2 : "  + sv2.noOfInstances);StaticVariable sv3 = new StaticVariable();System.out.println("No. of instances for sv3 : "  + StaticVariable.noOfInstances);    }}

  8. Deklarasi Method • Format umumpenulisan Method <modifier> <returnType> <name>(<parameter>*) { <statement>* } Accessor Method • KonsepEnkapsulasi :: SembarangObjektidakdapatmengakses data private • Untukdapatmengkases data private :-> gunakanAccessor Method

  9. ContohAccessor Method public class StudentRecord { private String name; : : public StringgetName(){ return name; } } public - Menjelaskanbahwa method tersebutdapatdiakses object luarkelas String - Tipe data return value dari method tersebut getName - Namadari method () - Menjelaskanbahwa method tidakmemiliki parameter apapun Tipe data pada instance variable dan method harussama

  10. ContohAccessor Method public class StudentRecord { private String name; : : public double getAverage(){ double result = 0; result = ( mathGrade+englishGrade+scienceGrade )/3; return result; } }

  11. Mutator Method • Tujuan :: Agar Objek lain dapatmengubah data • Dibutuhkansuatu method disebut: Mutator Method • Format Penulisan: set<namaInstanceVariabel>.

  12. ContohMutator Method public class StudentRecord { private String name; : : public void setName( String temp ){ name = temp; } } public - Menjelaskanbahwa method inidapatdipanggil object luarkelas void - Method initidakmenghasilkan return value setName - Namadari method (String temp) - Parameter yang akandigunakanpada method mengidentifikasinilaidari temp samadengan name danmengubah data pada instance variable name.

  13. Program Lengkap Click Here

  14. ReferensiThis • Mengakses instance variable yang dibiaskanoleh parameter public void setAge( int age ){ age = age; //SALAH!!! } public void setAge( int age ){ this.age = age; } Method iniakanmereferensikannilaidari parameter age kepada instance variable dari object StudentRecord

  15. Overloading Method • Method dengannama yang sama,akantetapimemilikifungsi yang berbedabergantungdari parameter yang digunakan • Akandipanggilsesuaidengan parameter yang sesuai

  16. ContohOveloading Method • Pada class StudentRecord, akankitagunakan overloading Method sbb: public void print( String temp ){ System.out.println("Name:" + name); System.out.println("Address:" + address); System.out.println("Age:" + age); } 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); System.out.println("Science Grade:" + sGrade); }

  17. Memanggil Overloading Method public static void main( String[] args ) { StudentRecordannaRecord = new StudentRecord(); annaRecord.setName("Anna"); annaRecord.setAddress("Philippines"); annaRecord.setAge(15); annaRecord.setMathGrade(80); annaRecord.setEnglishGrade(95.5); annaRecord.setScienceGrade(100); //overloaded methods annaRecord.print( annaRecord.getName() ); annaRecord.print( annaRecord.getEnglishGrade(), annaRecord.getMathGrade(), annaRecord.getScienceGrade()); }

  18. Output PemanggilanPertama Name:Anna Address:Philippines Age:15 PemanggilanKedua Name:Anna Math Grade:80.0 English Grade:95.5 Science Grade:100.0

  19. Sekian-:: SelamatBerlatih ::-

More Related