Ask Question
27 October, 01:32

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.

+5
Answers (1)
  1. 27 October, 01:49
    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?
Find 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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers