In: Computer Science
Create a coin and bill class
Coin and Bill constructors generate the coinDenomination and billDenomination randomly based on the number of denominations defined in the respective enums.
The method getValue returns the value of a coin (for example 0.25 for a quarter) or the value of paper bill respectively. The method toString returns the String representation of the object; for example: “WASHINGTON landed TAILS” or “NICKEL landed TAILS”.
I already have the coinDenomination and billDenomination just need help with the coin and bill classes
public enum BillDenomination { WASHINTON(1), JEFFERSON(2), LINCOLN(5), HAMILTON(10), JACKSON(20), GRANT(50), FRANKLIN(100); private int billValue; private BillDenomination(int billValue) { this.billValue = billValue; } public double getbillValue() { return this.billValue; } }
public enum CoinDenomination { PENNY(0.01), NICKEL(0.05), DIME(0.10), QUARTER(0.25); private double coinValue; private CoinDenomination(double coinValue) { this.coinValue = coinValue; } public double getCoinValue() { return this.coinValue; } }
Thanks for the question. Here is the completed code for this problem. Comments are included so that you understand whats going on. Let me know if you have any doubts or if you need anything to change. Thank You !! ================================================================================================ import java.util.Random; public class Coin { private CoinDenomination coin; // will have an instance type of CoinDenomination type private static final int TOTAL_COINS=4; // there are total 4 coin denominations public Coin(){ // randomly generate a number between 0 and 3 Random random = new Random(); int randomNumber = random.nextInt(TOTAL_COINS); switch (randomNumber){ case 0: coin = CoinDenomination.PENNY; case 1: coin = CoinDenomination.NICKEL; case 2: coin = CoinDenomination.DIME; case 3: coin = CoinDenomination.QUARTER; } } public CoinDenomination getCoin() { return coin; } }
====================================================================================
import java.util.Random; public class Bill { private BillDenomination bill; // will have an instance type of BillDenominationtype private static final int TOTAL_BILLS=7;// there are total 7 bill denominations public Bill(){ // randomly generate a number between 0 and 6 Random random = new Random(); int randomNumber = random.nextInt(TOTAL_BILLS); switch (randomNumber){ case 0: bill = BillDenomination.WASHINTON; case 1: bill = BillDenomination.JEFFERSON; case 2: bill = BillDenomination.LINCOLN; case 3: bill = BillDenomination.HAMILTON; case 4: bill = BillDenomination.JACKSON; case 5: bill = BillDenomination.GRANT; case 6: bill = BillDenomination.FRANKLIN; } } public BillDenomination getBill() { return bill; } }
====================================================================================
public class DriverCurrency { public static void main(String[] args) { Coin coin = new Coin(); System.out.println(coin.getCoin()); Bill bill = new Bill(); System.out.println(bill.getBill()); } }
====================================================================================