Question: Using UML notation, draw the class diagram in the space below (you class diagram must match your code, your class diagram should contain TestCoin, Coin,
Using UML notation, draw the class diagram in the space below (you class diagram must match your code, your class diagram should contain TestCoin, Coin, MonetaryCoin and the relationships among them.
public class TestCoin { public static void main(String[] args) { int totalHeads = 0; int totalValue = 0; MonetaryCoin mCoin = new MonetaryCoin(1); mCoin.flip(); System.out.println(mCoin); if(mCoin.isHeads()) { totalHeads += 1; } totalValue += mCoin.getValue();
mCoin = new MonetaryCoin(10); mCoin.flip(); System.out.println(mCoin); if(mCoin.isHeads()) { totalHeads += 1; } totalValue += mCoin.getValue(); mCoin = new MonetaryCoin(2); mCoin.flip(); System.out.println(mCoin); if(mCoin.isHeads()) { totalHeads += 1; } totalValue += mCoin.getValue(); mCoin = new MonetaryCoin(3); mCoin.flip(); System.out.println(mCoin); if(mCoin.isHeads()) { totalHeads += 1; } totalValue += mCoin.getValue(); System.out.println("The total heads is " + totalHeads); System.out.println("The total value is " + totalValue); } }
public class MonetaryCoin extends Coin { private int value; public MonetaryCoin(){ value = 0; } public MonetaryCoin(int aValue){ value = aValue; } public int getValue(){ return value; } public String toString(){ String faceName; if (isHeads()) { faceName = "head"; } else { faceName = "tail"; } return "The coin has "+faceName+" and value is "+value+"."; } }
public class Coin { private final int HEADS = 0; private final int TAILS = 1; private 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; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
