Thursday, 10 May 2018

Java: Perancangan Algoritma dan Static Methods

Berikut ini adalah contoh souce code perancangan algoritma untuk "Bake Sugar Cookies".
  1. Algoritma masih memiliki duplikasi.
  2. Dapat kita lihat pada source code di bawah masih memiliki duplikasi sehingga tidak efektif.
    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.");
        }
    }
    
  3. Perancangan Algoritma.
  4. Dari source code sebelumnya, dapat kita bagikan ke dalam beberapa step untuk merancang algoritmanya.
    // 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.");
        }
    }
    
  5. Algoritma dengan Static Method.
  6. Setelah itu kita buatkan methodnya dan source code akan menjadi seperti ini.
    // 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.");
        }
    }
    

Hasil dari ketiga source code di atas adalah sebagai berikut:

No comments:

Post a Comment

Odoo 11 - Create a new sample module

How To Create or Develop a Custom Module in Odoo 11 In this blog, we‘ll be focusing on how we can create or develop a custom mo...