-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathPg2
90 lines (80 loc) · 1.45 KB
/
Pg2
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
/*2) Write a program in java to implement the abstract class and override the methods of the
abstract class in the provided derived classes.
Class : Shape2D
Members: type
Abstract methods: area(), perimeter()
Class: Circle (inherits Shape2D)
Members: center, radius
Methods: area(), perimeter(), print()
Class: Triangle (inherits Shape2D)
Members: base, height
Methods: area(), perimeter(), print()
*/
import java.lang.Math;
abstract class Shape2d
{
String type;
abstract double area();
abstract double perimeter();
}
class Circle extends Shape2d
{ double aoc,poc,r;
Circle(double c)
{
r=c;
}
double area()
{
aoc=3.14*r*r;
return aoc;
}
double perimeter()
{
poc=2*3.14*r;
return poc;
}
void print()
{
System.out.println("area of "+type+" : "+aoc);
System.out.println("perimeter of "+type+" : "+poc);
}
}
class Triangle extends Shape2d
{double aot,pot,b,p,h;
Triangle(double d,double e)
{
b=d;
h=e;
}
double area()
{
aot=(b*h)/2;
return aot;
}
double perimeter()
{
p=Math.sqrt((b*b)+(h*h));
pot=b+h+p;
return pot;
}
void print()
{
System.out.println("area of "+type+":"+aot);
System.out.println("perimeter of "+type+" : "+pot);
}
}
public class Pg2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Circle s=new Circle(5.0);
Triangle t=new Triangle(9.0,9.0);
s.type="circle";
s.area();
s.perimeter();
s.print();
t.type="triangle";
t.area();
t.perimeter();
t.print();
}
}