A drawing app keeps rectangles, squares and triangles in one list. Write the shape types so that code holding that list can ask any shape for its name and its perimeter (the length all the way around it) without knowing which kind of shape it is. That is polymorphism: one call, shape.perimeter(), and each kind of shape runs its own version.
Go has no classes and no inheritance, so the common type is an interface: any struct that has its methods is a Shape, with no implements keyword.
- The interface
Shapelists two methods:name() stringreturns the kind of shape as text, andperimeter() intreturns the length around the shape. Rectanglekeeps awidthand aheight;NewRectangle(width, height)builds one. Itsnameis"Rectangle", itsperimeter2 × (width + height).Squarekeeps aside;NewSquare(side)builds one. Itsnameis"Square", itsperimeter4 × side.Trianglekeeps three sidesa,bandc;NewTriangle(a, b, c)builds one. Itsnameis"Triangle", itsperimetera + b + c.
The methods take the struct as a receiver named this, as in the course's other lessons. Then write two functions that get a slice of any shapes, []Shape:
totalPerimeter(shapes)returns the sum of the perimeters of all the shapes.largestShape(shapes)returns thenameof the shape with the largest perimeter. If several shapes share the largest perimeter, the one that comes first wins.
Both functions must work through the interface's methods only: never check which kind a shape is. Then a new kind of shape, added later, works with them unchanged.
The starter code already has every struct, constructor function and method header: declare the interface's methods and the structs' fields, and write the bodies. For each test, the harness reads the shapes, builds them in the order given, calls totalPerimeter and largestShape, and prints the two results on one line, separated by a space.
Example 1:
Input:
NewRectangle(3, 4)
NewSquare(5)
NewTriangle(3, 4, 5)
Output: 46 Square
Explanation: The perimeters are 2 × (3 + 4) = 14, 4 × 5 = 20 and 3 + 4 + 5 = 12. Their total is 46, and the square's 20 is the largest.
Example 2:
Input:
NewRectangle(2, 4)
NewSquare(3)
NewTriangle(4, 4, 4)
Output: 36 Rectangle
Explanation: All three perimeters are 12: 2 × (2 + 4), 4 × 3 and 4 + 4 + 4. On a tie the shape that comes first in the list wins, so the answer is the rectangle.
Example 3:
Input:
NewSquare(1)
NewRectangle(1, 1)
NewTriangle(2, 2, 3)
Output: 15 Triangle
Explanation: The perimeters are 4, 4 and 7, so the last shape, the triangle, is the largest. A 1 by 1 rectangle has the same sides as a square, but it is still a Rectangle: each object answers name() for its own class.
Constraints:
- Every test has between 1 and 100 shapes: the list is never empty.
- Every size (width, height, side, and each side of a triangle) is an integer from 1 to 104.
- A triangle's three sides always make a real triangle: each side is shorter than the other two together.
Note:
Each function looks at every shape once: O(n) time for n shapes, and no extra space.
Free plan: 30 tutor messages. PRO is unlimited.
