အခြေခံ ကွန်ပျူတာပရိုဂရမ် ရေးသားနည်း - Functions

Functions 

Function ကို task လုပ်ဆောင်စရာတွေကို သီးသန့်ခွဲထုတ်တဲ့အခါမှာ အသုံးပြုလေ့ရှိတယ်။ Procedure လို့လည်း ခေါ်တယ်။ Java မှာတော့ function ကို method လို့ ခေါ်တယ်။ ရေးသားထားတဲ့ code ကို reuse ပြန်လည်အသုံးပြုဖို့ရာမှာ အသုံးဝင်ပါတယ်။


ဉပမာ အီးမေးလ်ပို့ဖို့ကို function တစ်ခု SMS ပို့ဖို့ function တစ်ခု စသဖြင့် လုပ်ဆောင်စရာတွေကို သီးသန့်ခွဲခြား ရေးသားလို့ရပါတယ်။ အီးမေးလ်ပို့ဖို့လိုတဲ့ class တိုင်းက လိုသလို ခေါ်သုံးလို့ ရပါတယ်။ 

Static Method vs Instance Method

Math.pow() နဲ့ Math.random() အစရှိတဲ့ method တွေကို static method တွေလို့ ခေါ်တယ်။ Variable သို့မဟုတ် method ရဲ့ ရှေ့မှာ static ဆိုတဲ့ keyword ထည့်ပေးရပါတယ်။ Static ရဲ့ အဓိပ္ပါယ်က Class ထဲမှာ ကြေညာထားတဲ့ variable သို့မဟုတ် method ဟာ Class ကို ခေါ်အသုံးပြုတဲ့ object တွေ အားလုံးအတွက် တူညီတယ်ဆိုတဲ့ အဓိပ္ပါယ် ဖြစ်ပါတယ်။ constant တွေ ကြေညာဖို့အတွက်လည်း အသုံးပြုပါတယ်။ Memory ကို အကျိုးရှိစွာ အသုံးပြုဖို့ ရည်ရွယ်ပါတယ်။  


Instance variable သို့မဟုတ် instance method တွေကို အသုံးပြုဖို့ new ဆိုတဲ့ keyword နဲ့ အရင် instantiate လုပ်ရပါတယ်။ ပြီးမှ Class ရဲ့ variable နဲ့ method တွေကို အသုံးပြုလို့ ရပါတယ်။ 


For example : 


String[] strArr = new String[4]; // instantiate first

strArr[0] = 100;

Example of Static Method

/********************************************************************

 *  Compilation:  javac Harmonic.java

 *  Execution:    java Harmonic n

 *  

 *  Prints the nth harmonic number: 1/1 + 1/2 + ... + 1/n.

********************************************************************/


public class Harmonic { 

    // returns 1/1 + 1/2 + 1/3 + ... + 1/n

    public static double harmonic(int n) {

        double sum = 0.0;

        for (int i = 1; i <= n; i++) {

            sum += 1.0 / i;

        }

        return sum;

    }


    public static void main(String[] args) { 

        for (int i = 0; i < args.length; i++) {

            int arg = Integer.parseInt(args[i]);

            double value = harmonic(arg);

            StdOut.println(value);

        }

    }

}

Example of Instance Method

/********************************************************************

 *  Compilation:  javac InstanceHarmonic.java

 *  Execution:    java InstanceHarmonic n

 *  

 *  Prints the nth harmonic number: 1/1 + 1/2 + ... + 1/n.

********************************************************************/


class InstanceMethodExample{


// returns 1/1 + 1/2 + 1/3 + ... + 1/n

public double harmonic(int n) {

double sum = 0.0;

for (int i = 1; i <= n; i++) {

sum += 1.0 / i;

}

return sum;

}

}


public class InstanceHarmonic { 

    public static void main(String[] args) { 

  InstanceMethodExample insMthd = new InstanceMethodExample();

        for (int i = 0; i < args.length; i++) {

            int arg = Integer.parseInt(args[i]);

            double value = insMthd.harmonic(arg);

            StdOut.println(value);

        }

    }

}

Libraries and Clients

Java ပရိုဂရမ်ဖိုင်တွေကို တခြားပရိုဂရမ်ဖိုင်တွေကနေ ယူသုံးလို့ ရပါတယ်။ Math နဲ့ System လို java file တွေကို Library လို့ခေါ်တယ်။ အဲ့ဒီ Library တွေကို ယူသုံးတဲ့ class တွေကိုတော့ client လို့ ခေါ်တယ်။

Example of Libraries and Clients

/********************************************************************

 *  Compilation:  javac Heart.java

 *  Execution:    java Heart

 *  Dependencies: StdDraw.java

 *

 *  Draw a pink heart (a filled diamond plus two filled circles).

 *

********************************************************************/


public class Heart {

    public static void main(String[] args) {

        StdDraw.setXscale(-1.5, +1.5);

        StdDraw.setYscale(-1.5, +1.5);

        StdDraw.setPenColor(StdDraw.PINK);


        // draw diamond

        double[] xs = { -1,  0, 1, 0 };

        double[] ys = {  0, -1, 0, 1 };

        StdDraw.filledPolygon(xs, ys);


        // circles

        StdDraw.filledCircle(+0.5, 0.5, 1 / Math.sqrt(2));

        StdDraw.filledCircle(-0.5, 0.5, 1 / Math.sqrt(2));

    }

}

Recursion

Function တစ်ခုကို တခြား function တစ်ခုက ခေါ်လို့ ရတယ်ဆိုတဲ့ အိုင်ဒီယာဟာ function က ကိုယ့်ကိုကိုယ် ပြန်ခေါ်လို့ရမဲ့ အိုင်ဒီယာကိုပါ တွေးမိစေတယ်။ အဲ့ဒီလို ​ကိုယ့်ကိုကိုယ် ပြန်ခေါ်တဲ့ function ကို recursive function လို့ ခေါ်တယ်။ 

Example of Recursive Method

/********************************************************************

 *  Compilation:  javac Factorial.java

 *  Execution:    java Factorial n

 *

 *  Reads an integer command-line argument n, and prints n! = 1 * 2 * ... * n

 *  to standard output.

 *

 *  % java Factorial 0

 *  1

 *

 *******************************************************************/


public class Factorial {


    // return n!

    // precondition: n >= 0 and n <= 20

    public static long factorial(long n) {

        if      (n <  0) throw new RuntimeException("Underflow error in factorial");

        else if (n > 20) throw new RuntimeException("Overflow error in factorial");

        else if (n == 0) return 1;

        else             return n * factorial(n-1);

    }


    public static void main(String[] args) {

        long n = Long.parseLong(args[0]);

        StdOut.println(factorial(n));

    }

}



*********

Comments

Popular posts from this blog

စာသင်ကြားရန် အသုံးပြုသည့် ဆော့ဖ်ဝဲများ

အခြေခံ ကွန်ပျူတာပရိုဂရမ် ရေးသားနည်း - Objected-Oriented Programming

သင့်ရဲ့ ပထမဉီးဆုံးသော ကွန်ပျူတာ ပရိုဂရမ် (Hello, World!)