Menu
Coddy logo textTech

Recap - Robot Assembly

Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 54 of 70.

challenge icon

Challenge

Easy

Let'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 raised and 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 prints Head nods and a :shake() method that prints Head 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:

  1. The robot's name (a string)
  2. A response: yes or no

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 nods

Or if the response is no:

{name} says hello!
right arm raised
right arm lowered
left leg steps forward
right leg steps forward
Head shakes

For 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 nods

Notice 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