Advertisements
Advertisements
Question
Design a class to overload a function area( ) as follows:
(i) double area (double a, double b, double c) with three double arguments, returns the area of a scalene triangle using the formula:
area = `sqrt ("s" ("s" - "a")("s" - "b")("s" - "c")) "2ab"`
where `"s" = ("a" + "b" + "c")/2`
(ii) 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)
(iii) double area (double diagonal 1, double diagonal 2) with two double arguments, returns the area of a rhombus using the formula :
area = `1/2` (diagonal 1 × diagonal 2)
Advertisements
Solution
class Overload
{
double area(double a; double b, double c)
{
doub;le s, r;
s=(a+b+c)/2;
r=s*(s-a)*(s-b)*(s-c);
double ar = Math.sqrt (r);
return ar;
}
double area(int a, int b, int height)
{
return area;
}
double area (double diagonal1,double diagonal2)
{
double area=(diagonal1*diagonal2)/2;
return area;
}
The variable description is as follows:
| S.No. | Variable Name | Data type | Purpose |
| 1. | a | double | to enter side of the triangle |
| 2. | b | double | to enter side of the triangle |
| 3. | c | double | to enter side of the triangle |
| 4. | s | double | to store the value of s in area formula used. |
| 5. | r | double | to store the value of r in area formula used. |
| 6. | area | double | to store area of various shapes |
| 7. | a | int | to enter side of trapezium |
| 8. | b | int | to enter side of trapezium |
| 9. | height | int | to enter height of trapezium |
| 10. | diagonal 1 | double | to enter diagonal of rhombus |
| 11. | diagonal 2 | double | to enter diagonal of rhombus |
APPEARS IN
RELATED QUESTIONS
Write the memory capacity (storage size) of short and float data type in bytes.
Give the output of the following code:
String P = “20”, Q = “19”,
int a = Integer .parselnt(P);
int b = Integer. valueOf(Q);
System.out.println(a+””+b);
State the data type and value of res after the following is executed :
char ch = ‘9’;
res = Character. isDigit(ch)
State the number of bytes occupied by char and int data types.
int res = ‘A’;
What is the value of res?
Predict the output of the following code snippet: String P = "20", Q = "22";
int a = Integer.parseInt(P);
int b = Integer.value0f(Q);
System.out.println(a + " " + b);
Give the output of the following Character class method:
Character.toUpperCase('a')Give the output of the following Character class method:
Character.isLetterOrDigit('W')Write the value of n after execution:
char ch = 'd';
int n = ch + 5;Define a class to accept a string and convert the same to uppercase, create and display the new string by replacing each vowel by immediate next character and every consonant by the previous character. The other characters remain the same.
Example: Input: #IMAGINATION@2024
Output: #JLBFJMBSJPM@2024
