Recap - Moving Point
Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 5 of 70.
Challenge
EasyCreate a table named point with two fields:
x- the x-coordinatey- the y-coordinate
Add a method :move(dx, dy) using the colon syntax that adds dx to self.x and dy to self.y.
You will receive four inputs:
- Initial x-coordinate
- Initial y-coordinate
- Delta x (amount to move horizontally)
- Delta y (amount to move vertically)
After creating the point with the initial coordinates and calling :move() with the delta values, print the final position in this exact format:
[x], [y]For example, if the initial position is (3, 5) and you move by (2, -1), the output should be:
5, 4Try it yourself
-- Read inputs
local initial_x = tonumber(io.read())
local initial_y = tonumber(io.read())
local dx = tonumber(io.read())
local dy = tonumber(io.read())
-- TODO: Write your code below
-- 1. Create a table named 'point' with fields x and y (using initial coordinates)
-- 2. Add a :move(dx, dy) method using colon syntax that modifies self.x and self.y
-- Call the move method and print the result
point:move(dx, dy)
print(point.x .. ", " .. point.y)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