Ask Question
8 April, 17:26

Define a public class named Flip with a single public instance method called flop that takes no parameters and returns a boolean. It should also provide a single constructor that takes a boolean argument that sets the initial state of the Flip instance.

+3
Answers (1)
  1. 8 April, 18:05
    0
    Flip. java

    public class Flip {

    / / private attribute indicating state

    private boolean state;

    / / constructor taking initial boolean value for the state

    public Flip (boolean state) {

    this. state = state;

    }

    / / method to switch the state and return new state

    public boolean flop () {

    / / changing state to false if it is true, true if it is false

    state = ! state;

    / / returning the new state

    return state;

    }

    / / main method containing code for testing. remove if you don't need this

    public static void main (String[] args) {

    Flip flip = new Flip (true);

    System. out. println (flip. flop ()); / / false

    System. out. println (flip. flop ()); / / true

    System. out. println (flip. flop ()); / / false

    Flip flop = new Flip (false);

    System. out. println (flop. flop ()); / / true

    System. out. println (flop. flop ()); / / false

    System. out. println (flop. flop ()); / / true

    }

    }
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Define a public class named Flip with a single public instance method called flop that takes no parameters and returns a boolean. It should ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers