Recap - Car Factory
Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 11 of 70.
Challenge
EasyLet'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 yourCarclass 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:
- Model for the first car
- Color for the first car
- Model for the second car
- 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 TruckEach 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
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