Question: Consider an abstract class Money with two subclasses: class Coin that represents a coin class Bill that represents a paper bill. A paper bill can

Consider an abstract class Money with two subclasses:

class Coin that represents a coin

class Bill that represents a paper bill.

A paper bill can have one of the 7 denomination values: 0, 1, 2, 3, 4, 5, 6 representing respectively: 1, 2, 5, 10, 20, 50, and 100 dollars with names: WASHINGTON, JEFFERSON, LINCOLN, HAMILTON, JACKSON, GRANT, and FRANKLIN respectively.

A coin can have one of the 5 denomination values: 0, 1, 2, 3, 4 representing respectively: 1, 5, 10, 25, and 50 cents with names: PENNY, NICKEL, DIME, QUARTER, and HALF_DOLLAR respectively.

Denominations and denomination names are stored in respective arrays.

Each coin and bill can either be placed HEADS or TAILS up.

Money constructor generates the denomination randomly based on the number of denominations passed to it, and sets the value of heads to false.

The method getValue returns the value of a coin (for example 0.25 for a quarter) or the value of paper bill respectively.

The method toss simulates a money toss in which the coin/paper bill lands either heads up or tails up.

The method isHeads returns true if a coin is heads up.

The class has only one constructor, the secondary constructor, which sets the value of heads to false; the denomination is randomly generated based on the numberOfDenominations passed to it.

Now, you need to have a piggy bank with predefined capacity to hold your monies. The piggy bank holds the monies but gives them no other organization. And it can certainly contain duplicates. The piggy bank is an ADT that has only the following operations:

check the capacity of the piggy bank,

you can add money to the piggy bank one at a time,

o adding fails if there is no more room in the piggy bank (PiggyBankFullException will be thrown that must be handled by the client)

remove one (randomly selected),

o remove fails if the piggy bank is empty (PiggyBankEmptyException will be thrown that must be handled by the client)

see whether the piggy bank is empty,

see whether the piggy bank is full,

see how many monies can fit in the piggy bank,

see how many monies are in the piggy bank,

see the content of the piggy bank,

shake the piggy bank to rearrange the monies,

empty the piggy bank counting how many monies landed HEADS

This functionality should be implemented in a class PiggyBank where the piggy bank is represented as an ResizableArrayBag of Money objects (this.bucket instance variable). The PiggyBank constructor creates this.bucket object and fills it with the given number of monies, randomly choosing a Coin or a Bill object to create and add to this.bucket.

PLEASE NOTE: 
 that shake method must utilize toArray method to get the access to the objects: 
Object[] toShake = this.bucket.toArray();
For each element in the toShake array select randomly another element to swap it with. Once the objects are shuffled in the toShake array they need to be added to this.bucket (which needs to be cleared first)
 PiggyBank toString method utilizes polymorphism:
o Calls toArray to get the elements (see above)
o Uses a for loop over the resulted array to compute the total - must use casting: total += ((Money) content[i]).getValue();
o Calls Arrays.toString to get the content of the objects
 

emptyAndCountHeads method while removing money from the piggy bank counts how many coins/bills landed HEADS, displays the count and the dollar value, and returns the heads count.

Client called CountHeadsGame is fully implemented. It manages a game where the user and the computer take turns to create the piggy bank with the same capacity and the same initial number of monies (since the monies are randomly generated the denominations will be different for each player). Each player empties the piggy bank. The player with the larger number of monies landing HEADS wins. The tie is also possible.

Round constructor creates PiggyBank object and displays the content of the piggy bank. The Round object is created in the main.

addTwoMonies method creates one Coin object and one Bill object and attempts to add them to the piggy bank. Since the piggy bank may be full, the addTwoMonies method must handle PiggyBankFullException

run method calls addTwoMonies method; shakes the piggy bank; prints its content; asks the piggy bank to emptyAndCountHeads and returns the number of heads.

See the Sample Runs below.

Your Task:

1. Load the provided classes into IDEA

2. Create all the remaining classes as defined in the UML diagram provided below:

a. Define all the methods as skeletons first (see Round class)

b. Declare all instance variables private

c. Pay attention which methods should be defined as abstract

d. Make sure to implement inheritance relationship

e. Pay attention which variables should be defined as static

f. Pay attention which variables should be defined as final

g. Make sure that all constants have appropriate values assigned at the declaration level

h. Do not start implementation until all classes compile as skeletons

3. Use javadoc-style comments to describe the purpose of each method, its parameters and return values.

