• algorithm: Urutan instruksi program.
• Contoh: "Bake sugar cookies"
– Mix the dry ingredients.
– Cream the butter and sugar.
– Beat in the eggs.
– Stir in the dry ingredients.
– Set the oven temperature.
– Set the timer.
– Place the cookies into the oven.
– Allow the cookies to bake.
– Spread frosting and sprinkles onto the cookies.
Permasalahan
• Struktur tidak jelas: banyak instruksi yang sulit diingat.
• duplikasi:...
– Mix the dry ingredients.
– Cream the butter and sugar.
– Beat in the eggs.
– Stir in the dry ingredients.
– Set the oven temperature.
– Set the timer.
– Place the first batch of cookies into the oven.
– Allow the cookies to bake.
– Set the timer.
– Place the second batch of cookies into the oven.
– Allow the cookies to bake.
– Mix ingredients for frosting.
Strukturisasi
• structured algorithm: Pisahkan menjadi instruksi terpisah.
1 Make the cookie batter.
– Mix the dry ingredients.
– Cream the butter and sugar.
– Beat in the eggs.
– Stir in the dry ingredients.
2 Bake the cookies.
– Set the oven temperature.
– Set the timer.
– Place the cookies into the oven.
– Allow the cookies to bake.
3 Add frosting and sprinkles.
– Mix the ingredients for the frosting.
– Spread frosting and sprinkles onto the cookies.
Menghilangkan Duplikasi
• Suatu algoritma yang baik dapat menunjukkanprogram yang tidak terduplikasi.
–
1 Make the cookie batter.
– Mix the dry ingredients.
–...
–
2a Bake the cookies (first batch).
– Set the oven temperature.
– Set the timer.
–...
2b Bake the cookies (second batch).
–
3 Decorate the cookies.
–...
Dengan Duplikasi
public class BakeCookies {
public static void main(String[] args) {
System.out.println("Mix the dry ingredients.");
System.out.println("Cream the butter and sugar.");
System.out.println("Beat in the eggs.");
System.out.println("Stir in the dry ingredients.");
System.out.println("Set the oven temperature.");
System.out.println("Set the timer.");
System.out.println("Place a batch of cookies into the oven.");
System.out.println("Allow the cookies to bake.");
System.out.println("Set the oven temperature.");
System.out.println("Set the timer.");
System.out.println("Place a batch of cookies into the oven.");
System.out.println("Allow the cookies to bake.");
System.out.println("Mix ingredients for frosting.");
System.out.println("Spread frosting and sprinkles.");
}
}
Static Methods
•static method: Suatu namagroup dari statement.
•Menunjukkan struktur program
•Menghilangkan duplikasi
–
–dekomposisi:
memecahkan menjadi methods
memecahkan menjadi methods
–
–
•Menuliskan static method seperti
menambahkaninstruksi baru.
Menggunakan Static Methods
1. Rancang Algoritma.
– Lihat dari struktur masalah, dan lihat duplikasi.
– Lihat tujuan dari solusi masalah.
2. Tuliskan method nya.
– Atur instruksi ke dalamgroup dan berikan namanya.
3. Jalankan.
– Dimulai dari main method.
Perancangan Algoritma
// This program displays a delicious recipe for baking cookies.
public class BakeCookies2 {
public static void main(String[] args) {
// Step 1: Make the cake batter.
System.out.println("Mix the dry ingredients.");
System.out.println("Cream the butter and sugar.");
System.out.println("Beat in the eggs.");
System.out.println("Stir in the dry ingredients.");
// Step 2a: Bake cookies (first batch).
System.out.println("Set the oven temperature.");
System.out.println("Set the timer.");
System.out.println("Place a batch of cookies into the oven.");
System.out.println("Allow the cookies to bake.");
// Step 2b: Bake cookies (second batch).
System.out.println("Set the oven temperature.");
System.out.println("Set the timer.");
System.out.println("Place a batch of cookies into the oven.");
System.out.println("Allow the cookies to bake.");
// Step 3: Decorate the cookies.
System.out.println("Mix ingredients for frosting.");
System.out.println("Spread frosting and sprinkles.");
}
}
Pendeklarasian Method
Berikan nama untuk setiap method
• Syntax:
public static void name() {
statement;
statement;
...
statement;
}
public static void name() {
statement;
statement;
...
statement;
}
• Contoh:
public static void printWarning() {
System.out.println("This product causes cancer");
System.out.println("in lab rats and humans.");
}
public static void printWarning() {
System.out.println("This product causes cancer");
System.out.println("in lab rats and humans.");
}
Memanggil Method
Jalankan
• Syntax:
name();
–
–Kita bisa memanggil method berkali-kali.
–
• Contoh:
printWarning();
– Output:
This product causes cancer
in lab rats and humans.
Program dengan static method
public class FreshPrince {
public static void main(String[] args) {
rap(); // Calling (running) the rap method
System.out.println();
rap(); // Calling the rap method again
}
// This method prints the lyrics to my favoritesong.
public static void rap() {
System.out.println("Now this is the story all about how");
System.out.println("My life got flipped turned upside-down");
}
}
Output:
Now this is the story all about how
My life got flipped turned upside-down
Now this is the story all about how
My life got flipped turned upside-down
Final Program
// This program displays a delicious recipe for baking cookies.
public class BakeCookies3 {
public static void main(String[] args) {
makeBatter();
bake(); // 1st batch
bake(); // 2nd batch
decorate();
}
// Step 1: Make the cake batter.
public static void makeBatter() {
System.out.println("Mix the dry ingredients.");
System.out.println("Cream the butter and sugar.");
System.out.println("Beat in the eggs.");
System.out.println("Stir in the dry ingredients.");
}
// Step 2: Bake a batch of cookies.
public static void bake() {
System.out.println("Set the oven temperature.");
System.out.println("Set the timer.");
System.out.println("Place a batch of cookies into the oven.");
System.out.println("Allow the cookies to bake.");
}
// Step 3: Decorate the cookies.
public static void decorate() {
System.out.println("Mix ingredients for frosting.");
System.out.println("Spread frosting and sprinkles.");
}
}
Methods Memanggil Methods
public class MethodsExample {
public static void main(String[] args) {
message1();
message2();
System.out.println("Done with main.");
}
public static void message1() {
System.out.println("This is message1.");
}
public static void message2() {
System.out.println("This is message2.");
message1();
System.out.println("Done with message2.");
}
}
• Output:
This is message1.
This is message2.
This is message1.
Done with message2.
Done with main.
Aliran Kontrol
• Saat suatu method dipanggil...
– “berpindah" ke method tersebut dan menjalankannya. lalu
– “kembali" ke poin sebelumnya
}
Kapan Menggunakan Methods
• Gunakan static method jika:
– statements berhubungan secara struktural, dan/atau
– Berulang-ulang dipanggil / terduplikasi.
• Jangan gunakan static methods jika:
– Hanya satu println
– Hanya baris kosong
– Tidak ada perulangan atau duplikasi Terima Kasih :)
Tidak ada komentar:
Posting Komentar