CodeHub Menu
Home Exercise Practice About Download Portfolio Play Games NEW Privacy Policy Terms Contact
Login / Register

Method Overloading in Java – perform() Examples (CSA, Pattern, Quotient/ Remainder)

JAVA November 18, 2025 7 views 0 likes
Method Overloading - perform()

Method Overloading – perform()

Define a class to overload the method perform as follows:

  • double perform(double r, double h) — to calculate and return the Curved Surface Area (CSA) of a cone.
    CSA = π × r × l, where l = √(r² + h²)
  • void perform(int r, int c) — use nested loops to print a pattern
  • void perform(int m, int n, char ch) — print quotient if ch = 'Q' else remainder if ch = 'R'

import java.util.Scanner;

public class KboatOverloadPerform
{
    double perform(double r, double h) {
        double l = Math.sqrt((r * r) + (h * h));
        double csa = Math.PI * r * l;
        return csa;
    }
    
    void perform(int r, int c) {
        for (int i = 1; i <= r; i++) {
            for (int j = 1; j <= c; j++) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
    
    void perform(int m, int n, char ch) {
        if (ch == 'Q') {
            int q = m / n;
            System.out.println("Quotient: " + q);
        } else if (ch == 'R') {
            int r = m % n;
            System.out.println("Remainder: " + r);
        } else {
            System.out.println("Invalid Character!");
        }
    }
    
    public static void main(String[] args) {
        KboatOverloadPerform mo = new KboatOverloadPerform();

        // Calculating CSA of a cone
        double csa = mo.perform(3.0, 4.0);
        System.out.println("Curved Surface Area of Cone: " + csa);

        // Generating pattern
        mo.perform(4, 5);

        // Printing quotient or remainder
        mo.perform(20, 6, 'Q');
        mo.perform(20, 6, 'R');
    }
}
7 views 0 likes
Total visits: 2,089 • Unique visitors: 1,285