/** Coin class @author A. Ozgelen */ public class Coin { // constructor - default public Coin(){ value = 25; flip(); } // constructor - overloaded public Coin(int value) { this.value = value ; flip(); } // end of c'tor // flip - randomly picks head or tail and assigns it to face public void flip() { face = (int) (Math.random() * 2); } // accessor method to face public int getFace() { return face; } // accessor method to value public int getValue() { return value; } public String toString() { String facename; if(face == HEADS) { facename = "heads"; } else { facename = "tails"; } return (Integer.toString(value) + " cent coin, face: " + facename); } // static constants public static final int HEADS = 0 ; public static final int TAILS = 1 ; // data fields private int face ; // heads or tails private int value ; // in cents // unit test public static void main (String[] args) { // instantiate one of each: quarter, dime, nickel, penny Coin quarter = new Coin(25); Coin dime = new Coin(10); Coin nickel = new Coin(5); Coin penny = new Coin(1); // print each objects face and value using toString System.out.println(quarter.toString()); System.out.println(dime.toString()); System.out.println(nickel.toString()); System.out.println(penny.toString()); // test flip on each object quarter.flip(); dime.flip(); nickel.flip(); penny.flip(); // print each objects face and value using getFace and getValue methods System.out.println("value:" + quarter.getValue() + ", face:" + quarter.getFace()); System.out.println("value:" + dime.getValue() + ", face:" + dime.getFace()); System.out.println("value:" + nickel.getValue() + ", face:" + nickel.getFace()); System.out.println("value:" + penny.getValue() + ", face:" + penny.getFace()); } } // end of class Coin