l. Implement a class called Coin that represents a coin that flips A coin should
ID: 3722465 • Letter: L
Question
l. Implement a class called Coin that represents a coin that flips A coin should show a state of face value that can be either head or tail (Hint: you can use 0 for tail and 1 for head). There will be only one constructor that does not accept any arguments and initiate states to zero. The flip0 method should generate a random integer that will be either 0 or 1 for the face value. No setters (mutators) are required in the class but you need to include isHead0 getter the return true if the coin face is head. The last method is show) that display the coin information to the screen.Explanation / Answer
public class Coin {
private int state; //0 - Tail, 1 - Head
public Coin() {
this.state = 0;
}
public int flip() {
if(Math.random() < 0.5) {
this.state = 0;
return 0;
}
else {
this.state = 1;
return 1;
}
}
public boolean isHead() {
return (this.state == 1);
}
public void Show() {
if(this.state == 1)
System.out.println("Head");
else
System.out.println("Tails");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.