Menu
Coddy logo textTech

Recap - Student Grade

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

challenge icon

Challenge

Easy

Let's build a complete Student class that brings together everything you've learned in this chapter—instance variables, getters, setters, and calculated properties all working together!

You'll organize your code across two files:

  • Student.lua: Define your Student class with the standard prototype pattern. Each student should store a name and a grade. Your class needs:
    • A :new(name, grade) constructor
    • A :getName() getter that returns the student's name
    • A :getGrade() getter that returns the current grade
    • A :setGrade(newGrade) setter that updates the grade
    • A :hasPassed(threshold) method that returns true if the student's grade is greater than or equal to the threshold, false otherwise
  • main.lua: Require your Student module and demonstrate all the functionality by creating a student, checking their status, updating their grade, and verifying the changes.

You will receive four inputs:

  1. Student's name
  2. Initial grade
  3. Passing threshold to check
  4. New grade to set

In your main file, create a student with the given name and initial grade, then print the following on separate lines:

  1. The student's name
  2. The student's initial grade
  3. Whether the student passed with the given threshold (print true or false)
  4. The student's grade after updating it with the new grade
  5. Whether the student passed with the same threshold after the grade update

For example, if the inputs are Emma, 55, 60, and 75, the output should be:

Emma
55
false
75
true

Emma initially had a grade of 55, which didn't meet the passing threshold of 60. After updating her grade to 75, she now passes!

Try it yourself

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

-- Read inputs
local name = io.read()
local initialGrade = tonumber(io.read())
local threshold = tonumber(io.read())
local newGrade = tonumber(io.read())

-- TODO: Create a student with the given name and initial grade

-- TODO: Print the student's name using the getter

-- TODO: Print the student's initial grade using the getter

-- TODO: Check and print whether the student passed with the given threshold

-- TODO: Update the student's grade using the setter

-- TODO: Print the student's new grade

-- TODO: Check and print whether the student passed after the grade update

All lessons in Object Oriented Programming