• Objek berinteraksi menggunakan metode
  • Diketahui objek A berkomunikasi dengan objek B.
  • Objek A dan B akan saling berkomunikasi tergantung informasi yang dibutuhkan.

INTERAKSI ANTAR OBJEK
  • Komunikasi A dan B :
  • Objek A akan mencari method Objek B yang sesuai perintah yang akan dikerjakan.
  • Berdasarkan service request, Objek A memberikan informasi tambahan ke Objek B yang akan diproses.
  • Objek B harus mengetahui apakah objek A mengharapkan objek B untuk memberikan report balik setelah perintah dikerjakan.

METODE / METHOD


DEKLARASI METHOD




HEADER METHOD
  • Header method yaitu spesifikasi formal bagaimana method dipanggil.
  • Spesifikasi minimum terdiri dari :
  • Tipe Return
  • Nama method
  • Parameter

Contoh :


NAMING CONVENTION
  • The first letter of the method name is in lowercase.
  • The first letter of each subsequent concatenated word in the method name is in uppercase, and the remaining characters are in lowercase.
  • We don’t use any “punctuation” characters— dashes, underscores, etc.—to separate these words.
  • As an example, chooseAdvisor is an appropriate method name, whereas none of the following would be appropriate: ChooseAdvisor (uppercase “C”), chooseadvisor (lowercase “a”), choose_advisor (separating underscore)

PASSING ARGUMENT
  • The purpose of passing arguments into a method is twofold [Barker05]:
  • To provide it with the (optional) “fuel” necessary to do its job
  • To otherwise guide its behavior in some fashion

Contoh :

  • boolean registerForCourse(String courseID, int secNo)
  • boolean registerForCourse()



CONTOH PASSING ARGUMENT
public void test() { int x = 5; proses(x);
System.out.println(“x1:”+ x);
}

public void proses(int x) { x = x + 10;
System.out.println(“x2:”+ x);
}


CONTOH PASSING ARGUMENT
public void test() { int x = 5; proses(x);
System.out.println(“x1:”+ x);
}

public int proses(int x) { x = x + 10;
System.out.println(“x2:”+ x); return x;
}

TIPE RETURN METHOD
  • Tipe return method yaitu :
  • void
  • tipe data primitif
  • tipe data user-defined (class)
  • array
  • collection

Contoh :

void setName(String newName)
void switchMajor(String newDepartment, Professor newAdvisor)
Professor getAdvisor()


ANALOGI
  • Aktivitas “household chores” :
  • Taking out the trash
  • Mowing the lawn
  • Washing the car
  • Berarti dengan aktivitas seperti di atas akan memiliki class Person dengan tiga method di dalamnya. Methodnya yaitu :
void takeOutTheTrash()
boolean mowTheLawn()
void washTheCar(Car c)


BADAN METHOD
  • Badan method sebagai tempat implementasi program.
  • Dibatasi dengan tanda kurawal (buka dan tutup) seperti { .... }
  • Contoh :



STATEMENT RETURN
  • Statement return yaitu statement jump yang digunakan untuk keluar dari method.
  • Statement yang dikembalikan harus sama dengan tipe return.
  • Contoh untuk integer :


STATEMENT RETURN
Contoh untuk boolean :



STATEMENT RETURN
Contoh :


STATEMENT RETURN
Contoh :

Statement else bisa dihilangkan karena nilai false sudah diinisialisasi.


REFERENSI
  • Beginning Java Object: From Concept to Code. Author: JACQUIE BARKER
  • SoftwareEngineering: A Practitioner Approach 7th Edition. Author: Roger S Pressman

Tidak ada komentar:

Diberdayakan oleh Blogger.