Filter Shapes
Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 61 of 70.
Challenge
EasyLet's complete your Shape Manager by adding the ability to filter shapes by type. This is a powerful feature that lets you work with specific kinds of shapes from a mixed collection—perfect for when you need to calculate the total area of just the circles, or list only the rectangles.
You'll add a filtering method to your ShapeCollection that uses the _type field you learned about in the "Checking Type" lesson. Each shape class should identify itself so the collection can filter accordingly.
Here's what you'll build across your files:
Shape.lua: Add a_typefield set to"Shape"in your base class.Rectangle.lua: Add a_typefield set to"Rectangle"so rectangles can be identified.Circle.lua: Add a_typefield set to"Circle"so circles can be identified.ShapeCollection.lua: Add a:filterByType(typeName)method that returns a new table containing only the shapes whose_typematches the given type name.main.lua: Bring everything together—create a Rectangle and two Circles, add all three to your collection, then filter to get only the circles. Print the count of filtered shapes, followed by the area of each filtered shape in the order they were added.
The filter method should return a plain table (not a new ShapeCollection) containing references to the matching shapes. This keeps things simple while demonstrating how type checking enables selective operations on mixed collections.
Input: Four lines—rectangle width, rectangle height, first circle's radius, second circle's radius.
Output: Three lines—the count of filtered shapes (circles only), then the area of the first circle, then the area of the second circle.
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 radius1 = tonumber(io.read())
local radius2 = tonumber(io.read())
-- Create a new Rectangle instance with the given dimensions
local rect = Rectangle:new(width, height)
-- Create two Circle instances with the given radii
local circ1 = Circle:new(radius1)
local circ2 = Circle:new(radius2)
-- Create a new ShapeCollection
local collection = ShapeCollection:new()
-- Add all three shapes to the collection
collection:addShape(rect)
collection:addShape(circ1)
collection:addShape(circ2)
-- TODO: Filter to get only circles
-- local circles = collection:filterByType("Circle")
-- TODO: Print the count of filtered shapes
-- print(#circles)
-- TODO: Print the area of each filtered shape
-- for i = 1, #circles do
-- print(circles[i]:getArea())
-- 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