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: 3592861 • 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?

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

Baglnterface.java public interface BagInterfacecT> * Gets the cuzzent number of entries in this bag public 1nt getcurrent51ze0 * Sees whether this bag is eampty public boolean isEpty @return The integer number of entries currently in the bag. returo Tru i the bag is empty,o alse if not. / Addsa new entry to this bag param newEntzy The object to be added as a new entzy @return True if the addition is successful, or fàlee if not. public boolean add (T nevEntryz) Removes one unspecified entry Erom this bag, if possible retur Either the renoved entry, if te removal was successful, or null. public T renove): Removes one occurrence of a given enty Eron this bag. 8paran anEntzy The entry to be renoved. return Troe If the removal w53 uccesstul, or false It not. public boolean remove (T anEntry) /** Removes all entries from this bag. public void clear: Counts the umber of tinesagiven entry appears in this bag param anEntzy The entry to be counted. return The number of times anEntry appears in the bag. public int ge requencyor(T anEntry); /* * Tests tether this beg contains gven entry 8param anEntzy The entry to locate Breturn True If the bag contalna anEntry, or raise 1t not. public boolean containa( anEntry) Retrieves all entries that are in this bag eturn True if the addition is successEul, or false if not. public boolean add (I nekEntry /xx Removes one unspecified entry from th15 bag, f possible return Either the renoved entry, if the removal was successful, or null. public T reove Removes one occurrence of a given entry from this bag paran anEntzy The entzy to be renoved return Teue if the enoval was successtul, ox false it ot public boolcan remove (T anEntry): /** Remove all entries from this bag. public vod cla Counts the mumber of inca a given entry appears in this bag paran anEntry The entry to be counted retur The namber of tines anEntry appears in the bag. / public int gerfrequencyofT anEntry) Tests whether this bag contains a given entry paran anEntry The entzy to locate greturn True if the bag contains anEntry, or false if not. pubiic boolean containa (T anEntry) /** Retrieve5 all entrie5 that are in this bag greturn newly allocated array of all the entries in the bag ote: If the bag is empty, the returned array is enpty. / public T[] Array(); publicT> T toArray Alternate public Object[) toArray0: 7 Alternate Createa a neW bag that oombinea the contenta of thia bag @paran anotherBag The bag that to be added. greturn combined bag. public Baginterfacec> union (BaginterfacecI> anotherBag) Creates a new bag that containa those objects that occur in both this bag and anotherBag paran anotherDag The bag that is to be compared return A combined bag, A/ abi1c Bagintertacec Baginterface anotherBag) T> intersection

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