4. Implement the application iteratively using the bottom-up approach by implementing the classes in the following order:

a. Money

b. Bill

c. Coin

d. PiggyBank

e. Round

5. Make sure that the output is properly formatted as shown in the Sample Runs below.

Sample Runs:

Run#1:

How many coins/bills can fit in your piggy bank?

10

How many coins/bills should the computer put in your piggy bank?

60

How many rounds should we play?

4

How many coins/bills can fit in your piggy bank?

10

How many coins/bills should the computer put in your piggy bank?

6

How many rounds should we play?

4

******* Round #1 --> USER'S TURN *******

>> Adding 6 monies to your piggy bank

Added $0.50 to the piggy bank

Added $0.05 to the piggy bank

Added $0.10 to the piggy bank

Added $20.00 to the piggy bank

Added $10.00 to the piggy bank

Added $0.01 to the piggy bank

>> The content of your piggy bank

The piggy bank can hold 10 coins/bills;

There are 6 coins/bills in the piggy bank:[HALF_DOLLAR landed TAILS, NICKEL landed TAILS, DIME landed TAILS, JACKSON landed TAILS, HAMILTON landed TAILS, PENNY landed TAILS]

The total of $30.66

--> Adding additional monies:

Added $0.25 to the piggy bank

Added $50.00 to the piggy bank

>> Shaking your piggy bank

The piggy bank can hold 10 coins/bills;

There are 8 coins/bills in the piggy bank:[PENNY landed TAILS, HAMILTON landed TAILS, GRANT landed TAILS, DIME landed TAILS, NICKEL landed TAILS, QUARTER landed TAILS, JACKSON landed TAILS, HALF_DOLLAR landed TAILS]

The total of $80.91

>> Emptying your piggy bank

HALF_DOLLAR landed HEADS

JACKSON landed HEADS

QUARTER landed TAILS

NICKEL landed TAILS

DIME landed HEADS

GRANT landed HEADS

HAMILTON landed HEADS

PENNY landed TAILS

5 out of 8 coins/bills landed "HEADS"

The total value of "HEADS" is: $80.60

******* Round #2 --> COMPUTER'S TURN *******

>> Adding 6 monies to your piggy bank

Added $0.05 to the piggy bank

Added $0.05 to the piggy bank

Added $0.05 to the piggy bank

Added $0.10 to the piggy bank

Added $2.00 to the piggy bank

Added $20.00 to the piggy bank

>> The content of your piggy bank

The piggy bank can hold 10 coins/bills;

There are 6 coins/bills in the piggy bank:[NICKEL landed TAILS, NICKEL landed TAILS, NICKEL landed TAILS, DIME landed TAILS, JEFFERSON landed TAILS, JACKSON landed TAILS]

The total of $22.25

--> Adding additional monies:

Added $0.01 to the piggy bank

Added $5.00 to the piggy bank

>> Shaking your piggy bank

The piggy bank can hold 10 coins/bills;

There are 8 coins/bills in the piggy bank:[PENNY landed TAILS, DIME landed TAILS, NICKEL landed TAILS, LINCOLN landed TAILS, NICKEL landed TAILS, JEFFERSON landed TAILS, NICKEL landed TAILS, JACKSON landed TAILS]

The total of $27.26

>> Emptying your piggy bank

JACKSON landed HEADS

NICKEL landed HEADS

JEFFERSON landed TAILS

NICKEL landed TAILS

LINCOLN landed TAILS

NICKEL landed HEADS

DIME landed HEADS

PENNY landed TAILS

4 out of 8 coins/bills landed "HEADS"

The total value of "HEADS" is: $20.20

******* Round #3 --> USER'S TURN *******

>> Adding 6 monies to your piggy bank

Added $0.25 to the piggy bank

Added $0.05 to the piggy bank

Added $2.00 to the piggy bank

Added $50.00 to the piggy bank

Added $10.00 to the piggy bank

Added $0.25 to the piggy bank

>> The content of your piggy bank

The piggy bank can hold 10 coins/bills;

There are 6 coins/bills in the piggy bank:[QUARTER landed TAILS, NICKEL landed TAILS, JEFFERSON landed TAILS, GRANT landed TAILS, HAMILTON landed TAILS, QUARTER landed TAILS]

The total of $62.55

--> Adding additional monies:

Added $0.10 to the piggy bank

Added $50.00 to the piggy bank

>> Shaking your piggy bank

The piggy bank can hold 10 coins/bills;

