Recap - Student Grade
Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 18 of 70.
Challenge
EasyLet'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 yourStudentclass with the standard prototype pattern. Each student should store anameand agrade. 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 returnstrueif the student's grade is greater than or equal to the threshold,falseotherwise
- A
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:
- Student's name
- Initial grade
- Passing threshold to check
- 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:
- The student's name
- The student's initial grade
- Whether the student passed with the given threshold (print
trueorfalse) - The student's grade after updating it with the new grade
- 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
trueEmma 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
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