-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLab6B.java
More file actions
66 lines (52 loc) · 1.49 KB
/
Lab6B.java
File metadata and controls
66 lines (52 loc) · 1.49 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
// 6: Develop a Java program to create an abstract class shape with 2 abstract methods area() and perimeter(). Create 2 sub classes
// Circle and Triangle that extends the shape and implements the respective methods to calculate the area and perimeter of the each shape.
abstract class Shape {
abstract void calArea();
abstract void calPeri();
}
class Circle extends Shape {
double r;
Circle(double r) {
this.r = r;
}
void calArea() {
System.out.printf("Area of Circle is %.4f\n", (3.14 * r * r));
}
void calPeri() {
System.out.printf("Perimeter of Circle is %.4f\n", (2 * 3.14 * r));
}
}
class Triangle extends Shape {
double a, b, c;
Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
void calArea() {
double s = (a + b + c) / 2;
double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
System.out.printf("Area of Triangle is %.4f\n", area);
}
void calPeri() {
double s = a + b + c;
System.out.printf("Perimeter of Triangle is %.4f\n", (s));
}
}
public class Lab6B {
public static void main(String[] args) {
Shape obj = new Circle(10);
obj.calArea();
obj.calPeri();
obj = new Triangle(4, 6, 7);
obj.calArea();
obj.calPeri();
}
}
/*
* Output:
* Area of Circle is 314.0000
* Perimeter of Circle is 62.8000
* Area of Triangle is 11.9765
* Perimeter of Triangle is 17.0000
*/