Back to the 2026 paper

Module 3: Methods of Java

20267m

Construct overloaded methods for calculating the area of a circle, rectangle and triangle. Analyse how Java selects the appropriate method based on the parameter list.

Worked SolutionAI Assisted

Method overloading for areas

Overloading means methods share a name but have different parameter lists. Java resolves the call at compile time using argument count and types.

class Area {
    static double area(double r) { return Math.PI * r * r; }
    static double area(double length, double width) { return length * width; }
    static double area(double base, double height, boolean triangle) {
        return 0.5 * base * height;
    }
    public static void main(String[] args) {
        System.out.println(area(2.0));
        System.out.println(area(4.0, 3.0));
        System.out.println(area(4.0, 5.0, true));
    }
}

Return type alone cannot distinguish overloads. The extra boolean is used here to give the triangle method a distinct signature; a named method such as triangleArea would be clearer in production code.

Similar questions

Official AppAndroid App

PYQDeck for Android

Solve BEU previous-year exam papers anywhere, anytime. Fast search, offline paper viewing, and step-by-step solutions designed specifically for your phone.