There are 8 coins/bills in the piggy bank:[GRANT landed TAILS, QUARTER landed TAILS, GRANT landed TAILS, DIME landed TAILS, JEFFERSON landed TAILS, NICKEL landed TAILS, QUARTER landed TAILS, HAMILTON landed TAILS]

The total of $112.65

>> Emptying your piggy bank

HAMILTON landed TAILS

QUARTER landed HEADS

NICKEL landed HEADS

JEFFERSON landed HEADS

DIME landed TAILS

GRANT landed HEADS

QUARTER landed TAILS

GRANT landed HEADS

5 out of 8 coins/bills landed "HEADS"

The total value of "HEADS" is: $102.30

******* Round #4 --> COMPUTER'S TURN *******

>> Adding 6 monies to your piggy bank

Added $0.01 to the piggy bank

Added $100.00 to the piggy bank

Added $10.00 to the piggy bank

Added $20.00 to the piggy bank

Added $0.01 to the piggy bank

Added $100.00 to the piggy bank

>> The content of your piggy bank

The piggy bank can hold 10 coins/bills;

There are 6 coins/bills in the piggy bank:[PENNY landed TAILS, FRANKLIN landed TAILS, HAMILTON landed TAILS, JACKSON landed TAILS, PENNY landed TAILS, FRANKLIN landed TAILS]

The total of $230.02

--> Adding additional monies:

Added $0.50 to the piggy bank

Added $10.00 to the piggy bank

>> Shaking your piggy bank

The piggy bank can hold 10 coins/bills;

There are 8 coins/bills in the piggy bank:[PENNY landed TAILS, FRANKLIN landed TAILS, FRANKLIN landed TAILS, JACKSON landed TAILS, PENNY landed TAILS, HALF_DOLLAR landed TAILS, HAMILTON landed TAILS, HAMILTON landed TAILS]

The total of $240.52

>> Emptying your piggy bank

HAMILTON landed HEADS

HAMILTON landed TAILS

HALF_DOLLAR landed HEADS

PENNY landed HEADS

JACKSON landed HEADS

FRANKLIN landed HEADS

FRANKLIN landed TAILS

PENNY landed HEADS

6 out of 8 coins/bills landed "HEADS"

The total value of "HEADS" is: $130.52

***GAME OVER***

computerHeadsCount = 10

userHeadsCount = 10

It's a tie!!!

Process finished with exit code 0

Run#2:

How many coins/bills can fit in your piggy bank?

10

How many coins/bills should the computer put in your piggy bank?

9

How many rounds should we play?

2

******* Round #1 --> USER'S TURN *******

>> Adding 9 monies to your piggy bank

Added $20.00 to the piggy bank

Added $1.00 to the piggy bank

Added $0.50 to the piggy bank

Added $1.00 to the piggy bank

Added $0.10 to the piggy bank

Added $0.50 to the piggy bank

Added $50.00 to the piggy bank

Added $2.00 to the piggy bank

Added $0.25 to the piggy bank

>> The content of your piggy bank

The piggy bank can hold 10 coins/bills;

There are 9 coins/bills in the piggy bank:[JACKSON landed TAILS, WASHINGTON landed TAILS, HALF_DOLLAR landed TAILS, WASHINGTON landed TAILS, DIME landed TAILS, HALF_DOLLAR landed TAILS, GRANT landed TAILS, JEFFERSON landed TAILS, QUARTER landed TAILS]

The total of $75.35

--> Adding additional monies:

Added $0.10 to the piggy bank

No more room in the piggy bank! - additional monies will not be added to your piggy bank.

>> Shaking your piggy bank

The piggy bank can hold 10 coins/bills;

There are 10 coins/bills in the piggy bank:[DIME landed TAILS, QUARTER landed TAILS, JEFFERSON landed TAILS, GRANT landed TAILS, HALF_DOLLAR landed TAILS, JACKSON landed TAILS, DIME landed TAILS, HALF_DOLLAR landed TAILS, WASHINGTON landed TAILS, WASHINGTON landed TAILS]

The total of $75.45

>> Emptying your piggy bank

WASHINGTON landed HEADS

WASHINGTON landed HEADS

HALF_DOLLAR landed HEADS

DIME landed TAILS

JACKSON landed TAILS

HALF_DOLLAR landed TAILS

GRANT landed HEADS

JEFFERSON landed TAILS

QUARTER landed TAILS

DIME landed TAILS

4 out of 10 coins/bills landed "HEADS"

The total value of "HEADS" is: $52.50

