Learn AP Comp Sci

Problem of the Day

Daily problems are published Monday - Friday during the school year, September - June.

In the meantime, here's one from the archives.


The class ElectronicDevice has a String instance variable name to describe the device, as well as a boolean instance variable turnedOn to describe whether the device is "on" or "off"—for a device that is "on," the variable turnedOn is true. The following public methods have been defined for the class:

  • String getName()
  • void turnOn()
  • void turnOff()
  • boolean isTurnedOn()
  • Based on the method names, which would presumably be accessor methods, and which would be mutator methods?

     Accessor methodsMutator methods
    a.getName()turnOn(), turnOff(), isTurnedOn()
    b.turnOn(), turnOff()getName(), isTurnedOn()
    c.getName(), isTurnedOn()turnOn(), turnOff()
    d.turnOn(), turnOff(), isTurnedOn()getName()

    Show solution:

    The correct answer is c. The methods getName and isTurnedOn both return the value of instance variables that indicate the current state of the electronic device. They are "getter" methods that get information from the object, and don't that information in any way. The methods turnOn and turnOff, on the other hand, each modify (or "mutate") the state of the device—they are "setter" methods that set a new value for an instance variable.