Computer Applications
Design a class to overload a function area( ) as follows:
- double area (double a, double b, double c) with three double arguments, returns the area of a scalene triangle using the formula:
area = √(s(s-a)(s-b)(s-c))
where s = (a+b+c) / 2 - double area (int a, int b, int height) with three integer arguments, returns the area of a trapezium using the formula:
area = (1/2)height(a + b) - double area (double diagonal1, double diagonal2) with two double arguments, returns the area of a rhombus using the formula:
area = 1/2(diagonal1 x diagonal2)
Java
User Defined Methods
ICSE 2014
276 Likes
Answer
import java.util.Scanner;
public class KboatOverload
{
double area(double a, double b, double c) {
double s = (a + b + c) / 2;
double x = s * (s-a) * (s-b) * (s-c);
double result = Math.sqrt(x);
return result;
}
double area (int a, int b, int height) {
double result = (1.0 / 2.0) * height * (a + b);
return result;
}
double area (double diagonal1, double diagonal2) {
double result = 1.0 / 2.0 * diagonal1 * diagonal2;
return result;
}
}Variable Description Table
Program Explanation
Output
Answered By
134 Likes
Related Questions
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
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 5void 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
Assertion (A): An argument is a value that is passed to a method when it is called.
Reason (R): Variables which are declared in a method prototype to receive values are called actual parameters
- Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A)
- Both Assertion (A) and Reason (R) are true and Reason (R) is not a correct explanation of Assertion(A)
- Assertion (A) is true and Reason (R) is false
- Assertion (A) is false and Reason (R) is true
Assertion (A): Static method can access static and instance variables.
Reason (R): Static variable can be accessed only by static method.
- Assertion and Reason both are correct.
- Assertion is true and Reason is false.
- Assertion is false and Reason is true.
- Assertion and Reason both are false.