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, wherel = √(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 ifch = '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');
}
}