Kotlinlearncs.online LogoJava
Return to List

Solve: Toggler Object

Created By: Geoffrey Challen
/ Version: 2021.9.0

Define a class named Toggler with a single instance method named toggle that takes no parameters and returns a Boolean. Toggler should also provide a primary constructor that accepts a Boolean argument and sets the initial state of the Toggler instance.

Toggler maintains one piece of private state: the Boolean. Calling toggle changes the Boolean from true to false or false to true and returns the new (not the old) state of the Boolean. So, for example:

Note that the internal state should be private.

Related Lessons

Stuck? You may find these lessons helpful: