-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inheritance_Hierarchy.java
134 lines (130 loc) · 3.82 KB
/
Inheritance_Hierarchy.java
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//Read Description for question
import java.util.*;
class GeometricObject{
String color;
boolean filled;
GeometricObject(){
color="White";
filled=false;
}
GeometricObject(String s,boolean b){
color=s;
filled=b;
}
boolean isFilled(){
return filled;
}
String getColor(){
return color;
}
void setColor(String s){
color=s;
}
void setFilled(boolean b){
filled=b;
}
}
class Circle extends GeometricObject{
double radius;
Circle(){
radius=1.0;
}
Circle(double d){
radius=d;
}
Circle(double d,String s, boolean f){
radius=d;
color=s;
filled=f;
}
void setRadius(double d){
radius=d;
}
double getArea(){
return Math.PI*radius*radius;
}
double getPerimeter(){
return 2*Math.PI*radius;
}
void printCircle(){
System.out.println("Radius:"+radius);
}
}
class Rectangle extends GeometricObject{
double width,height;
Rectangle(){
width=height=1;
}
Rectangle(double l, double b){
width=l;
height=b;
}
Rectangle(double l, double b,String s, boolean f){
width=l;
height=b;
color=s;
filled=f;
}
void setData(double l, double b){
width=l;
height=b;
}
double getArea(){
return width*height;
}
double getPerimeter(){
return 2*(width+height);
}
void printRectangle(){
System.out.println("Width:"+width);
System.out.println("Height:"+height);
}
}
class Main{
public static void main(String args[]){
Circle c1=new Circle();
c1.setRadius(3.2);
System.out.println("Circle Object 1");
c1.printCircle();
System.out.println("Area:"+c1.getArea());
System.out.println("Perimeter:"+c1.getPerimeter());
System.out.println("Color:"+c1.getColor());
System.out.println("Filled:"+c1.isFilled());
Circle c2=new Circle(2.0);
System.out.println("Circle Object 2");
c2.printCircle();
System.out.println("Area:"+c2.getArea());
System.out.println("Perimeter:"+c2.getPerimeter());
System.out.println("Color:"+c2.getColor());
System.out.println("Filled:"+c2.isFilled());
Circle c3=new Circle(3.0,"Black",true);
System.out.println("Circle Object 3");
c3.printCircle();
System.out.println("Area:"+c3.getArea());
System.out.println("Perimeter:"+c3.getPerimeter());
System.out.println("Color:"+c3.getColor());
System.out.println("Filled:"+c3.isFilled());
Rectangle r1=new Rectangle();
r1.setData(20,30);
System.out.println("Rectangle Object 1");
r1.printRectangle();
System.out.println("Area:"+r1.getArea());
System.out.println("Perimeter:"+r1.getPerimeter());
System.out.println("Color:"+r1.getColor());
System.out.println("Filled:"+r1.isFilled());
Rectangle r2=new Rectangle(5,10);
System.out.println("Rectangle Object 2");
r2.printRectangle();
System.out.println("Area:"+r2.getArea());
System.out.println("Perimeter:"+r2.getPerimeter());
System.out.println("Color:"+r2.getColor());
System.out.println("Filled:"+r2.isFilled());
Rectangle r3=new Rectangle(10,15,"Red",true);
System.out.println("Rectangle Object 3");
r3.printRectangle();
System.out.println("Area:"+r3.getArea());
System.out.println("Perimeter:"+r3.getPerimeter());
System.out.println("Color:"+r3.getColor());
System.out.println("Filled:"+r3.isFilled());
}
}