Question
Create a UML diagram for both the class and the Program below. Class Coin public class Coin { private final int HEADS = 0; private
Create a UML diagram for both the class and the Program below.
Class Coin
public class Coin { private final int HEADS = 0; private final int TAILS = 1; public int face; public Coin () { flip(); } public void flip () { face = (int) (Math.random() * 2); } public boolean isHeads () { return (face == HEADS); }
public String toString() { String faceName;
if (face == HEADS) faceName = "Heads"; else faceName = "Tails";
return faceName; } }
Program FlipRace
public class FlipRace { public static void main (String[] args) { //two new classes of the class Coin Coin coin1 = new Coin(); Coin coin2 = new Coin(); //defined variables int count1 = 0, count2 = 0, total = 0; //loops until a coin wins while(true) { total++; if (coin1.isHeads()) { count1++; } if (coin2.isHeads()) { count2++; } //Prints out coins System.out.println(coin1 + " " + coin2); //definition for when coin 1 wins if (count1 == 3 & count2 < 3) { System.out.println("Coin 1 wins!"); System.out.println("It took " + total + " flips"); break; } //definition for when coin 2 wins else if (count2 == 3 & count1 < 3) { System.out.println("Coin 2 wins!"); System.out.println("It took " + total + " flips"); break; } //definition for when the race is tied else if (count2 == 3 & count1 == 3) { System.out.println("Race is tied!"); System.out.println("It took " + total + " flips"); break; } //Keeps flipping coins else { coin1.flip(); coin2.flip(); } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started