Menu
Coddy logo textTech

Recap - Moving Point

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

challenge icon

Challenge

Easy

Create a table named point with two fields:

  • x - the x-coordinate
  • y - 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:

  1. Initial x-coordinate
  2. Initial y-coordinate
  3. Delta x (amount to move horizontally)
  4. 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, 4

Try 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