209k views
1 vote
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.

User Elda
by
3.2k points

1 Answer

5 votes

Answer:

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

}

}

Step-by-step explanation:

User Karim Omaya
by
4.0k points