Question

In: Computer Science

Create a coin and bill class Coin and Bill constructors generate the coinDenomination and billDenomination randomly...

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;
    }
}

Solutions

Expert Solution

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());
    }
}

====================================================================================


Related Solutions

JAVA Homework 1) Create a die class. This is similar to the coin class , but...
JAVA Homework 1) Create a die class. This is similar to the coin class , but instead of face having value 0 or 1, it now has value 1,2,3,4,5, or 6. Also instead of having a method called flip, name it roll (you flip a coin but roll a die). You will NOT have a method called isHeads, but you will need a method (call it getFace ) which returns the face value of the die. Altogether you will have...
Create 2 derived classes from Clothing class: Pants class Write only necessary constructors Override the wash()...
Create 2 derived classes from Clothing class: Pants class Write only necessary constructors Override the wash() method to indicate that pants are dry clean only. Include additional method hang() Add to your driver/tester file to make and print new pants objects and test it. Shirt class Include additional property of type string called sleeves. Write necessary constructors For sleeves only allow it to be set to {"short", "long", "none"} For size, only allow {"S","M","L"} Override the wash() method to indicate...
Q3) Design a class for saving data of employee, the class should have multiple constructors (constructor...
Q3) Design a class for saving data of employee, the class should have multiple constructors (constructor overloading) for initializing objects with different parameters. The employee class should have different functions for saving and updating bio-data, educational background, and current status like experience, Salary, position & travel history. The employee class should have a function which asks user what do he likes to see about employee (.i.e., Bio-data, current status....) and only show the respective data. Create two objects of interns...
Part1. Create a number guessing game in Python. Randomly generate a number from 1 to 10....
Part1. Create a number guessing game in Python. Randomly generate a number from 1 to 10. Have the user guess the number. If it is too high, tell the user to guess lower - it it is too low, tell the user to guess higher. Continue until she guesses the correct number. - Part 2. Allow the user to play a new game after they have guessed correctly.   Part 3. Ask for a different name and keep track of how...
summer/** * This Object-Oriented version of the "Summer" class * is a simple introduction to constructors...
summer/** * This Object-Oriented version of the "Summer" class * is a simple introduction to constructors / * private data members / static vs. not static / and the * "toString" method. * * SKELETON FOR LAB TEST. * * @author Raymond Lister * @version April 2015; */ public class SummerOO { public static int numSummers = 0; // The above variable is used to count the number of // instances of the class SummerOO that have been created. //...
C# Assignment: Create a DLL that contains a class used to generate QR codes. From a...
C# Assignment: Create a DLL that contains a class used to generate QR codes. From a console application, reference the project that generates the DLL. Allow the user to enter an address and pass it to the method in the separate DLL that will generate the QR code. You only need to allow the user to generate a single QR code at a time (you may close the application once you have generated it).
Create a class called RandomGuess. In this game, generate and store a random number between 1...
Create a class called RandomGuess. In this game, generate and store a random number between 1 and 100. Then, continuously (in a loop): Ask the user to enter a number between 1 and 100 Let the user know if the guess is high or low, until the user enters the correct value If the user enters the correct value, then display a count of the number of attempts it took and exit
Let’s have a dice and a coin. Randomly throw the dice once and the coin twice....
Let’s have a dice and a coin. Randomly throw the dice once and the coin twice. Determine the space of elementary events and P (A∪B) and check the independence of events A and B, if event A consists in the fact that on dice we have odd number, B that on coin we have heads twice and on dice we have number lower than 3.
Let’s have a dice and a coin. Randomly throw the dice once and the coin twice....
Let’s have a dice and a coin. Randomly throw the dice once and the coin twice. Determine the space of elementary events and P (A∪B) and check the independence of events A and B, if event A consists in the fact that on dice we have even number, B that on coin we have heads twice and on dice we have number lower than 3.
Create a class called RandomWhen. The program must continuously generate a random number between 1 and...
Create a class called RandomWhen. The program must continuously generate a random number between 1 and 100 inclusive. The program must stop when the number 1 is generated, and the loop iteration will be printed. Run the program 3 different times. Sample output: It took ## iterations to generate a 1. It took ## iterations to generate a 1. It took # iterations to generate a 1.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT