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

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!