Recap - Robot Assembly
Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 54 of 70.
Challenge
EasyLet's assemble a Robot from individual component parts! Instead of using inheritance, you'll build a robot by composing it from separate Arm, Leg, and Head objects—each with its own specialized behavior. The robot will coordinate these parts through delegation.
You'll organize your code across five files:
Arm.lua: Create an Arm class with a constructor:new(side)that stores which side it's on (e.g., "left" or "right"). Include a:raise()method that prints{side} arm raisedand a:lower()method that prints{side} arm lowered.Leg.lua: Create a Leg class with a constructor:new(side)that stores the side. Include a:step()method that prints{side} leg steps forward.Head.lua: Create a Head class with a constructor:new(). Include a:nod()method that printsHead nodsand a:shake()method that printsHead shakes.Robot.lua: Create a Robot class whose constructor:new(name)stores the name and creates all the component parts internally: a left arm, a right arm, a left leg, a right leg, and a head. The robot should have these methods that delegate to its parts::greet()— prints{name} says hello!, then raises the right arm, then lowers it:walk()— makes the left leg step, then the right leg step:respond(answer)— if answer is"yes", the head nods; if"no", the head shakes
main.lua: Read inputs for the robot's name and a yes/no response. Create a Robot, have it greet, walk, and then respond based on the input.
You will receive two inputs:
- The robot's name (a string)
- A response:
yesorno
Your output should show the robot coordinating its parts:
{name} says hello!
right arm raised
right arm lowered
left leg steps forward
right leg steps forward
Head nodsOr if the response is no:
{name} says hello!
right arm raised
right arm lowered
left leg steps forward
right leg steps forward
Head shakesFor example, if the inputs are Robo and yes, the output should be:
Robo says hello!
right arm raised
right arm lowered
left leg steps forward
right leg steps forward
Head nodsNotice how the Robot doesn't know how arms raise or legs step—it simply tells each part what to do. Each component is independent and reusable, and the robot acts as a coordinator bringing them all together!
Try it yourself
-- main.lua: Entry point for the Robot composition challenge
local Robot = require('Robot')
-- Read inputs
local name = io.read()
local answer = io.read()
-- TODO: Create a Robot with the given name
-- TODO: Have the robot greet
-- TODO: Have the robot walk
-- TODO: Have the robot respond based on the answer input
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 Hierarchy9Composition & Mixins
Has-A RelationshipDelegationSimple MixinsApplying Multiple MixinsMixins vs InheritanceRecap - Robot Assembly