How do I display true if the member variable is off, without changing the type from void to boolean?

oopnoob

I want to print true, in the main method, if the member variable is off. How do I do this without changing the types from void to boolean as off needs to remain void.

The code for the SmartDevice class is here

public class SmartDevice extends Device {

    private boolean member;


    public SmartDevice() {

    }


    @Override
    public void on() {

    }

    @Override
    public void off() {

    }
}

The code where I need the tweek,

public class TestDevice {
    public static void main(String[] args) {

       //smart device
        SmartDevice smartDevice = new SmartDevice();

        //up casting
        Device upCastedSmartDevice = new SmartDevice();

        //down casting
        Device device = new SmartDevice();
        SmartDevice downCastedSmartDevice = (SmartDevice) device;

        //call methods
        smartDevice.on();
        smartDevice.off();

        //passing devices into switch methods
        System.out.println();

        //smart device
        aSwitch.turnOn(smartDevice);
        aSwitch.turnOff(smartDevice);
        System.out.println();

        //down casted smart device
        aSwitch.turnOn(downCastedSmartDevice);
        aSwitch.turnOff(downCastedSmartDevice);
        System.out.println();

        //up casted smart device
        aSwitch.turnOn(upCastedSmartDevice);
        aSwitch.turnOff(upCastedSmartDevice);
        System.out.println();
Joel

There are multiple possibilities to achieve a solution for that problem. 1. Add a Method e.g. isOn in your SmartDevice class (recommended)

public boolean isOn() {
  return member;
}
  1. If you really don't want to modify your SmartDevice class, you could use Reflection. But this does indeed break the contract of your OOP design so I would strongly dissuade doing that.

  2. Print true in your SmartDevice after method off() is being called.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

emberjs {{#if variable }} boolean will not display as true

If Boolean variable true, increment i

How do I increase the clickable area of a pseudo element without changing its display size?

Recursion: How do I keep a changing variable in a for-loop from changing during recursion?

List changing value from boolean "true" to "3"

How do I display a message that tells the user how far off their guess was from the randomly generated number?

how to conditionally declare a type without function, based on boolean variable?

How do I declare a member variable within the constructor that doesn't have a predefined type?

How do I change the 'Type' designation of PDFs without changing the default PDF reader?

How do I get NSJSONSerialization to output a boolean as true or false?

My variable is not changing from true to false, how to fix?

How do I declare a variable with a type from another package in Go?

How do I return a boolean from AsyncTask?

How do I change a Boolean variable's value from a grandchild component in this Vue 3 application?

Changing a boolean in a variable from Identifiable struct

How do I display an alert() off of a google maps 'marker' click?

How do I change 0 values in numeric data type columns to NA, without changing FALSE operators in logical data type columns to NA?

In PowerShell how do I remove a duplicate item from a split path without changing the order?

How do I create a list from an already existing list without the new list changing?

How do I change list2 without changing list1 in from a function in Python?

How do I display the content of the void method java

How do I display the amount without refresh?

How do I have a Python 3 program check if changing a variable's type would cause a crash before trying to change the type?

How do I truncate milliseconds off "Ticks" without converting to datetime?

How do I get decimals values without rounding them off

How do I cut off a decimal without rounding?

How can I update/rebind variable without changing the value?

How do I store a boolean test/expression without Evaluating it?

How to send variable from blade to controller without changing the url