In this problem, you are asked to create a class for change jars. A change jar contains an arbitrary collection of coins i.e., quarters, dimes, nickels, and pennies. There are no dollar bills of any denomination in a change jar. We may use change jars to get exact change for some specified amount, or we may add more coins into it. Create a module called problem2.py to implement these and other methods for change jars in a class called ChangeJar. Further details are provided below:

1. __init__: The constructor has a single parameter, which is a dictionary of key:value pairs in which the keys must be 25, 10, 5, or 1 (for quarters, dimes, nickels, and pennies). All other keys are invalid (you may raise an exception in this case). The value associated with each key is the number of coins of that denomination in the change jar. Note that not every key need appear in the parameter. If a key does not appear, it means that there are no coins of that denomination in the jar. Set the default value of the parameter to be the empty dictionary, which will allow us to create change jars with no quarters, dimes, nickels, or pennies.

The constructor should create a dictionary as an instance attribute which has exactly four keys: 25, 10, 5, and 1. The value associated with each key should be the number of coins of that type in the jar.

For example, the statement J = ChangeJar({25:8, 5:10, 1:45}) creates a change jar that has 8 quarters, 0 dimes, 10 nickels, and 45 pennies. The statement J = ChangeJar() creates a change jar with 0 quarters, 0 dimes, 0 nickels, and 0 pennies.

2. get_change: This method has a single parameter, dollar amt, which is a value corresponding to a dollar amount. It should return a ChangeJar object that contains the number of quarters, dimes, nickels, and pennies required to create exact change corresponding to that dollar amount. The corresponding numbers of coins should be deducted from the activating change jar.

There is more than one way to make exact change for a specified value. Here, you are used to use the fewest number of coins possible to make the change. Note that you must return exact change. This means that if the coins in the jar cannot form exact change for dollar amt, the method should return an empty change jar. Keep in mind that this may happen even if there is enough total money in the change jar. For example, if a change jar has 4 quarters, 3 dimes, and 4 pennies, we cannot get exact change for $1.25 from the jar, even though it has $1.34 in it.

Tip: Convert dollar amt to cents before you find the change. That is, work with integer values, rather than real values.

3. __getitem__: This method overloads the index operator. When the index value is 25, 10, 5, or 1, the method returns the number of quarters, dimes, nickels, or pennies, respectively, in the jar. All other index values smaller than 25 should return 0, and an index value greater than 25 should raise the StopIteration exception.

4. insert: This method is used to add more coins of a particular value into the change jar. The method has two parameters: coin value (which has value 25, 10, 5, or 1) and num coins (the number of coins of that value being inserted into the jar). Note that you are adding to the existing coins in the jar. For example, if J is a ChangeJar instance, J.insert(10, 12) will insert 12 more dimes into J.

5. total_value: This method returns the total dollar value of the change jar as a real number.

6. __str__: This method returns a string representation of the change jar. Recall that this method must return a string. It should contain succinct information about the number of quarters, dimes, nickels, and pennies in the change jar.

7. __repr__: This method returns a printable representation (also a string) of the change jar.

8. __add__: This method overloads the + operator. It returns a change jar that contains all the coins from the two change jars that are the operands. Keep in mind that the operands themselves should not get modified by this method.

9. __eq__: This method overloads the == operator. It returns True if the two change jars have exactly the same numbers of coins of each type, and False otherwise.

10. __ne__: This method overloads the != operator. It returns True if the two change jars are not equal (as defined above) and False otherwise.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.