/** * This class will hold information for a coin. * It will also allow us to perform coin operations. * * @author Mr. Merlis * @version 12/12/2007 */ import java.util.Random; // use the Random Class public class Coin { // instance variables private double myValue; // stores the value of the coin private String myName; // the name of the coin private Random rand = new Random(); /** * Default constructor - makes a penny */ public Coin() { myValue = .01; myName = "penny"; }//=================================== /** * This constructor creates a coin with the * parameter specified value. */ public Coin(double newValue) { myValue = newValue; if(myValue == .50) // this is a half dollar myName = "half dollar"; else if(myValue == .25) // quarter myName = "quarter"; else if(myValue == .10) // dime myName = "dime"; else if(myValue == .05) // nickel myName = "nickel"; else if(myValue == .01) // penny myName = "penny"; else myName = "unknown coin"; }//=================================== /** * This constructor creates a coin with the * parameter specified name. */ public Coin(String newName) { myName = newName; if(myName.equalsIgnoreCase("half dollar")) // this is a half dollar myValue = .50; else if(myName.equalsIgnoreCase("quarter")) // this is a quarter myValue = .25; else if(myName.equalsIgnoreCase("dime")) myValue = .10; else if(myName.equalsIgnoreCase("nickel")) myValue = .05; else if(myName.equalsIgnoreCase("penny")) myValue = .01; else { myValue = 0; myName = "unknown coin"; } }//=================================== /** * This method will return the value of the coin. */ public double getMyValue() { return myValue; }//==================================== /** * This method will return the name of the coin. */ public String getMyName() { return myName; }//==================================== /** * This method flips the coin. * It returns heads or tails or "side" */ public String flip() { int flipResult = rand.nextInt(2); if(flipResult == 0) return "tails"; else return "heads"; }//============================= /** * This method will print the information about the coin. */ public String toString() { return ("This is a " + myName + " with a value of $" + myValue); }//==================================================================== }