Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please write in Java 2. Design and implement a class called MonetaryCoin that is

ID: 3735131 • Letter: P

Question

Please write in Java

2. Design and implement a class called MonetaryCoin that is derived from the Coin class presented in Chapter 5 (see below) . Store a value in the monetary coin that represents its value, add getter and setter methods for the monetary value as well as a toString method.

3. Create a main driver class to instantiate 25 monetary coins (use an array for this) with random monetary values. The driver flips all the coins, computes and prints to the screen the average monetary value of all coins with TAILS face.

//******************************************************************** // Coin.java Author: Lewis/Loftus // // Solution to Programming Project 5.6 // // Represents a coin with two sides that can be flipped. //******************************************************************** public class Coin { private final int HEADS = 0; private final int TAILS = 1; private int face; //----------------------------------------------------------------- // Sets up the coin by flipping it initially. //----------------------------------------------------------------- public Coin () { flip(); } //----------------------------------------------------------------- // Flips the coin by randomly choosing a face value. //----------------------------------------------------------------- public void flip () { face = (int) (Math.random() * 2); } //----------------------------------------------------------------- // Returns true if the current face of the coin is heads. //----------------------------------------------------------------- public boolean isHeads () { return (face == HEADS); } //----------------------------------------------------------------- // Returns the current face of the coin as a string. //----------------------------------------------------------------- public String toString() { String faceName; if (face == HEADS) faceName = "Heads"; else faceName = "Tails"; return faceName; } }

Explanation / Answer

TestCoin.java

public class TestCoin {

public static void main(String[] args) {

int tailCount = 0, headCount = 0;

for(int i=0; i<25; i++){

MonetaryCoin myCoin = new MonetaryCoin();

myCoin.flip();

System.out.println(myCoin.toString());

if(myCoin.isHeads()){

headCount++;

System.out.println("The coin has head and you win.");

}else{

tailCount++;

System.out.println("The coin has tail and you lose.");

}

}

System.out.println("Number of Heads: "+headCount);

System.out.println("Number of Tails: "+tailCount );

System.out.println("Average: "+(tailCount*100)/25);

}

}

MonetaryCoin.java

public class MonetaryCoin extends Coin {

int value;

public MonetaryCoin(){

super();

}

public MonetaryCoin(int aValue){

value = aValue;

}

public int getValue() {

return value;

}

public void setValue(int value) {

this.value = value;

}

public String toString(){

return "Value : "+getValue();

}

}

Coin.java

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;

}

}

Output:

Value : 0
The coin has tail and you lose.
Value : 0
The coin has tail and you lose.
Value : 0
The coin has tail and you lose.
Value : 0
The coin has head and you win.
Value : 0
The coin has head and you win.
Value : 0
The coin has head and you win.
Value : 0
The coin has head and you win.
Value : 0
The coin has head and you win.
Value : 0
The coin has tail and you lose.
Value : 0
The coin has head and you win.
Value : 0
The coin has head and you win.
Value : 0
The coin has tail and you lose.
Value : 0
The coin has tail and you lose.
Value : 0
The coin has head and you win.
Value : 0
The coin has tail and you lose.
Value : 0
The coin has tail and you lose.
Value : 0
The coin has head and you win.
Value : 0
The coin has head and you win.
Value : 0
The coin has head and you win.
Value : 0
The coin has head and you win.
Value : 0
The coin has head and you win.
Value : 0
The coin has head and you win.
Value : 0
The coin has tail and you lose.
Value : 0
The coin has head and you win.
Value : 0
The coin has head and you win.
Number of Heads: 16
Number of Tails: 9
Average: 36