Shape Collection
Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 59 of 70.
Challenge
EasyNow that you have both Rectangle and Circle classes with full functionality, let's create a ShapeCollection class that can manage multiple shapes together. This is a great example of composition—the collection "has" shapes rather than "being" a shape.
You'll add a new file to your growing project:
Shape.lua: Your base Shape class (unchanged).Rectangle.lua: Your Rectangle class with:getArea()and:getPerimeter().Circle.lua: Your Circle class with:getArea()and:getPerimeter().ShapeCollection.lua: Create a new class that stores shapes internally. Include a:new()constructor that initializes an empty list of shapes, an:addShape(shape)method that adds any shape to the collection, and a:count()method that returns how many shapes are currently stored.main.lua: Bring everything together by requiring your modules. Read dimensions from input, create a Rectangle and a Circle, add both to a ShapeCollection, then print the collection's count followed by each shape's name (in the order they were added).
The ShapeCollection doesn't need to know whether it's storing rectangles or circles—it just holds shapes. This demonstrates how polymorphism lets you treat different object types uniformly through a common interface.
Input: Three lines—width (number), height (number), and radius (number).
Output: Three lines—the count of shapes in the collection, then the name of the first shape added, then the name of the second shape added.
Try it yourself
-- Require the Rectangle module
local Rectangle = require('Rectangle')
-- Require the Circle module
local Circle = require('Circle')
-- Require the ShapeCollection module
local ShapeCollection = require('ShapeCollection')
-- Read width and height from input
local width = tonumber(io.read())
local height = tonumber(io.read())
local radius = tonumber(io.read())
-- Create a new Rectangle instance with the given dimensions
local rect = Rectangle:new(width, height)
-- Create a new Circle instance with the given radius
local circ = Circle:new(radius)
-- Create a new ShapeCollection
local collection = ShapeCollection:new()
-- Add both shapes to the collection
collection:addShape(rect)
collection:addShape(circ)
-- Print the count of shapes in the collection
print(collection:count())
-- Print the name of each shape in the order they were added
for i = 1, collection:count() do
print(collection.shapes[i]:getName())
end
All lessons in Object Oriented Programming
1The 'Self' Concept
Tables with FunctionsExplicit 'self'The Colon SyntaxDot vs ColonRecap - Moving Point4Project: Digital Bank
Project SetupDeposit Method7Polymorphism & Overriding
Overriding MethodsCalling Parent MethodsDuck TypingCommon InterfaceChecking TypeRecap - Employee Roles10Project: Shape Manager
Project SetupRectangle Class2Class Prototype Pattern
The Prototype ConceptLinking with __indexThe :new() ConstructorInitializing AttributesIndependent InstancesRecap - Car Factory5Operator Overloading in OOP
Adding ObjectsSubtracting ObjectsConcatenating ObjectsComparing Objects (<, >)Recap - Wallet Math8Encapsulation
Naming ConventionsClosures for PrivacyAccess via ClosuresRead-Only TablesValidation LogicRecap - Secure Vault11Design Patterns (Lite)
Factory FunctionsSingleton TableIterator PatternObserver (Listener)Recap - Logger Factory3Object State and Behavior
Instance VariablesGetter MethodsSetter MethodsCalculated PropertiesFormatting StringsEquality ChecksRecap - Student Grade6Inheritance Basics
The Inheritance SetupInheriting MethodsExtending the ConstructorAdding Child MethodsShared vs UniqueRecap - Shape Hierarchy