Module 7 – Methods

ශ්‍රිත (Reusable Code)

1. Method (ශ්‍රිතයක්) යනු කුමක්ද?

Method එකක් යනු නිශ්චිත කාර්යයක් ඉටු කිරීම සඳහා නිර්මාණය කරන ලද, නැවත නැවත භාවිත කළ හැකි කේත කොටසකි (reusable block of code). Java හි, methods යම් class එකක් තුළ අර්ථ දක්වනු ලැබේ. අපට අවශ්‍ය ඕනෑම විටක method එකේ නම අඬගැසීමෙන් (calling) එම කේත කොටස ක්‍රියාත්මක කරවිය හැක.


2. Method එකක් නිර්මාණය කිරීම

Method එකක් නිර්මාණය කිරීමේදී, එහි return type (ආපසු ලබාදෙන දත්ත වර්ගය), නම, සහ වරහන් () තුළ parameters (පරාමිතීන්) සඳහන් කරයි. කිසිදු අගයක් ආපසු ලබා නොදෙන්නේ නම්, return type එක void ලෙස යොදයි.

ව්‍යුහය (Syntax):

[returnType] [methodName]([parameter1], [parameter2], ...) {
    // method body - the code to be executed
}

3. Parameters සහ Arguments

Method එකකට පිටතින් දත්ත ලබා දීමට Parameters භාවිතා කරයි. Method එක නිර්වචනය කිරීමේදී වරහන් තුළ ලබාදෙන විචල්‍යයන් parameters වේ. Method එක call කරන විට එම parameters සඳහා ලබාදෙන සැබෑ අගයන් Arguments ලෙස හැඳින්වේ.


4. අගයක් ආපසු ලබා දීම (`return` statement)

Method එකකට යම් ගණනය කිරීමක් කර, එහි ප්‍රතිඵලය ආපසු ලබා දීමට return ප්‍රකාශය භාවිතා කරයි. return කරන අගයේ දත්ත වර්ගය, method එකේ return type එකට ගැලපිය යුතුය.


ප්‍රායෝගික උදාහරණ 10ක්

1. සරල Method එකක් Call කිරීම

සරල සුබ පැතුමක් පෙන්වන, කිසිවක් return නොකරන (void) method එකක්.

public class Example1 {
    // Method definition
    static void sayHello() {
        System.out.println("Hello from a method!");
    }

    public static void main(String[] args) {
        // Calling the method
        sayHello();
    }
}
ප්‍රතිඵලය:
Hello from a method!
2. Parameter එකක් සහිත Method එකක්

නමක් parameter එකක් ලෙස ලබාගෙන, පුද්ගලික සුබ පැතුමක් පෙන්වමු.

public class Example2 {
    static void greet(String name) {
        System.out.println("Hello, " + name + "!");
    }

    public static void main(String[] args) {
        greet("Kamal"); // "Kamal" is the argument
    }
}
ප්‍රතිඵලය:
Hello, Kamal!
3. `return` මගින් `int` අගයක් ආපසු ලබා දීම

අංක දෙකක් එකතු කර, එහි ප්‍රතිඵලය ආපසු ලබා දෙන method එකක්.

public class Example3 {
    static int add(int x, int y) {
        return x + y;
    }

    public static void main(String[] args) {
        int sum = add(10, 20); // Store the returned value
        System.out.println("The sum is: " + sum);
    }
}
ප්‍රතිඵලය:
The sum is: 30
4. සෘජුකෝණාස්‍රයක වර්ගඵලය සෙවීම
public class Example4 {
    static double calculateArea(double width, double height) {
        return width * height;
    }

    public static void main(String[] args) {
        double area = calculateArea(5.5, 10.0);
        System.out.println("The area of the rectangle is " + area);
    }
}
ප්‍රතිඵලය:
The area of the rectangle is 55.0
5. Method එකක Return අගය `if` statement එකක භාවිතය
public class Example5 {
    static boolean isAdult(int age) {
        return age >= 18;
    }

    public static void main(String[] args) {
        if (isAdult(22)) {
            System.out.println("This person is an adult.");
        } else {
            System.out.println("This person is not an adult.");
        }
    }
}
ප්‍රතිඵලය:
This person is an adult.
6. `String` අගයක් Return කිරීම
public class Example6 {
    static String getFullName(String firstName, String lastName) {
        return firstName + " " + lastName;
    }

    public static void main(String[] args) {
        String fullName = getFullName("Saman", "Silva");
        System.out.println(fullName);
    }
}
ප්‍රතිඵලය:
Saman Silva
7. Method Overloading

එකම නම සහිත, නමුත් විවිධ parameters ඇති methods නිර්මාණය කළ හැක.

public class Example7 {
    static int add(int a, int b) {
        return a + b;
    }
    static double add(double a, double b) {
        return a + b;
    }

    public static void main(String[] args) {
        System.out.println("Integer sum: " + add(5, 10));
        System.out.println("Double sum: " + add(5.5, 10.5));
    }
}
ප්‍රතිඵලය:
Integer sum: 15
Double sum: 16.0
8. Array එකක් Parameter එකක් ලෙස

Array එකක් method එකකට යවා, එහි ඇති සියලුම අයිතම print කරමු.

public class Example8 {
    static void printArray(String[] arr) {
        for (String item : arr) {
            System.out.println(item);
        }
    }

    public static void main(String[] args) {
        String[] colors = {"Red", "Green", "Blue"};
        printArray(colors);
    }
}
ප්‍රතිඵලය:
Red
Green
Blue
9. Array එකක එකතුව Return කිරීම
public class Example9 {
    static int sumArray(int[] numbers) {
        int sum = 0;
        for (int num : numbers) {
            sum += num;
        }
        return sum;
    }

    public static void main(String[] args) {
        int[] myNumbers = {10, 20, 30, 40};
        System.out.println("Sum of the array is: " + sumArray(myNumbers));
    }
}
ප්‍රතිඵලය:
Sum of the array is: 100
10. Method එකක් තුළ තවත් Method එකක් Call කිරීම
public class Example10 {
    static int getSquare(int number) {
        return number * number;
    }

    static void displaySquare(int num) {
        int square = getSquare(num); // Calling another method
        System.out.println("The square of " + num + " is " + square);
    }

    public static void main(String[] args) {
        displaySquare(5);
    }
}
ප්‍රතිඵලය:
The square of 5 is 25