PROGRAM:
OUTPUT:
Area- 468.875 Perimeter- 91.5
Area- 55.41769440932395 Perimeter- 26.389378290154262
package main
import (
"fmt"
"math"
)
type Geometry interface {
Area() float64
Perimeter() float64
}
type Rectangle struct {
length float64
width float64
}
type Circle struct {
radius float64
}
/* Rectangle type implementing Geometry interface */
func (r Rectangle) Area() float64 {
return r.length * r.width
}
func (r Rectangle) Perimeter() float64 {
return 2 * (r.length + r.width)
}
/* Circle type implementing Geometry interface */
func (c Circle) Area() float64 {
return math.Pi * c.radius * c.radius
}
//Circumference
func (c Circle) Perimeter() float64 {
return 2 * math.Pi * c.radius
}
func Calculate(g Geometry) {
area := g.Area()
perimeter := g.Perimeter()
fmt.Println("Area-", area, "Perimeter-", perimeter, "\n")
}
func main() {
/* create a variable of type Rectangle and initialize it */
rect := Rectangle{15.50, 30.25}
circ := Circle{4.2}
Calculate(rect)
Calculate(circ)
}
OUTPUT:
Area- 468.875 Perimeter- 91.5
Area- 55.41769440932395 Perimeter- 26.389378290154262
No comments:
Post a Comment