******* Round #2 --> COMPUTER'S TURN *******

>> Adding 9 monies to your piggy bank

Added $0.50 to the piggy bank

Added $0.50 to the piggy bank

Added $10.00 to the piggy bank

Added $0.25 to the piggy bank

Added $0.05 to the piggy bank

Added $0.10 to the piggy bank

Added $0.05 to the piggy bank

Added $100.00 to the piggy bank

Added $0.10 to the piggy bank

>> The content of your piggy bank

The piggy bank can hold 10 coins/bills;

There are 9 coins/bills in the piggy bank:[HALF_DOLLAR landed TAILS, HALF_DOLLAR landed TAILS, HAMILTON landed TAILS, QUARTER landed TAILS, NICKEL landed TAILS, DIME landed TAILS, NICKEL landed TAILS, FRANKLIN landed TAILS, DIME landed TAILS]

The total of $111.55

--> Adding additional monies:

Added $0.25 to the piggy bank

No more room in the piggy bank! - additional monies will not be added to your piggy bank.

>> Shaking your piggy bank

The piggy bank can hold 10 coins/bills;

There are 10 coins/bills in the piggy bank:[DIME landed TAILS, NICKEL landed TAILS, NICKEL landed TAILS, FRANKLIN landed TAILS, HALF_DOLLAR landed TAILS, QUARTER landed TAILS, HALF_DOLLAR landed TAILS, DIME landed TAILS, QUARTER landed TAILS, HAMILTON landed TAILS]

The total of $111.80

>> Emptying your piggy bank

HAMILTON landed HEADS

QUARTER landed TAILS

DIME landed HEADS

HALF_DOLLAR landed HEADS

QUARTER landed TAILS

HALF_DOLLAR landed HEADS

FRANKLIN landed HEADS

NICKEL landed TAILS

NICKEL landed HEADS

DIME landed TAILS

6 out of 10 coins/bills landed "HEADS"

The total value of "HEADS" is: $111.15

***GAME OVER***

computerHeadsCount = 6

userHeadsCount = 4

Computer wins!!!

Process finished with exit code 0

Run#3:

How many coins/bills can fit in your piggy bank?

10

How many coins/bills should the computer put in your piggy bank?

8

How many rounds should we play?

******* Round #1 --> COMPUTER'S TURN *******

>> Adding 8 monies to your piggy bank

Added $2.00 to the piggy bank

Added $0.50 to the piggy bank

Added $0.01 to the piggy bank

Added $0.50 to the piggy bank

Added $50.00 to the piggy bank

Added $0.50 to the piggy bank

Added $100.00 to the piggy bank

Added $50.00 to the piggy bank

>> The content of your piggy bank

The piggy bank can hold 10 coins/bills;

There are 8 coins/bills in the piggy bank:[JEFFERSON landed TAILS, HALF_DOLLAR landed TAILS, PENNY landed TAILS, HALF_DOLLAR landed TAILS, GRANT landed TAILS, HALF_DOLLAR landed TAILS, FRANKLIN landed TAILS, GRANT landed TAILS]

The total of $203.51

--> Adding additional monies:

Added $0.10 to the piggy bank

Added $5.00 to the piggy bank

>> Shaking your piggy bank

The piggy bank can hold 10 coins/bills;

There are 10 coins/bills in the piggy bank:[DIME landed TAILS, HALF_DOLLAR landed TAILS, GRANT landed TAILS, PENNY landed TAILS, HALF_DOLLAR landed TAILS, FRANKLIN landed TAILS, GRANT landed TAILS, JEFFERSON landed TAILS, LINCOLN landed TAILS, HALF_DOLLAR landed TAILS]

The total of $208.61

>> Emptying your piggy bank

HALF_DOLLAR landed TAILS

LINCOLN landed TAILS

JEFFERSON landed HEADS

GRANT landed HEADS

FRANKLIN landed HEADS

HALF_DOLLAR landed TAILS

PENNY landed HEADS

GRANT landed TAILS

HALF_DOLLAR landed TAILS

DIME landed HEADS

5 out of 10 coins/bills landed "HEADS"

The total value of "HEADS" is: $152.11

******* Round #2 --> USER'S TURN *******

>> Adding 8 monies to your piggy bank

Added $5.00 to the piggy bank

Added $0.01 to the piggy bank

Added $2.00 to the piggy bank

Added $2.00 to the piggy bank

Added $0.50 to the piggy bank

Added $0.25 to the piggy bank

Added $20.00 to the piggy bank

