Ask 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 also provide a single constructor that takes a boolean argument that sets the initial state of the Flip instance.

+2
Answers (1)
  1. 26 March, 14:22
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers