What's new

Help PHC BOT

Yayann

Honorary Poster
Established
Joined
Mar 23, 2022
Posts
338
Reaction
94
Points
144
Do the following instruction using java.
1. Create a class called LightSwitch
2. Create a private Boolean property in LigthSwitch called Light
3. Add a public method in the LightSwitch class called isON
4. In the isON method make the Light property to TRUE
5. Add another public method in the LightSwitch class called isOFF
6. In the isOFF method make the Light property to FALSE
7. Create a public method in the LightSwitch called LightStatus that returns the value of the property Light
8. Create a new class called Switch extend the Class LightSwitch
9. Create constructor on the Switch class that would write the return value of the method LightStatus - System.out.println(LightStatus());
10. Add another command that would call the isON() method
11. Add another command that would write the return value of the method LightStatus - System.out.println(LightStatus());
12. Add another command that would call the isOFF() method
13. Add another command that would write the return value of the method LightStatus - System.out.println(LightStatus());
14. finally in the main method of the Switch class instantiate the Switch class - new Switch();
 
Sure! Here's the implementation of the instructions using Java:

Java:
// Step 1: Create a class called LightSwitch
class LightSwitch {
    // Step 2: Create a private Boolean property called Light
    private boolean Light;

    // Step 3: Add a public method called isON
    public void isON() {
        // Step 4: Set the Light property to TRUE
        Light = true;
    }

    // Step 5: Add another public method called isOFF
    public void isOFF() {
        // Step 6: Set the Light property to FALSE
        Light = false;
    }

    // Step 7: Create a public method called LightStatus that returns the value of the property Light
    public boolean LightStatus() {
        return Light;
    }
}

// Step 8: Create a new class called Switch extending the LightSwitch class
class Switch extends LightSwitch {
    // Step 9: Create a constructor that prints the return value of the method LightStatus
    public Switch() {
        System.out.println(LightStatus());
    }

    public static void main(String[] args) {
        // Step 14: Instantiate the Switch class
        new Switch().isON(); // Step 10: Call the isON() method
        System.out.println(new Switch().LightStatus()); // Step 11: Print the return value of LightStatus
        new Switch().isOFF(); // Step 12: Call the isOFF() method
        System.out.println(new Switch().LightStatus()); // Step 13: Print the return value of LightStatus
    }
}

When you run the main method in the Switch class, it will instantiate the Switch class, call the isON() method, print the return value of LightStatus(), call the isOFF() method, and print the final return value of LightStatus().
 

Similar threads

Back
Top