Photo AI

Smart switches have been installed to monitor and automate name devices - NSC Information Technology - Question 3 - 2023 - Paper 1

Question icon

Question 3

Smart-switches-have-been-installed-to-monitor-and-automate-name-devices-NSC Information Technology-Question 3-2023-Paper 1.png

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

Answer

To create a constructor for the TSmartSwitch class, define the method as follows:

constructor TSmartSwitch.Create(aSwitchID: Integer; aDevice: String; aPowerUsage: Double);
begin
  fSwitchID := aSwitchID;
  fDevice := aDevice;
  fPowerUsage := aPowerUsage;
  fSwitchStatus := False;
end;

Step 2

3.1.3 Write code for the function energyUsed

99%

104 rated

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

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

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

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

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

Answer

To implement the writing to the file:

var
  logFile: TextFile;
begin
  AssignFile(logFile, 'log.txt');
  Append(logFile);
  WriteLn(logFile, Format('%s, %s, Switch ID: %d, Status: %s', [DateToStr(Date), TimeToStr(Time), objSmartSwitch.getSwitchID, objSmartSwitch.determineSwitchStatus()]));
  CloseFile(logFile);
end;

Step 8

3.2.4 Power usage calculation

99%

104 rated

Answer

To calculate power usage when the user inputs the number of hours:

var
  hours: Integer;
begin
  hours := StrToInt(edtQ3_2.Text);
  ShowMessage('Energy used: ' + FloatToStr(objSmartSwitch.energyUsed(hours)) + ' kWh');
end;

Join the NSC students using SimpleStudy...

97% of Students

Report Improved Results

98% of Students

Recommend to friends

100,000+

Students Supported

1 Million+

Questions answered

;