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

The PiggyBank is an example of a bag implementation in Java. However, although t

ID: 3592991 • Letter: T

Question

The PiggyBank is an example of a bag implementation in Java. However, although the individual classes appear to be a complete example of a bag implementation you cannot obtain any output.

1. What exactly is causing this problem?

2. What must be done to fix it?

Coin.java public class coin private enum CoinSide HEADS, TAILSh private CoinName myName; private int value; I/ in cents private int year I/ mint year private CoinSide sideUp; /**Constructs an object for the coin having a given value and mint year. The visible side of the new coin is set at random. / public Coin (int coinValue, int mintYear) switch (coinValue) case 1: myName = CoinName . PENNY; break; case 5: = CoinName. NICKEL; myName break; case 10: myName = CoinName. DIME ; break; case 25: myName = CoinName. QUARTER; break; case 50: myName = CoinName. FIFTY-CENT ; break; case 100: CoinName . DOLLAR; myName break; default: myName = C01 nName . PENNY; break; // end switch value = coinvalue ; year = mintYear; sideUp = getToss () ; // end constructor /**Constructs an object for the coin having a given name and mint year. The visible side of the new coin is set at random.

Explanation / Answer

public class Coin

{

private enum CoinSide {HEADS, TAILS}

private CoinName myName;

private int value; // in cents

private int year; // mint year

private CoinSide sideUp;

/** Constructs an object for the coin having a given

value and mint year. The visible side of the new

coin is set at random. */

public Coin(int coinValue, int mintYear)

{

switch (coinValue)

{

case 1:

myName = CoinName.PENNY;

break;

case 5:

myName = CoinName.NICKEL;

break;

case 10:

myName = CoinName.DIME;

break;

case 25:

myName = CoinName.QUARTER;

break;

case 50:

myName = CoinName.FIFTY_CENT;

break;

case 100:

myName = CoinName.DOLLAR;

break;

default:

myName = CoinName.PENNY;

break;

} // end switch

value = coinValue;

year = mintYear;

sideUp = getToss();

} // end constructor

/** Constructs an object for the coin having a given

name and mint year. The visible side of the new

coin is set at random. */

public Coin(CoinName name, int mintYear)

{

switch (name)

{

case PENNY:

value = 1;

break;

case NICKEL:

value = 5;

break;

case DIME:

value = 10;

break;

case QUARTER:

value = 25;

break;

case FIFTY_CENT:

value = 50;

break;

case DOLLAR:

value = 100;

break;

default:

value = 1;

break;

} // end switch

myName = name;

year = mintYear;

sideUp = getToss();

} // end constructor

/** Returns the name of the coin. */

public CoinName getCoinName()

{

return myName;

} // end getCoinName

/** Returns the value of the coin in cents. */

public int getValue()

{

return value;

} // end getValue

/** Returns the coin's mint year as an integer. */

public int getYear()

{

return year;

} // end getYear

/** Returns "HEADS" if the coin is heads-side up;

otherwise, returns "TAILS". */

public String getSideUp()

{

/*

String result = "Tails";

if (sideUp == CoinSide.HEADS)

result = "Heads";

return result;

*/

return sideUp.toString();

} // end getSideUp

/** Returns true if the coin is heads-side up. */

public boolean isHeads()

{

return sideUp == CoinSide.HEADS;

} // end isHeads

/** Returns true if the coin is tails-side up. */

public boolean isTails()

{

return sideUp == CoinSide.TAILS;

} // end isTails

/** Tosses the coin; sideUp will be either HEADS or TAILS at random. */

public void toss()

{

sideUp = getToss();

} // end toss

/** Returns the coin as a string in the form "value/year/side-up". */

public String toString()

{

return value + "/" + year + "/" + sideUp;

} // end toString

// Returns a random value of either HEADS or TAILS.

private CoinSide getToss()

{

CoinSide result;

if (Math.random() < 0.5)

result = CoinSide.HEADS;

else

result = CoinSide.TAILS;

return result;

} // end getToss

} // end Coin

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