Challenge: Abstraction
Task
An abstract class called Shape has been created for you. It contains two abstract methods: calculateArea() and calculatePerimeter(). Your task is to implement two classes: Rectangle and Circle. You need to inherit these classes from the abstract class Shape and override its methods.
Here's a brief guide:
-
For the
Circleclass, you will need fields such asradius, and for theRectangleclass, you will need fields likewidthandheight. These fields and their constructors are already provided for you. Use these fields when overriding the methods. -
If you don't have strong geometry knowledge, here's a quick reference with formulas you need to use:
- Circle Area: Pi * radius * radius;
- Circle Perimeter: 2 * PI * radius;
- Rectangle Area: width * height;
- Rectangle Perimeter: 2 * (width + height).
To use the value of Ο (PI), make use of the Math library. You can use Math.PI to access the value of Ο.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain how to use the @Override annotation in this context?
What does the abstract class Shape look like?
Can you show an example of how to create and use a Rectangle or Circle object?
Awesome!
Completion rate improved to 4.76
Challenge: Abstraction
Swipe to show menu
Task
An abstract class called Shape has been created for you. It contains two abstract methods: calculateArea() and calculatePerimeter(). Your task is to implement two classes: Rectangle and Circle. You need to inherit these classes from the abstract class Shape and override its methods.
Here's a brief guide:
-
For the
Circleclass, you will need fields such asradius, and for theRectangleclass, you will need fields likewidthandheight. These fields and their constructors are already provided for you. Use these fields when overriding the methods. -
If you don't have strong geometry knowledge, here's a quick reference with formulas you need to use:
- Circle Area: Pi * radius * radius;
- Circle Perimeter: 2 * PI * radius;
- Rectangle Area: width * height;
- Rectangle Perimeter: 2 * (width + height).
To use the value of Ο (PI), make use of the Math library. You can use Math.PI to access the value of Ο.
Thanks for your feedback!