016-001-007
Define Shapes with Abstract Class
Easy
Problem Description
Define Shapes with Abstract Class
In this problem, you will create a program that defines an abstract method getArea() in an abstract class Shape, implements circle area calculation by overriding it in a Circle subclass, and displays the area of a circle with radius 5.0 to 2 decimal places on standard output.
Learning Objective: Understand the basics of abstract classes and abstract methods
Overview
Define an abstract method getArea() that returns the area in the abstract class Shape, and implement the specific area calculation (Math.PI * radius * radius) by overriding it in the Circle class.
Specifications
- Create a Shape class (Shape.java): Declare as
abstract class, definepublic abstract double getArea() - Create a Circle class (Circle.java): Extends Shape, private field
radius, initialize via constructor, overridegetArea() - In Main class, create a Circle with radius 5.0 and display area to 2 decimal places
Input
None
Output Format
Area: 78.54
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up