Grade 12 · Object-oriented programming · Lesson and coding task · about 18 minutes
Open this task in the Delphi workspace →
Real exam questions give methods decisions to make. A method can use IFs, loops — anything — on its own fields:
function TLearner.GetStatus : String;
begin
if GetAverage >= 50 then
Result := fName + ': ' + FloatToStrF(GetAverage, ffFixed, 6, 1) + '% (PASS)'
else
Result := fName + ': ' + FloatToStrF(GetAverage, ffFixed, 6, 1) + '% (FAIL)';
end;
And the final boss pattern: an array of objects. The starter's FormCreate builds four TLearner objects into arrLearners. Loop over them exactly like an array of numbers — each element just happens to be an object:
for i := 1 to 4 do
memOutput.Lines.Add(arrLearners[i].GetStatus);
Your task
Implement GetStatus, then loop over the array to report every learner, count the passes (average ≥ 50) and the distinctions (average ≥ 80).
What you will do
- Implement GetStatus with the PASS/FAIL decision (average >= 50 passes)
- Loop over arrLearners showing each learner's status line
- Count passes and distinctions (>= 80) and end with: Passed: 3 then Distinctions: 1
Hints
- GetStatus is an IF on GetAverage — both branches build the same string, only (PASS)/(FAIL) differs.
- In the loop, arrLearners[i].GetStatus and arrLearners[i].GetAverage work exactly like any method call.
- Initialise both counters to 0 before the loop; add the two summary lines after it.
Model solution
type
TLearner = class
private
fName : String;
fMark1, fMark2 : Integer;
public
constructor Create(sName : String; iM1, iM2 : Integer);
function GetAverage : Real;
function GetStatus : String;
end;
constructor TLearner.Create(sName : String; iM1, iM2 : Integer);
begin
fName := sName;
fMark1 := iM1;
fMark2 := iM2;
end;
function TLearner.GetAverage : Real;
begin
Result := (fMark1 + fMark2) / 2;
end;
function TLearner.GetStatus : String;
begin
if GetAverage >= 50 then
Result := fName + ': ' + FloatToStrF(GetAverage, ffFixed, 6, 1) + '% (PASS)'
else
Result := fName + ': ' + FloatToStrF(GetAverage, ffFixed, 6, 1) + '% (FAIL)';
end;
var
arrLearners : array[1..4] of TLearner;
procedure TfrmClass.FormCreate(Sender: TObject);
begin
arrLearners[1] := TLearner.Create('Thabo', 78, 71);
arrLearners[2] := TLearner.Create('Anele', 90, 85);
arrLearners[3] := TLearner.Create('Pieter', 55, 40);
arrLearners[4] := TLearner.Create('Fatima', 82, 77);
end;
procedure TfrmClass.btnReportClick(Sender: TObject);
var
i, iPassed, iDistinctions : Integer;
begin
iPassed := 0;
iDistinctions := 0;
for i := 1 to 4 do
begin
memOutput.Lines.Add(arrLearners[i].GetStatus);
if arrLearners[i].GetAverage >= 50 then
Inc(iPassed);
if arrLearners[i].GetAverage >= 80 then
Inc(iDistinctions);
end;
memOutput.Lines.Add('Passed: ' + IntToStr(iPassed));
memOutput.Lines.Add('Distinctions: ' + IntToStr(iDistinctions));
end;