Hone logo
Hone
Problems

Shape Area Calculator with Interfaces

This challenge focuses on implementing interfaces in Go to achieve polymorphism. You'll design a system where different shapes (Circle, Rectangle, Triangle) can be treated uniformly through a common interface, allowing for flexible area calculation. This demonstrates a core principle of object-oriented programming and promotes code reusability.

Problem Description

You are tasked with creating a system that can calculate the area of various shapes. To achieve this, you should define an interface called Shape with a single method Area() float64. Then, implement this interface for three specific shapes: Circle, Rectangle, and Triangle. Each shape struct should have its own relevant dimensions (radius for Circle, width and height for Rectangle, base and height for Triangle). Your program should then be able to take a slice of Shape interfaces and calculate the total area of all shapes in the slice.

Key Requirements:

  • Define the Shape interface with the Area() float64 method.
  • Create Circle, Rectangle, and Triangle structs.
  • Implement the Shape interface for each of the three structs.
  • Write a function CalculateTotalArea(shapes []Shape) float64 that takes a slice of Shape interfaces and returns the sum of their areas.
  • The Area() method should return the correct area for each shape.
  • Handle potential errors gracefully (though explicit error handling isn't strictly required for this challenge, consider how you might handle invalid dimensions in a real-world scenario).

Expected Behavior:

The CalculateTotalArea function should iterate through the slice of Shape interfaces, call the Area() method on each shape, and sum the returned areas. The function should return the total area as a float64.

Edge Cases to Consider:

  • An empty slice of shapes. The function should return 0.0 in this case.
  • Shapes with zero or negative dimensions (though the problem doesn't explicitly require error handling, consider how this might be handled in a production environment).

Examples

Example 1:

Input: []Shape{Circle{Radius: 5}, Rectangle{Width: 4, Height: 6}, Triangle{Base: 3, Height: 8}}
Output: 101.53981633974483
Explanation: Circle area = π * 5^2 ≈ 78.54, Rectangle area = 4 * 6 = 24, Triangle area = 0.5 * 3 * 8 = 12. Total area = 78.54 + 24 + 12 = 114.54

Example 2:

Input: []Shape{}
Output: 0.0
Explanation: The slice is empty, so the total area is 0.

Example 3:

Input: []Shape{Circle{Radius: 2.5}, Rectangle{Width: 1, Height: 1}}
Output: 20.10609906494845
Explanation: Circle area = π * 2.5^2 ≈ 19.63, Rectangle area = 1 * 1 = 1. Total area = 19.63 + 1 = 20.63

Constraints

  • The radius of a Circle must be a non-negative number.
  • The width and height of a Rectangle must be non-negative numbers.
  • The base and height of a Triangle must be non-negative numbers.
  • The CalculateTotalArea function must have a time complexity of O(n), where n is the number of shapes in the input slice.
  • All area calculations should use math.Pi for accurate results.

Notes

  • Remember to import the math package for using math.Pi.
  • The interface allows you to treat different shape types uniformly. This is a key benefit of using interfaces.
  • Consider how you might extend this system to support more shapes in the future. The interface-based design makes this easy.
  • Focus on the correct implementation of the Area() method for each shape. The CalculateTotalArea function is relatively straightforward once the interface is implemented correctly.
Loading editor...
go