Recap - Shape Calculator
Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 49 of 110.
Challenge
EasyLet's build a shape calculator system that brings together abstract classes and interfaces to create a flexible geometry toolkit. You'll design a class hierarchy where different shapes share common behavior through abstraction while each shape provides its own unique calculations.
You'll organize your code into three files:
shape.dart: Define your abstract base class and interface here:- An abstract class
Shapewith aString nameproperty and constructor. Include abstract methodsdouble calculateArea()anddouble calculatePerimeter(). Add a regular methoddescribe()that prints[name] - Area: [calculateArea()], Perimeter: [calculatePerimeter()] - A
Drawableclass with a methoddraw()that printsDrawing shape...
- An abstract class
shapes.dart: Importshape.dartand create your concrete shape classes:- A
Rectangleclass that extendsShapeand implementsDrawable. Adddouble widthanddouble heightproperties. Calculate area aswidth * heightand perimeter as2 * (width + height). Thedraw()method should printDrawing rectangle [width]x[height] - A
Circleclass that extendsShapeand implementsDrawable. Add adouble radiusproperty. Use3.14159for pi. Calculate area aspi * radius * radiusand perimeter (circumference) as2 * pi * radius. Thedraw()method should printDrawing circle with radius [radius]
- A
main.dart: Importshapes.dartand demonstrate your shape calculator:- Create a
Rectanglenamed'Rectangle'with width5.0and height3.0 - Create a
Circlenamed'Circle'with radius4.0 - For each shape, call
draw()thendescribe() - Print an empty line between the two shapes
- Create a
This challenge demonstrates how abstract classes define shared structure (every shape has area and perimeter calculations), while interfaces add additional capabilities (shapes that can be drawn). Each concrete class extends the abstract class for its core identity and implements the interface for extra functionality.
Expected output:
Drawing rectangle 5.0x3.0
Rectangle - Area: 15.0, Perimeter: 16.0
Drawing circle with radius 4.0
Circle - Area: 50.26544, Perimeter: 25.13272Try it yourself
import 'shapes.dart';
void main() {
// TODO: Create a Rectangle named 'Rectangle' with width 5.0 and height 3.0
// TODO: Create a Circle named 'Circle' with radius 4.0
// TODO: For the rectangle, call draw() then describe()
// TODO: Print an empty line
// TODO: For the circle, call draw() then describe()
}
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesLibraries & ImportsIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsInstance VariablesConstructor BasicsRecap - Simple Calculator4Null Safety
Intro to Null SafetyNullable vs Non-NullableThe ? and ! OperatorsLate Keyword & Null SafetyNull-Aware OperatorsNull Safety in ClassesRecap - User Profile System7Abstract Classes & Interfaces
Abstract ClassesAbstract MethodsInterfaces in DartImplicit InterfacesImplementing vs ExtendingMultiple InterfacesRecap - Shape Calculator10Collections & Generics
List, Set, Map OverviewType-Safe CollectionsGeneric ClassesGeneric MethodsGeneric ConstraintsIterable & IteratorRecap - Generic Storage13Advanced OOP Concepts
Composition vs InheritanceExtension MethodsCallable ClassesSealed Classes (Dart 3)Records (Dart 3)Patterns & Matching (3.0)Enums with Methods16Project: Library Management
Project OverviewBook and User Classes2Constructors in Dart
Default ConstructorNamed ConstructorsInitializer ListsConstant ConstructorsFactory ConstructorsRedirecting ConstructorsRecap - Shape Builder5Encapsulation
Public vs Private MembersThe _ Prefix ConventionLibrary-Level PrivacyGetters & Setters DepthInformation HidingRecap - Student Records8Mixins
Introduction to MixinsCreating MixinsUsing Multiple Mixinson Keyword in MixinsMixin vs InheritanceMixin vs InterfaceRecap - Animal System11Special Methods
toString() OverridehashCode & == OverrideComparable Interfacecall() MethodnoSuchMethod OverrideRecap - Custom Collection14Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternObserver PatternStrategy Pattern3Class Properties
Instance vs Static MembersFinal & Const FieldsLate VariablesStatic Methods & FieldsGetters and SettersRecap - Bank Account Manager6Inheritance
Basic InheritanceThe super KeywordMethod OverridingThe @override AnnotationThe final Class KeywordConstructors & InheritanceRecap - Employee Hierarchy9Polymorphism
Polymorphism BasicsPolymorphism via InterfacesRuntime Type CheckingThe is & as OperatorsCovariant KeywordRecap - Payment Processor