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

Java Problem 1.Design and implement a class called MonetaryCoin that is derived

ID: 3819700 • Letter: J

Question

Java Problem

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

2.Create a main driver class to instantiate 25 monetary coins 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

public class Coin

{

public final int HEADS = 0;

public final int TAILS = 1;

private int face;

Coin(int face)

public Coin(int face)

{

this.face = face;

}

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;

}

}

public class MonetaryCoinHW extends Coin

{

public MonetaryCoinHW(int face)

{

setFace(face);

}

public int getFace()

{

if (isHeads())

{

return HEADS;

}

return TAILS;

}

public void setFace( int newFace )

{

while (newFace != getFace())

{

flip();

}

}

In this program you need tomention the coin in class and call itby using this keywordandthen you needtohave one constructor in an argument.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote