Advertisement
advictoriam

Untitled

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