Interface for Geometric shapes

Report a typo

Lena wants to create a Go program that takes as an input a certain length and prints the calculated area of a square and a circle.

So far, Lena has created the Square and Circle structs plus the Shape interface. However, she still needs to implement the interface methods that will make her program calculate the area of the Square and Circle structs!

Please help Lena and write the additional required code to implement the interfaces for the Square and Circle structs. Take notice that the mathematical formulas to calculate the areas of those geometric shapes are the following:

Square=Side2Square = Side^2

Circle=πRadius2Circle = π * Radius^2

To solve this task, please use the special math.Pi constant to properly calculate the interface method for the Circle struct.

Sample Input 1:

1.618

Sample Output 1:

Square area: 2.62
Circle area: 2.06
Write a program in Go
package main

import (
"fmt"
"math"
)

// Do not change the type declarations below!
type (
Square struct {
Side float64
}

Circle struct {
Radius float64
}

Shape interface {
Area() float64
}
)

// DO NOT change these lines -- they create the proper output string:
func (s Square) String() string { return fmt.Sprintf("Square area: %.2f", s.Area()) }
func (c Circle) String() string { return fmt.Sprintf("Circle area: %.2f", c.Area()) }

// Implement the interface methods for the 'Square' and 'Circle' structs below:
func (s Square) ? { return ? * ? }
func (c Circle) ? { return math.Pi * ? * ? }

func main() {
// Do NOT change the code below! This reads the input and outputs as required.
var length float64
fmt.Scanln(&length)

for _, shape := range []Shape{Square{length}, Circle{length / 2}} {
___

Create a free account to access the full topic