Menu
Coddy logo textTech

Recap - Car Factory

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

challenge icon

Challenge

Easy

Let's build a complete Car class that brings together everything you've learned in this chapter! You'll create a car factory system where each car has its own model and color, demonstrating proper module organization and independent instances.

You'll organize your code across two files:

  • Car.lua: Define your Car class following the standard prototype pattern. Your class needs a :new(model, color) constructor that creates cars with their own attributes. Include a :describe() method that lets each car introduce itself.
  • main.lua: Require your Car module and create multiple car instances to demonstrate that each car maintains its own independent state.

Your :describe() method should print in this exact format:

A [color] [model]

You will receive four inputs:

  1. Model for the first car
  2. Color for the first car
  3. Model for the second car
  4. Color for the second car

In your main file, create two cars using the provided inputs and have each one describe itself on a separate line.

For example, if the inputs are Sedan, blue, Truck, and red, the output should be:

A blue Sedan
A red Truck

Each car should store its own model and color, proving that your class creates truly independent instances.

Try it yourself

-- Require the Car module
local Car = require('Car')

-- Read inputs
local model1 = io.read()
local color1 = io.read()
local model2 = io.read()
local color2 = io.read()

-- TODO: Create two car instances using Car:new()
-- TODO: Call :describe() on each car to print their descriptions

All lessons in Object Oriented Programming