Shapes and method overriding
HardClasses15 min
Write a class Shape and two subclasses of it: Rectangle and Circle.
new Shape(name)savesname(a string) in the fieldname.- The method
area()ofShapereturns0. describe()ofShapereturns the text<name>: area <area>, for example'Rectangle: area 6'. It takes the area fromthis.area()and rounds it to 2 digits after the point withMath.round(area * 100) / 100. The number is printed the default JavaScript way:6,3.14,2.5.new Rectangle(width, height)creates a shape with the name'Rectangle'. Itsarea()returnswidth * height.new Circle(radius)creates a shape with the name'Circle'. Itsarea()returnsMath.PI * radius * radius, not rounded.RectangleandCircleuse method overriding: each one has its ownarea()method in the class body (not a property set in the constructor).- Do not write
describe()inRectangleandCircle. They get it fromShape, and for every instance it must show the area of that shape. - Sizes are numbers, 0 or more.
Examples
new Rectangle(2, 3).describe()Returns 'Rectangle: area 6'
new Circle(1).describe()Returns 'Circle: area 3.14'
new Shape('Point').area()Returns 0
Asked at EPAM · Source
Loading the editor...
- Rectangle 2 x 3: area 6 and its describe()
- Circle with radius 1: area PI, describe() rounds to 3.14
- A plain Shape has area 0 and uses its own name
Hidden tests run on Submit