/
polygon.rb
61 lines (60 loc) · 984 Bytes
/
polygon.rb
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
class Polygon
@@first=ARGV[0].to_i
@@sec=ARGV[1].to_i
@@third=ARGV[2].to_i
# puts "#@@first #@@sec #@@third"
def printperi
puts "perimieter"
end
def printarea
puts "area"
end
end
class Triangle<Polygon
def printperi
@peri=@@first+@@sec+@@third
puts "perimieter is is #@peri"
end
def printarea
@s=(@@first+@@sec+@@third).to_f/2
@area=(Math.sqrt(@s*(@s-@@first)*(@s-@@sec)*(@s-@@third))).to_f
puts "area is #@area"
end
end
class Square<Polygon
def printperi
@peri=4*@@first
puts "perimieter is #@peri"
end
def printarea
@area=@@first*@@first
puts "area is #@area"
end
end
class Rectangle<Polygon
def printperi
@peri=2*(@@first+@@sec)
puts "perimietre is #@peri"
end
def printarea
@area=@@first*@@sec
puts "area is #@area"
end
end
p=Polygon.new
len=ARGV.length;
if len==3
t=Triangle.new
t.printarea;
t.printperi;
end
if len==2
t=Rectangle.new
t.printarea;
t.printperi;
end
if len==1
t=Square.new
t.printarea;
t.printperi;
end