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

Sort and count a bag of coins Specification: I have a bag of coins. Design a cla

ID: 3752950 • Letter: S

Question

Sort and count a bag of coins Specification: I have a bag of coins. Design a class that can sort and count coins according to their size. The class should also contain the methods that display the tallies for each kind of coin and the total amount of money in the bag. There may be foreign coins mixed in with the normal coins. These coins should be tallied but not used to calculate the total cash. These are the diameters of US coins QUARTER 24 mm DIME NICKEL PENNY 18 mm 21 mm 19mm We will simulate the bag of coins using an array of 30 integers; The simplest solution is to use switch 0 construct to select and tally the different coins. Declare the size (diameter) of each coin as private constant in your BogofCoins class Class Design: You may add any additional local variables and methods to implement the logic of your solution. Class Name: BagOfCoins Private data members: int counter for quarters in the quarters dines nickels pennies forei int int int counter for dimes in the onter for nickels in the bag counter for of pennies in the bag int counter for coins in the public methods: There is only one default (no-arg) constrictor. No getter/setter BagofCoins sortCoinTally void default constructor. Set al the counters to zero. (int coin)->parameter list takes a coin argument and sorts and tallies respectively if the coin matches coin type then increment the coin counter, otherwise increment the foreign coin counter Return a string containing the coin tallies and total amount Calculate and return total amount toStri calculateTotal double Stri 1. Create a NetBeans project and named it 2. Create a separate java class file named it BagofCoins java. This file contains the public class

Explanation / Answer

Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you

BagOfCoins.java
---------------

public class BagOfCoins {
private int quarters;
private int dimes;
private int nickels;
private int pennies;
private int foreign;
private static final int QUARTER_SIZE = 24;
private static final int DIME_SIZE = 18;
private static final int NICKEL_SIZE = 21;
private static final int PENNY_SIZE = 19;
public BagOfCoins(){
quarters = 0;
dimes = 0;
nickels = 0;
pennies = 0;
foreign = 0;
}
public void sortCoinTally(int coin){
switch(coin){
case QUARTER_SIZE: quarters++;
break;
case DIME_SIZE: dimes++;
break;
case NICKEL_SIZE: nickels++;
break;
case PENNY_SIZE: pennies++;
break;
default:
foreign++;
}
}
public double calculateTotal(){
double total = 0;
total += quarters * 25 + dimes * 10 + nickels * 5 + pennies;
total /= 100; //convert to dollar and cents
return total;
}
public String toString(){
String s = "";
s += "No. of quarters = " + quarters + " ";
s += "No. of dimes = " + dimes + " ";
s += "No. of nickels = " + nickels + " ";
s += "No. of penniess = " + dimes + " ";
s += "Total amount = $" + calculateTotal() +" ";
return s;
}
}
output
-----
No. of quarters = 7
No. of dimes = 6
No. of nickels = 6
No. of penniess = 6
Total amount = $2.7


TestBagOfCoins.java
------------
public class TestBagOfCoins {
public static void main(String[] args) {
int[] bagCoins = {24,18,21,20,19,24,18,21,20,19,24,18,21,19,24,18,23,21,19,24,18,21,19,24,18,21,30,32,24,26};
BagOfCoins myBag = new BagOfCoins();
for(int i = 0; i < bagCoins.length; i++)
myBag.sortCoinTally(bagCoins[i]);
System.out.println(myBag);
}
}

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