-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram19_Shape.java
More file actions
102 lines (82 loc) · 2.7 KB
/
Program19_Shape.java
File metadata and controls
102 lines (82 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Design a class hierarchy for a shape. Create a base class Shape with methods area() and perimeter(). Derive classes Circle, Rectangle, and Triangle from Shape. Implement the necessary methods in each derived class. Create objects of each class and calculate the area and perimeter for each.
// Date : 10/01/2024, Author : Yash Wadhvani
class Shape {
double area() {
return 0.0d;
}
double perimeter() {
return 0.0d;
}
}
class Circle extends Shape {
double rad;
Circle(double rad) {
this.rad = rad;
}
@Override
double perimeter() {
return (2 * Math.PI * this.rad);
}
@Override
double area() {
return (Math.PI * Math.pow(this.rad, 2));
}
void displayMetrics() {
System.out.println(String.format("Radius of Circle= %.2f", this.rad));
System.out.println(String.format("Perimeter of Circle = %.2f", this.perimeter()));
System.out.println(String.format("Area of Circle = %.2f", this.area()));
System.out.println();
}
}
class Rectangle extends Shape {
double height, width;
Rectangle(double height, double width) {
this.height = height;
this.width = width;
}
@Override
double perimeter() {
return (2 * (this.height + this.width));
}
@Override
double area() {
return (this.height * this.width);
}
void displayMetrics() {
System.out.println(String.format("Height of Rectangle = %.2f", this.height));
System.out.println(String.format("Width of Rectangle = %.2f", this.width));
System.out.println(String.format("Perimeter of Rectangle = %.2f", this.perimeter()));
System.out.println(String.format("Area of Rectangle = %.2f", this.area()));
System.out.println();
}
}
class Triangle extends Shape {
double side;
Triangle(double side) {
this.side = side;
}
@Override
double area() {
return ((Math.sqrt(3) / 4) * Math.pow(this.side, 2));
}
@Override
double perimeter() {
return (3 * this.side);
}
void displayMetrics() {
System.out.println(String.format("Side of Triangle = %.2f", this.side));
System.out.println(String.format("Perimeter of Triangle = %.2f", this.perimeter()));
System.out.println(String.format("Area of Triangle = %.2f", this.area()));
System.out.println();
}
}
public class Program19_Shape {
public static void main(String[] args) {
Circle circle = new Circle(10);
circle.displayMetrics();
Rectangle rectangle = new Rectangle(15, 20);
rectangle.displayMetrics();
Triangle triangle = new Triangle(5);
triangle.displayMetrics();
}
}