Sort and count a bag of coins T have a bag of coins. Design a class that can sor
ID: 3751127 • Letter: S
Question
Sort and count a bag of coins T have a bag of coins. Design a class that can sort and count colns 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. QUARTER24 mm DIME NICKEL PENNY19 mm 18 mm 21 mm We will simulate the bag of coins using an array of 30 integers The simplest solution is to use switch ) construct to select and tally the different coins. Declare the size (diameter) of each coin as private constant in your BagofCoins 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 bag int counter for dimes in the bag quarters dimes nickels pennies foreign int counter for nickels in the bag int counter for of pennies in the bag int counter for foreign coins in the bag public methods: *There is only one default (no-arg) constrictor. No getter/setter required BagOfCoins sortCoinTally void default constructor. Set all 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 toString calculateTotal double StringReturn a string containing the coin tallies and total amount Calculate and return total amount Assignment Instructions 1. Create a NetBeans project and named it FirstName LastName A2 2. Create a separate java class file named it BagofCoins,java. This file contains the public class BagofCoins 3. In your main class, create an array of 30 integers with initialization list. Use the following data to test your class. Create an instance/object of BagofCoins classExplanation / Answer
The following is the JAVA code for class BagOfCoins and the mian function.
import java.io.*;
class BagOfCoins
{
private int quarters;
private int dime;
private int nickel;
private int penny;
private int foreign;
private double total;
BagOfCoins()
{
this.quarters=0;
this.dime=0;
this.nickel=0;
this.penny=0;
this.foreign=0;
this.total=0.0;
}
public void sortCoinTally(int coin)
{
switch(coin)
{
case 24:
this.quarters++;
total+=0.25;
break;
case 18:
this.dime++;
total+=0.1;
break;
case 21:
this.nickel++;
total+=0.05;
break;
case 19:
this.penny++;
total+=0.01;
break;
default:
this.foreign++;
break;
}
}
public String toString()
{
return "quarters: "+this.quarters+" dimes: "+this.dime+" nickel: "+this.nickel+" penny: "+this.penny+" foreign: "+this.foreign+" total: "+this.total+"$";
}
public double calculateTotal()
{
return total;
}
}
class Main {
public static void main (String[] args) {
BagOfCoins bag1=new BagOfCoins();
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};
for(int i=0;i<30;i++)
{
bag1.sortCoinTally(bagCoins[i]);
}
int x=2;
String s=bag1.toString();
System.out.println(s);
}
}
Feel free to ask if you have any query. Upvote if you like my explaination.
Have a nice day!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.