Smart switches have been installed to monitor and automate name devices - NSC Information Technology - Question 3 - 2023 - Paper 1
Question 3
Smart switches have been installed to monitor and automate name devices. The program to be developed will determine the device power usage of the switch and the user... show full transcript
Worked Solution & Example Answer:Smart switches have been installed to monitor and automate name devices - NSC Information Technology - Question 3 - 2023 - Paper 1
Step 1
3.1.1 Write code for a constructor method
96%
114 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
To create a constructor for the TSmartSwitch class, define the method as follows:
Sign up now to view full answer, or log in if you already have an account!
Answer
The energyUsed method calculates the energy used based on the number of hours the device is on:
function TSmartSwitch.energyUsed(hours: Integer): Double;
begin
Result := fPowerUsage * hours / 1000; // Converting to kWh
end;
Step 3
3.1.4 Write code for the procedure setSwitchStatus
96%
101 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
To implement the setSwitchStatus method, use the following code:
procedure TSmartSwitch.setSwitchStatus(status: Boolean);
begin
fSwitchStatus := status;
end;
Step 4
3.1.5 Write code for the function toString
98%
120 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
To create the toString method for the TSmartSwitch class:
function TSmartSwitch.toString: String;
begin
Result := Format('Switch ID: %d
Device: %s
Power usage: %.2f W
Switch status: %s',
[fSwitchID, fDevice, fPowerUsage, determineSwitchStatus()]);
end;
Step 5
3.2.1 Write code to instantiate the object
97%
117 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
To instantiate the object from the combo box:
var
iSwitchID: Integer;
sDevice: String;
iPowerUsage: Integer;
begin
iSwitchID := cmbQ3_2.ItemIndex;
sDevice := lstQ3_2.Items[iSwitchID];
// Extract power usage from sDevice
// ... (code to extract)
objSmartSwitch := TSmartSwitch.Create(iSwitchID, sDevice, iPowerUsage);
// Display objSmartSwitch.toString in rcdQ3_2
end;
Step 6
3.2.2 Use the selected value from the radio group
97%
121 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
To set the switch status based on the radio group:
if rgrpQ3_2.ItemIndex = 0 then
objSmartSwitch.setSwitchStatus(True)
else
objSmartSwitch.setSwitchStatus(False);
// Display results
rcdQ3_2.Lines.Add(IntToStr(objSmartSwitch.getSwitchID) + ' - Status: ' + objSmartSwitch.determineSwitchStatus());
Step 7
3.2.3 Write to the file
96%
114 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!