- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram19_Shape.java
More file actions
Latest commit
102 lines (82 loc) · 2.7 KB
/
Copy pathProgram19_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
classShape {
doublearea() {
return0.0d;
}
doubleperimeter() {
return0.0d;
}
}
classCircleextendsShape {
doublerad;
Circle(doublerad) {
this.rad = rad;
}
@Override
doubleperimeter() {
return (2 * Math.PI * this.rad);
}
@Override
doublearea() {
return (Math.PI * Math.pow(this.rad, 2));
}
voiddisplayMetrics() {
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();
}
}
classRectangleextendsShape {
doubleheight, width;
Rectangle(doubleheight, doublewidth) {
this.height = height;
this.width = width;
}
@Override
doubleperimeter() {
return (2 * (this.height + this.width));
}
@Override
doublearea() {
return (this.height * this.width);
}
voiddisplayMetrics() {
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();
}
}
classTriangleextendsShape {
doubleside;
Triangle(doubleside) {
this.side = side;
}
@Override
doublearea() {
return ((Math.sqrt(3) / 4) * Math.pow(this.side, 2));
}
@Override
doubleperimeter() {
return (3 * this.side);
}
voiddisplayMetrics() {
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();
}
}
publicclassProgram19_Shape {
publicstaticvoidmain(String[] args) {
Circlecircle = newCircle(10);
circle.displayMetrics();
Rectanglerectangle = newRectangle(15, 20);
rectangle.displayMetrics();
Triangletriangle = newTriangle(5);
triangle.displayMetrics();
}
}