Recap - Shape Builder
Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 16 of 110.
Challenge
EasyLet's build a Rectangle class that brings together all the constructor techniques you've learned in this chapter. You'll create a versatile shape class that can be instantiated in multiple ways.
You'll organize your code into two files:
rectangle.dart: Define aRectangleclass with the following:- Two
final doublefields:widthandheight - Two computed
final doublefields:areaandperimeter - A main constructor that takes
widthandheight, using an initializer list to calculatearea(width × height) andperimeter(2 × (width + height)) - A named constructor
Rectangle.square(double side)that redirects to the main constructor with equal width and height - A named constructor
Rectangle.golden(double width)that creates a rectangle using the golden ratio (height = width × 1.618), redirecting to the main constructor - A constant constructor
Rectangle.unit()that creates a 1×1 rectangle (you'll need a separate const constructor for this) - A
display()method that prints the rectangle's information
- Two
main.dart: Import your rectangle class and create four rectangles:- A custom rectangle with width
4.0and height3.0 - A square with side
5.0 - A golden rectangle with width
10.0 - A unit rectangle using the constant constructor (create it as a compile-time constant)
display()on each rectangle in the order listed above.- A custom rectangle with width
The display() method should print in this exact format:
Rectangle [width]x[height] | Area: [area] | Perimeter: [perimeter]Expected output:
Rectangle 4.0x3.0 | Area: 12.0 | Perimeter: 14.0
Rectangle 5.0x5.0 | Area: 25.0 | Perimeter: 20.0
Rectangle 10.0x16.18 | Area: 161.8 | Perimeter: 52.36
Rectangle 1.0x1.0 | Area: 1.0 | Perimeter: 4.0Try it yourself
import 'rectangle.dart';
void main() {
// TODO: Create a custom rectangle with width 4.0 and height 3.0
// TODO: Create a square with side 5.0
// TODO: Create a golden rectangle with width 10.0
// TODO: Create a unit rectangle using the constant constructor
// (create it as a compile-time constant using 'const')
// TODO: Call display() on each rectangle in the order listed above
}
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