Added $5.00 to the piggy bank

>> The content of your piggy bank

The piggy bank can hold 10 coins/bills;

There are 8 coins/bills in the piggy bank:[LINCOLN landed TAILS, PENNY landed TAILS, JEFFERSON landed TAILS, JEFFERSON landed TAILS, HALF_DOLLAR landed TAILS, QUARTER landed TAILS, JACKSON landed TAILS, LINCOLN landed TAILS]

The total of $34.76

--> Adding additional monies:

Added $0.10 to the piggy bank

Added $10.00 to the piggy bank

>> Shaking your piggy bank

The piggy bank can hold 10 coins/bills;

There are 10 coins/bills in the piggy bank:[JEFFERSON landed TAILS, QUARTER landed TAILS, JACKSON landed TAILS, LINCOLN landed TAILS, PENNY landed TAILS, DIME landed TAILS, HALF_DOLLAR landed TAILS, HAMILTON landed TAILS, LINCOLN landed TAILS, JEFFERSON landed TAILS]

The total of $44.86

>> Emptying your piggy bank

JEFFERSON landed HEADS

LINCOLN landed TAILS

HAMILTON landed HEADS

HALF_DOLLAR landed HEADS

DIME landed HEADS

PENNY landed HEADS

LINCOLN landed TAILS

JACKSON landed HEADS

QUARTER landed TAILS

JEFFERSON landed TAILS

6 out of 10 coins/bills landed "HEADS"

The total value of "HEADS" is: $32.61

***GAME OVER***

computerHeadsCount = 5

userHeadsCount = 6

User wins!!!

Process finished with exit code 0

Consider an abstract class Money with two subclasses: class Coin that represents

Piggy8ankEmptyException PiggyBankEmptyException(String) bucket Baginterface a capacity -PiggyBank(int, int remove misEmpty shake() memptyAndCountHeads( int toString() PiggyBankFullE xoeption PiggyBankFullExcept ion(St ring) &bag &numberOf Entries initialized boolean .DEFAULT-CAPACITY int MAX CAPACITY ResizableArrayBag) ResizableArrayBag(int) ResizableArrayBagiTI Baginterface getCurrentSize0 isEmpty add(T) remove) remove(T doar() add(T boolean @ toArray0 isEmpty0 getCurrentSize0 getFrequencyf(T) int cont ain s T) boolean boolean &clear0 ntains(T) toArray "An boolean removeo remove(T) getindexOf(T) boolean & removeEntryint isArrayFull() doubleCapacity oid chockCapacity(int) void checklnit iaization0 void boolean Round DENOMINATION VALUE int D DENOMINATION NAME StringD ecreateNUMBER OF DENOMINATIONS int ..myPiggyBank riggyBank | Round(int, int) addTwoMonies() '_ main(String) void void a run) getValue) to String Money i denomination int DENOMINATION VALUE DENOMNA TION NAME StringD NUMBER-OF-DENOMINATIONS CoinO getValue) int D heads boolean m Money@nt) int m getDanomination0 int double void booloan String getValue) to Stringo @ sHeads() toString() Piggy8ankEmptyException PiggyBankEmptyException(String) bucket Baginterface a capacity -PiggyBank(int, int remove misEmpty shake() memptyAndCountHeads( int toString() PiggyBankFullE xoeption PiggyBankFullExcept ion(St ring) &bag &numberOf Entries initialized boolean .DEFAULT-CAPACITY int MAX CAPACITY ResizableArrayBag) ResizableArrayBag(int) ResizableArrayBagiTI Baginterface getCurrentSize0 isEmpty add(T) remove) remove(T doar() add(T boolean @ toArray0 isEmpty0 getCurrentSize0 getFrequencyf(T) int cont ain s T) boolean boolean &clear0 ntains(T) toArray "An boolean removeo remove(T) getindexOf(T) boolean & removeEntryint isArrayFull() doubleCapacity oid chockCapacity(int) void checklnit iaization0 void boolean Round DENOMINATION VALUE int D DENOMINATION NAME StringD ecreateNUMBER OF DENOMINATIONS int ..myPiggyBank riggyBank | Round(int, int) addTwoMonies() '_ main(String) void void a run) getValue) to String Money i denomination int DENOMINATION VALUE DENOMNA TION NAME StringD NUMBER-OF-DENOMINATIONS CoinO getValue) int D heads boolean m Money@nt) int m getDanomination0 int double void booloan String getValue) to Stringo @ sHeads() toString()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!