Sort and count a bag of coins Specification: I have a bag of coins. Design a cla
ID: 3752318 • 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 18 mm NICKEL 21 mm PENNY 19 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: quarters int counter for quarters in the bag dimes int counter for dimes in the bag nickels int counter for nickels in the bag pennies int counter for of pennies in the bag foreign int counter for foreign coins in the bag
public methods: **There is only one default (no-arg) constrictor. No getter/setter required. BagOfCoins default constructor. Set all the counters to zero. sortCoinTally void (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 String Return a string containing the coin tallies and total amount calculateTotal double 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 class 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();
Explanation / Answer
The following is the JAVA code for class BagOfCoins and the mian function.:
method-1:
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);
}
}
method-2:
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);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.