Question: Java 1. Here is a Coin class that is designed to represent a coin that can be tossed to come up with heads or tails,
Java
1. Here is a Coin class that is designed to represent a coin that can be tossed to come up with heads or tails, 50/50 chance each.
public class Coin { bool isHeads; // true if the coin is currently heads up, false otherwise.
public Coin() { } public void flip() { if (Math.random() >= 0.5)
isHeads = true; else {
isHeads = false; }
}
-
Should the isHeads instance variable be public, private, or protected? Why?
-
What should happen in the constructor for this class? Explain why, including any pros and cons of decisions you make.
-
Should there be a getter for isHeads? Why or why not? If so, should it be public or private? Should there be a setter for isHeads? Why/Why not?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
