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 a class called College that has: At least 2 constructors (one should be the default...
Create a class called College that has: At least 2 constructors (one should be the default constructor) All members private The relevant setters and getters A print_me() function A college_ID member (unique) Write two unsorted lists that manage the data for colleges and their ranking: An unsorted list using an array An unsorted list using a linked list Each college will have a unique ID number that will be used for search. For the lab you don't need to read...
Overloaded Constructors Add a pair of constructors to the Height class that implement the initializations provided...
Overloaded Constructors Add a pair of constructors to the Height class that implement the initializations provided by the two setHeight operations in Figure 7.11. Minimize the total number of statements by having the one parameter constructor call the one-parameter setHeight method and having the two-parameter constructor call the two-parameter setHeight method. Provide a complete rewritten main method for the HeightDriver class such that the new method uses one of the new constructors from part a) to generate this output: 6.0...
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...
C++: 1.When and in what order are constructors and destructors called? 2. Create a class without...
C++: 1.When and in what order are constructors and destructors called? 2. Create a class without any constructors, and show that you can create objects with the default constructor. Now create a non-default constructor (one with an argument, aka parameterized constructor) for the class, and try compiling again. Explain what happened.
Implement the Nickel class. Include Javadoc comments for the class, public fields, constructors, and methods of...
Implement the Nickel class. Include Javadoc comments for the class, public fields, constructors, and methods of the class. I have added the Javadoc comments but please feel free to format them if they are done incorrectly. public class Nickel implements Comparable { private int year; /** * The monetary value of a nickel in cents. */ public final int CENTS = 5; /** * Initializes this nickel to have the specified issue year. * * @param year * * @pre....
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...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT