...

/

Solution: Calculate Shape Area

Solution: Calculate Shape Area

Go over how to model shapes and calculate their areas using inheritance.

We'll cover the following...

Solution

Press + to interact
class Shape
end
class Triangle < Shape
attr_accessor :base, :height
def initialize(base, height)
@base = base
@height = height
end
def area
return base * height / 2.0
end
end
class Rectangle < Shape
attr_accessor :width, :length
def initialize(length, width)
@length = length
@width = width
end
def area
return length * width
end
end
class Square < Rectangle
def initialize(length)
super(length, length)
end
end
puts Triangle.new(10, 5).area()
puts Rectangle.new(10, 5).area()
puts Square.new(10).area()

Explanation

  • Line 34: The ...