๐Shapes Calculator LabLAB
Shapes Calculator Lab
In this lab you will build a small geometry calculator by defining a Shape interface and implementing it for three concrete types.
The interface
Shapes to implement
Circle
- Area:
math.Pi * r * r - Perimeter (circumference):
2 * math.Pi * r
Rectangle
- Area:
Width * Height - Perimeter:
2 * (Width + Height)
Triangle โ using Heron's formula
- Perimeter:
A + B + C - Area (Heron's formula):
- Compute the semi-perimeter:
s = (A + B + C) / 2 - Area =
math.Sqrt(s * (s-A) * (s-B) * (s-C))
- Compute the semi-perimeter:
Helper functions
TotalArea(shapes []Shape) float64
Returns the sum of Area() for every shape in the slice.
Describe(s Shape)
Prints: Shape: <String()>, Area: <area>, Perimeter: <perimeter>
Use fmt.Printf with %.2f for the numeric values.
In main
Create and describe three shapes, then print the total area:
Expected output
โ Enter (Mac) ยท Ctrl+Enter (Win/Linux)
โ Enter (Mac) ยท Ctrl+Enter (Win/Linux)