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 method called energyUsed

96%

114 rated

Answer

In the SmartSwitch class, define a method called energyUsed(int hours) that calculates the energy consumption using the formula provided:

public double energyUsed(int hours) {
    return (powerUsage * hours) / 1000.0;
}

This method accepts the number of hours the device has been powered on and returns the energy used in kilowatt-hours (kWh).

Step 2

3.1.2 Write code for a mutator method called setSwitchStatus

99%

104 rated

Answer

The setSwitchStatus method will set the device's switch status. Define it as follows:

public void setSwitchStatus(boolean status) {
    this.fSwitchStatus = status;
}

This method will receive a Boolean value, setting the switch status accordingly.

Step 3

3.1.3 Write code for a toString method

96%

101 rated

Answer

Define the toString method to return a formatted string of all object attributes:

@Override
public String toString() {
    return String.format("Switch ID: %s\nDevice: %s\nPower usage: %s\nPower status: %s", 
        fSwitchID, fDeviceName, fPowerUsage, determineSwitchStatus());
}

Here, determineSwitchStatus() checks if the device is ON or OFF.

Step 4

3.2.1 Write code to extract the switchID

98%

120 rated

Answer

To extract the switchID from the combo box cmbQ3_2_1 and instantiate the SmartSwitch object, write:

// Extract switch ID
String switchID = (String) cmbQ3_2_1.getSelectedItem();

// Extract the selected device and power usage
String selectedDevice = lstQ3_2_1.getSelectedValue();
String[] parts = selectedDevice.split(" ");

String device = parts[0];
int powerUsage = Integer.parseInt(parts[1]);

// Instantiate the SmartSwitch object
SmartSwitch objSmartSwitch = new SmartSwitch(switchID, device, powerUsage);

This initializes the SmartSwitch object with the proper attributes.

Step 5

3.2.2 Use the radio group to select the switch status

97%

117 rated

Answer

To change the switch status based on the selection from the radio group, implement:

boolean status = radioGroup.isSelected() ? true : false;
objSmartSwitch.setSwitchStatus(status);

// Display the ID and status
String switchStatus = objSmartSwitch.determineSwitchStatus();
richEdit.setText(String.format("%s, %s", switchID, switchStatus));

This updates the switch status and shows it in the rich edit.

Step 6

3.2.3 Append to log.txt

97%

121 rated

Answer

To write a date-time entry in log.txt, the following code should be used:

FileWriter writer = new FileWriter("log.txt", true);
writer.write(String.format("%s, %s, %s, %s\n",
    DateToStr(new Date()), 
    getCurrentTime(),
    objSmartSwitch.getSwitchID(), 
    objSmartSwitch.determineSwitchStatus()));
writer.close();

This appends the required information into the log file.

Step 7

3.2.4 Obtain hours and calculate energy usage

96%

114 rated

Answer

For the power usage calculation, use:

int hours = Integer.parseInt(edtQ3_2_4.getText().toString());
double energyUsed = objSmartSwitch.energyUsed(hours);

// Display the result
richEdit.setText(String.format("Energy used: %.2f kWh", energyUsed));

This retrieves the user-inputted hours and displays the energy used in kilowatt-hours.

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

;