4
Define a class to overload the method perform as follows:
double perform (double r, double h) — to calculate and return the value of curved surface area of cone
$CSA = \pi rl \quad l = \sqrt{r^2 + h^2}$
void perform (int r, int c) — Use NESTED FOR LOOP to generate the following format
r = 4, c = 5
output
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
void perform (int m, int n, char ch) — to print the quotient of the division of m and n if ch is Q else print the remainder of the division of m and n if ch is R
Show SolutionHide Solution↓
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');
}
}
