Advertisement
advictoriam

Untitled

Feb 1st, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.49 KB | None | 0 0
  1. /**
  2.    A coin with a monetary value.
  3. */
  4.  
  5. /*
  6.    TODO: Add a static variable that maintains the total value of coin objects
  7.    that have been constructed. Add a static method totalValue that
  8.    reports the total value.
  9. */
  10.  
  11. public class Coin
  12. {
  13.    private double value;
  14.    private String name;
  15.    static double valueOfCoins = 0;
  16.    /**
  17.       Constructs a coin.
  18.       @param aValue the monetary value of the coin.
  19.       @param aName the name of the coin
  20.    */
  21.    public Coin(double aValue, String aName)
  22.    {
  23.       value = aValue;
  24.       name = aName;
  25.       valueOfCoins += value;
  26.    }
  27.  
  28.    /**
  29.       Gets the coin value.
  30.       @return the value
  31.    */
  32.    public double getValue()
  33.    {
  34.       return value;
  35.    }
  36.    
  37.    public static double totalValue()
  38.    {
  39.       return valueOfCoins;
  40.    }
  41.  
  42.    /**
  43.       Gets the coin name.
  44.       @return the name
  45.    */
  46.    public String getName()
  47.    {
  48.       return name;
  49.    }
  50.  
  51.    /**
  52.       Returns a string representation of the object.
  53.       @return name and value of coin
  54.    */
  55.    public String toString()
  56.    {
  57.       return "Coin[value=" + value + ",name=" + name + "]";
  58.    }
  59.  
  60.    public static double check(int dimes, int quarters)
  61.    {
  62.       Coin[] coins = new Coin[dimes + quarters];
  63.       for (int i = 1; i <= dimes; i++)
  64.       {
  65.          coins[i] = new Coin(0.10, "dime");
  66.       }
  67.       for (int i = 1; i <= quarters; i++)
  68.       {
  69.          coins[dimes] = new Coin(0.25, "quarter");
  70.       }
  71.       return Coin.totalValue();
  72.    }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement