Source of Coin.java


  1: /** 
  2:     A class that represents a coin.
  3:     @author Frank M. Carrano
  4:     @version 4.0
  5: */
  6: public class Coin
  7: {
  8:    private enum CoinSide {HEADS, TAILS}
  9:    private CoinName myName;
 10:    private int value; // In cents
 11:    private int year;  // Mint year
 12:         private CoinSide sideUp;
 13:    /** Constructs an object for the coin having a given  
 14:        value and mint year. The visible side of the new
 15:             coin is set at random. */
 16:    public Coin(int coinValue, int mintYear)
 17:    {
 18:       switch (coinValue)
 19:       {
 20:          case 1:
 21:              myName = CoinName.PENNY;
 22:              break;
 23:          case 5:
 24:              myName = CoinName.NICKEL;
 25:              break;
 26:          case 10:
 27:              myName = CoinName.DIME;
 28:              break;
 29:          case 25:
 30:              myName = CoinName.QUARTER;
 31:              break;
 32:          case 50:
 33:              myName = CoinName.FIFTY_CENT;
 34:              break;
 35:          case 100:
 36:              myName = CoinName.DOLLAR;
 37:              break;
 38:          default:   
 39:              myName = CoinName.PENNY;
 40:              break;
 41:       } // end switch
 42:       value = coinValue;
 43:       year = mintYear;
 44:                 sideUp = getToss();
 45:    } // end constructor
 46:         
 47:    /** Constructs an object for the coin having a given  
 48:        name and mint year. The visible side of the new
 49:             coin is set at random. */
 50:    public Coin(CoinName name, int mintYear)
 51:    {
 52:       switch (name)
 53:       {
 54:          case PENNY:
 55:              value = 1;
 56:              break;
 57:          case NICKEL:
 58:              value = 5;
 59:              break;
 60:          case DIME:
 61:              value = 10;
 62:              break;
 63:          case QUARTER:
 64:              value = 25;
 65:              break;
 66:          case FIFTY_CENT:
 67:              value = 50;
 68:              break;
 69:          case DOLLAR:
 70:              value = 100;
 71:              break;
 72:          default:   
 73:              value = 1;
 74:              break;
 75:       } // end switch
 76:       myName = name;
 77:       year = mintYear;
 78:                 sideUp = getToss();
 79:    } // end constructor
 80:         
 81:    /** Returns the name of the coin. */
 82:         public CoinName getCoinName()
 83:    {
 84:       return myName;
 85:    } // end getCoinName
 86:    /** Returns the value of the coin in cents. */
 87:         public int getValue()
 88:         {
 89:                 return value;
 90:         } // end getValue
 91:         /** Returns the coin's mint year as an integer. */
 92:         public int getYear()
 93:         {
 94:                 return year;
 95:         } // end getYear
 96:         
 97:    /** Returns "HEADS" if the coin is heads-side up;
 98:             otherwise, returns "TAILS". */
 99:         public String getSideUp()
100:         {
101:    /*
102:                 String result = "Tails";
103:                 if (sideUp == CoinSide.HEADS)
104:                         result = "Heads";
105:                 return result;
106:    */
107:       return sideUp.toString();
108:         } // end getSideUp
109:         
110:         /** Returns true if the coin is heads-side up. */
111:         public boolean isHeads()
112:         {
113:                 return sideUp == CoinSide.HEADS;        
114:         } // end isHeads
115:         
116:         /** Returns true if the coin is tails-side up. */
117:         public boolean isTails()
118:         {
119:                 return sideUp == CoinSide.TAILS;        
120:         } // end isTails
121:         
122:         /** Tosses the coin; sideUp will be either 
123:        HEADS or TAILS randomly. */
124:         public void toss()
125:         {
126:                 sideUp = getToss();
127:         } // end toss
128:    
129:    /** Returns the coin as a string in the form "value/year/side-up". */
130:    public String toString()
131:    {
132:       return value + "/" + year + "/" + sideUp;
133:    } // end toString
134:    
135:    // Returns a random value of either HEADS or TAILS.
136:         private CoinSide getToss()
137:         {
138:                 CoinSide result;
139:                 if (Math.random() < 0.5)
140:                         result = CoinSide.HEADS;
141:                 else
142:                         result = CoinSide.TAILS;
143:       
144:                 return result;
145:         } // end getToss
146: } // end Coin