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; }

}

  1. Should the isHeads instance variable be public, private, or protected? Why?

  2. What should happen in the constructor for this class? Explain why, including any pros and cons of decisions you make.

  3. 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!