Menu
Coddy logo textTech

Final OOP Check

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

challenge icon

Challenge

Easy

Let's bring together everything you've learned about OOP in Lua! You'll build a vehicle system that demonstrates inheritance, encapsulation through naming conventions, and mixins for optional capabilities.

You'll organize your code across five files:

  • Vehicle.lua: The base class for all vehicles. Each vehicle has a name and a private-convention _speed (initialized to 0). Include a :getName() getter, a :getSpeed() getter, and a :setSpeed(value) setter that rejects negative values (if negative, print Speed cannot be negative and don't change the value). Add a :describe() method that returns "{name} moving at {speed}".
  • Refuelable.lua: A mixin table containing fuel-related behavior. Include a :refuel(amount) method that adds to self._fuel, and a :getFuel() method that returns the current fuel level.
  • Car.lua: Inherits from Vehicle and uses the Refuelable mixin. The constructor accepts a name and initializes _fuel to 0. Override :describe() to return "{name} moving at {speed}, fuel: {fuel}". Apply the Refuelable mixin to give cars fuel capabilities.
  • Bicycle.lua: Inherits from Vehicle only—no fuel functionality needed. The constructor accepts just a name. Bicycles use the inherited :describe() method without modification.
  • main.lua: Bring everything together! Create a car and a bicycle based on the inputs, demonstrate their different capabilities, and show how they share common vehicle behavior while having distinct features.

You will receive four inputs:

  1. Car name
  2. Bicycle name
  3. Speed to set for both vehicles (a number)
  4. Fuel amount to add to the car (a number)

In your main file:

  1. Create a car and a bicycle with the given names
  2. Set the speed for both vehicles using the third input
  3. Refuel the car with the fourth input
  4. Print the car's description
  5. Print the bicycle's description

For example, if the inputs are Sedan, Mountain Bike, 60, and 45, the output should be:

Sedan moving at 60, fuel: 45
Mountain Bike moving at 60

If the inputs are Sports Car, Road Bike, 120, and 80, the output should be:

Sports Car moving at 120, fuel: 80
Road Bike moving at 120

This challenge showcases how inheritance provides shared behavior (both vehicles can move), mixins add optional capabilities (only cars need fuel), and encapsulation protects data integrity (speed validation). Each technique serves a different purpose in your design!

Try it yourself

-- main.lua: Bring everything together

local Car = require('Car')
local Bicycle = require('Bicycle')

-- Read inputs
local carName = io.read()
local bicycleName = io.read()
local speed = tonumber(io.read())
local fuelAmount = tonumber(io.read())

-- TODO: Create a car with the given name

-- TODO: Create a bicycle with the given name

-- TODO: Set the speed for both vehicles

-- TODO: Refuel the car with the fuel amount

-- TODO: Print the car's description

-- TODO: Print the bicycle's description

All lessons in Object Oriented Programming