-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab7Task3.java
179 lines (179 loc) · 3.09 KB
/
Lab7Task3.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
class Shape
{
private String color;
private boolean filled;
final double pi=3.14;
Shape()
{
color="red";
filled=true;
}
Shape(String color,boolean filled)
{
this.color=color;
this.filled=filled;
}
public void setColor(String color)
{
this.color=color;
}
public void setFilled(boolean filled)
{
this.filled=filled;
}
public String getColor()
{
return color;
}
public boolean isFilled()
{
return filled;
}
public String toString()
{
return "Shape[color="+color+",filled="+filled+"]";
}
}
class Circle extends Shape
{
private double radius;
Circle()
{
radius=1.0;
}
Circle(double radius)
{
this.radius=radius;
}
Circle(double radius,String color,boolean filled)
{
super(color,filled);
this.radius=radius;
}
public void setRadius(double radius)
{
this.radius=radius;
}
public double getRadius()
{
return radius;
}
public double getArea()
{
return (pi*radius*radius);
}
public double getPerimeter()
{
//C=2πr
return (2*pi*radius);
}
public String toString()
{
return super.toString()+",Circle[radius="+radius+",Area="+this.getArea()+",Perimeter="+this.getPerimeter()+"]";
}
}
class Rectangle extends Shape
{
private double width;
private double length;
Rectangle()
{
width=1.0;
length=1.0;
}
Rectangle(double width,double length)
{
this.width=width;
this.length=length;
}
Rectangle(double width,double length, String color, boolean filled)
{
super(color,filled);
this.width=width;
this.length=length;
}
public void setLength(double length)
{
this.length=length;
}
public double getLength()
{
return length;
}
public void setWidth(double width)
{
this.width=width;
}
public double getWidth()
{
return width;
}
public double getArea()
{
return (length*width);
}
public double getPerimeter()
{
//P=2(l+w)
return (2*(length*width));
}
public String toString()
{
return super.toString()+",Rectangle[length="+length+",width="+width+",Area="+this.getArea()+",Perimeter="+this.getPerimeter()+"]";
}
}
class Square extends Rectangle
{
private double side;
Square()
{
side=1.0;
}
Square(double side)
{
this.side=side;
}
Square(double side,String color,boolean filled)
{
super(1.0,1.0,color,filled);
this.side=side;
}
public void setSide(double side)
{
this.side=side;
}
public double getSide()
{
return side;
}
public void setWidth(double side)
{
this.setWidth(side);
}
public void setLength(double side)
{
this.setLength(side);
}
public String toString()
{
return super.toString()+",Square[side="+side+"]";
}
}
class Lab7Task3
{
public static void main(String[] args)
{
Circle ci=new Circle(4.5,"red",true);
Rectangle re=new Rectangle(3.4,4.5,"green",false);
Square sq=new Square(6.0,"white",true);
ci.getArea();
ci.getPerimeter();
System.out.println(ci);
System.out.println();
re.getArea();
re.getPerimeter();
System.out.println(re);
System.out.println();
System.out.println(sq);
}
}