/* Author: Gary Zoppetti and Stephanie Elzer * Date: March 9th, 2007 * Purpose: To demonstrate the fundamentals of writing a class */ import java.util.Random; public class Card { public enum Suit {Hearts, Clubs, Spades, Diamonds} private Suit m_suit; private char m_face; private String m_faces = "23456789TJQKA"; public Card() { m_suit = Suit.Diamonds; m_face = 'A'; } public Card(Suit suit, char face) { m_suit = suit; // check if face value supplied is valid. If the index returned is -1, it means that // the face value supplied does not occur in the m_faces string, so set the face to a // default of 'A'. if (m_faces.indexOf(face) == -1) m_face = 'A'; else m_face = face; } public void setSuit(Suit suit) { m_suit = suit; } public Suit getSuit() { return m_suit; } public void setFace(char face) { // Only change the face value of the card if the face supplied is valid // (it appears in the m_faces string). indexOf returns -1 if the character // does not appear in the string. if (m_faces.indexOf(face) != -1) m_face = face; } public char getFace() { return m_face; } public void choose() { // String representing all possible face values Random generator = new Random(); // generator random value for index of face value int faceindex = generator.nextInt(13); m_face = m_faces.charAt(faceindex); // generator random value for index of suit (0-3) int suit=generator.nextInt(4); m_suit = Suit.values()[suit]; } public String toString() { String result = m_face + " of " + m_suit; return result; } }