Using C# Program Language I have a bag of coins. Design a class that can sort an
ID: 3837603 • Letter: U
Question
Using C# Program Language
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 of each coin as private constant in your BagOfCoin 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 NO initialization constrictor. No Properties 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
ToString
override string
Display the coin tallies and total amount
CalculateTotal
decimal
Calculate and return total amount
In your program.cs, create an array of 30 integers with initialization list. Use the following data to test your class.
const int SIZE = 30;
int [ ] bagCoins = new int [SIZE]
{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}
QUARTER
24 mm
DIME
18 mm
NICKEL
21 mm
PENNY
19 mm
Explanation / Answer
using System;
namespace Test
{
class BagOfCoins
{
private int quarters;
private int dimes;
private int nickels;
private int pennies;
private int foreign;
public BagOfCoins(){
quarters=0;
dimes=0;
nickels=0;
pennies=0;
foreign=0;
}
public int CalculateTotal(){
return (quarters*25)+(dimes*10)+(nickels*5)+(pennies);
}
public void SortCoinTally(int[] coin){
for (int i = 0; i < coin.Length; i++)
{
if(coin[i]==24)
quarters++;
else if(coin[i]==18)
dimes++;
else if(coin[i]==21)
nickels++;
else if(coin[i]==19)
pennies++;
else
foreign++;
int index = i;
for (int j = i + 1; j < coin.Length; j++){
if (coin[j] < coin[index]){
index = j;
}
}
int smallerNumber = coin[index];
coin[index] = coin[i];
coin[i] = smallerNumber;
}
for (int i = 0; i < coin.Length; i++)
{
//Console.WriteLine(i+""+coin[i]);
}
}
public override String ToString(){
String str ="";
str += "QUARTER : "+quarters+" ";
str += "DIME : "+dimes+" ";
str += "NICKEL : "+nickels+" ";
str += "PENNY : "+pennies+" ";
str += "FOREIGN : "+foreign+" ";
str += "------------------------- ";
str += "TOTAL : "+CalculateTotal()+" ";
return str;
}
}
}
namespace Test
{
public class Program
{
public static void Main(string[] args)
{
const int SIZE = 30;
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 coins = new BagOfCoins();
coins.SortCoinTally(bagCoins);
Console.Write(coins);
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.