-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapes.rb
93 lines (72 loc) · 1.51 KB
/
shapes.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
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
class Shape
def initialize(size_of_base, height, radius)
@size_of_base = size of base
@height = height
@radius = radius
end
end
class Triangle < Shape
def initialize(size_of_base, height)
@size_of_base = size_of_base
@height = height
end
def area
@size_of_base * @height / 2
end
def name
"triangle"
end
end
class Circle < Shape
def initialize(radius)
@radius = radius
end
def area
@radius * @radius * Math::PI
end
def name
"circle"
end
end
class Square < Shape
def initialize(size_of_base)
@size_of_base = size_of_base
end
def area
@size_of_base * @size_of_base
end
def name
"square"
end
end
puts "What shape do you want? (1 = triangle/ 2 = circle/ 3 = square)"
shape = gets
puts "You chose " + shape
shape = shape.to_i
if shape == 1 || shape == 3
puts "What is the size of the base?"
size_of_base = gets
size_of_base = size_of_base.to_i
end
if shape == 1
puts "What is the size of the height?"
height = gets
height = height.to_i
end
if shape == 2
puts "What is the radius?"
radius = gets
radius = radius.to_i
end
if shape == 1
shape_object = Triangle.new(size_of_base, height)
puts "The area of your #{shape_object.name} is #{shape_object.area}"
end
if shape == 2
shape_object = Circle.new(radius)
puts "The area of your #{shape_object.name} is #{shape_object.area}"
end
if shape == 3
shape_object = Square.new(size_of_base)
puts "The area of your #{shape_object.name} is #{shape_object.area}"
end