Nesting Levels

Nesting Levels with tab set at 4 characters. /--------------------------- Level 0, not indented | | /----------------------- Level 1, method headers | | | | /------------------- Level 2, statements inside of methods | | | | | | /--------------- Level 3, statements nested inside of statements | | | | that are inside of methods | | | | | | | | /----------- Level 4, statements nested inside of statements | | | | | nested inside of other statements | | | | | /////////////////////////////////////////////////////////////////////////////// // // Main Class File: Crazy8.java // File: Card.java // Semester: Spring 2013 // // Author: Deb Deppeler Copyright (2001). All rights reserved. // CS Login: deppeler // TA's Name: Leah Kluegel // // Pair Partner: Jim Skrentny // CS Login: skrentny // TA's Name: Will Benton // // Credits: none //////////////////////////// 80 columns wide ////////////////////////////////// import java.awt.Color; // needed for color of each Card

Class Header Example

/** * Represents a card with a Suit and a CardValue. * * Bugs: none known * * @author Deb Deppeler Copyright (2001) * @version 1.0 * @see also Suit, CardValue */ public class Card {

Data Members Example

/** This Card's suit: S-Spades, H-Hearts, C-Clubs, or D-Diamonds */ private Suit mySuit; /** This Card's value: Ace through King. */ private CardValue myValue; /** * Constructs a card from characters representing the suit and value. * Uppercase or lowercase characters may be used. * * @param suit a character code representing the suit. h=Heart, * s=Spade, d=Diamond, c=Club. * @param value an integer representing the value of card. * 1=Ace, 2=Two, ... , 10=Ten, 11=Jack, 12=Queen, 13=King */ private Card(char suit, int value) { this( Suit.getSuit(suit), CardValue.getCardValue(value)); } /** * Constructor: Requires a specified Suit and CardValue. * @param suit The Suit of this card, e.g. Heart, Spade, Diamond, Club. * @param value The CardValue */ private Card(Suit suit, CardValue value) { mySuit = suit; myValue = value; }

Method Header Example

/** * Returns a String representation of this Card. */ public String toString() { return mySuit.toString() + myValue.toString(); } /** * Returns true if either the suit OR the value of the Card is the same as * the specified card. * * @param card The card to compare with this card. * * @return true if either the suit or the value matches. This correct for * the game of crazy 8s, but may not be correct for other card games. * Use equals methods if both suit and value must be the same. */ public boolean matches ( Card card ) { return ( mySuit.equals ( card.mySuit ) || myValue.equals( card.myValue ) ); } /** * Returns true if the specified card has both the same Suit and CardValue * as this card. * * @param card The card to compare with this card. * * @return true if both the suit and the value match. This is too * restrictive for Crazy 8s. Use matches method if playing Crazy 8s. */ public boolean equals( Card card ) { return (( mySuit.equals(card.mySuit) ) && ( myValue.equals(card.myValue) )); } /** * Returns the CardValue of this card. * * @return The CardValue of this card. */ private CardValue getCardValue ( ) { return myValue; } /** * Returns the Suit of this card. * * @return The Suit of this card. */ private Suit getSuit() { return mySuit; } /** * Returns the color of this Card. Heart and Diamond are red and * Spade and Club are black. * * @return The Color (red or black) of this card. */ public Color getColor() { return mySuit.getColor(); } /** * Returns a comma separated value list of String representations of the cards. * @param deck An array of Card objects. * @return Each card representation after a comma */ public static String toString(Card[] deck) { return toString(deck,','); // returns deck as a comma separated list } /** * Returns a String representation of the deck of cards. * @param deck An array of Card objects. * @param delimiter character between cards, e.g. ' ', ',', '\n' * @return Each card representation after the delimiter character */ public static String toString(Card[] deck, char delimiter) { String s = "No deck"; if (null != deck && deck.length > 0) { Card c = deck[0]; s = c.toString(); for (int i=1; i < deck.length; i++ ) s += "" + delimiter + deck[i]; } return s; } /** * Returns an unshuffled deck [array] of cards. */ public static Card [] getDeck() { Card [] deck = new Card[52]; Suit [] S = {Suit.SPADE, Suit.HEART, Suit.CLUB, Suit.DIAMOND}; CardValue [] V = {CardValue.ACE, CardValue.TWO, CardValue.THREE, CardValue.FOUR, CardValue.FIVE, CardValue.SIX,CardValue.SEVEN, CardValue.EIGHT, CardValue.NINE, CardValue.TEN, CardValue.JACK, CardValue.QUEEN, CardValue.KING }; int nextCard=0; for (int s=0; s < S.length; s++ ) { for (int v=0; v < V.length; v++ ) { deck[nextCard++] = new Card( S[s] , V[v] ); } } return deck; } /** * Shuffles the deck of cards that is sent. * @param deck An array of card objects that will be returned in a different order. */ public static void shuffle(Card [] deck) { java.util.Random rng = new java.util.Random(); for (int R=deck.length-1; R > 0; R-- ) { // Pick a random number from 0 to R int r = rng.nextInt(R); // Pull that card and replace with last card Card tempCard = deck[R]; deck[R] = deck[r]; deck[r] = tempCard; } } /** * Main method gives a place to place with Card objects and methods. * @param args */ public static void main( String [] args) { Card [] deck = getDeck(); System.out.println("NEW DECK:\n"+toString(deck,',')); for ( int i = 0; i < 3; i++ ) { shuffle(deck); System.out.println("\nSHUFFLED DECK"); System.out.println(toString(deck,',')); } } } // end of Card class /** * Represents one of four possible suits in a typical deck * of cards. * * @author Deb Deppeler * @version 1.0 * * Bugs: none known */ class Suit { /** The suit known as SPADE */ public static final Suit SPADE = new Suit( Suit.SPADES ); /** The suit known as HEART */ public static final Suit HEART = new Suit( Suit.HEARTS ); /** The suit known as DIAMOND */ public static final Suit DIAMOND = new Suit( Suit.DIAMONDS ); /** The suit known as CLUB */ public static final Suit CLUB = new Suit( Suit.CLUBS ); /** A private integer representing the suit. */ private int mySuit = -1; /** Enumeration of Suit constants */ private static final int HEARTS = 0; private static final int SPADES = 1; private static final int DIAMONDS = 2; private static final int CLUBS = 3; /** * Private constructor for use in creating the public suit constants. */ private Suit ( int suit ) { if ( suit >= 0 && suit <= 3 ) mySuit = suit; } /** * Returns the Suit object that corresponds to the first character * of the string specified. * * @param suitname A string that specifies which suit to return; * spades, hearts, clubs or diamonds. Only uses the first * character of the string. * * @returns A Suit object of the specified card Suit. * Null is return, if there's no match to the first character passed. */ static Suit getSuit( String suitname ) { return getSuit( suitname.toLowerCase().charAt(0)); } /** * Returns the Suit object that corresponds to the first character * of the string specified. * * @param s A character that specifies which suit to return; * s=spades, h=hearts, c=clubs or d=diamonds. Only uses the first * character of the string. * * @returns A Suit object of the specified card Suit. * Null is return, if there's no match to the first character passed. */ static Suit getSuit( char s ) {

Local Variable Example

Suit suit = null; // temporarily hold the Suit until // it is returned to the caller. if ( s == 's' || s == 'S' ) suit = Suit.SPADE; else if ( s == 'h' || s == 'H' ) suit = Suit.HEART; else if ( s == 'c' || s == 'C' ) suit = Suit.CLUB; else if ( s == 'd' || s == 'D' ) suit = Suit.DIAMOND; return suit; } /** * Returns the color of this Suit. Heart and Diamond are red and * Spade and Club are black. * * @return The Color (red or black) of this suit. */ Color getColor() { switch ( mySuit ) { case SPADES: case CLUBS: return Color.black; case HEARTS: case DIAMONDS: return Color.red; default: return Color.white; } } /** * Returns a string of characters that tells the card's suit. */ String text() { switch ( mySuit ) { case SPADES: return "SPADES"; case HEARTS: return "HEARTS"; case CLUBS: return "CLUBS"; case DIAMONDS: return "DIAMONDS"; default: return "??????"; } } /** * Returns a string represenation of this Suit: * S for Spades, H for Hearts, C for Clubs and D for diamonds. */ public String toString() { switch (mySuit) { case SPADES: return "S"; case HEARTS: return "H"; case CLUBS: return "C"; case DIAMONDS: return "D"; default: return "?"; } } /** * Returns true if the specified suit is the same as this Suit. */ public boolean equals( Suit s ) { return (mySuit == s.mySuit); } } // end of Suit class /** * Enumerates the value of a card. * * @author Deb Deppeler, Copyright (2001) * @version 1.0 * * Bugs: none known */ class CardValue { /** The value of an Ace of any suit. */ public static final CardValue ACE = new CardValue(1); /** The value of a 2 of any suit. */ public static final CardValue TWO = new CardValue(2); /** The value of a 3 of any suit. */ public static final CardValue THREE = new CardValue(3); /** The value of a 4 of any suit. */ public static final CardValue FOUR = new CardValue(4); /** The value of a 5 of any suit. */ public static final CardValue FIVE = new CardValue(5); /** The value of a 6 of any suit. */ public static final CardValue SIX = new CardValue(6); /** The value of a 7 of any suit. */ public static final CardValue SEVEN = new CardValue(7); /** The value of an 8 of any suit. */ public static final CardValue EIGHT = new CardValue(8); /** The value of a 9 of any suit. */ public static final CardValue NINE = new CardValue(9); /** The value of an 10 of any suit. */ public static final CardValue TEN = new CardValue(10); /** The value of a Jack of any suit. */ public static final CardValue JACK = new CardValue(11); /** The value of a Queen of any suit. */ public static final CardValue QUEEN = new CardValue(12); /** The value of a King of any suit. */ public static final CardValue KING = new CardValue(13); /** A numeric value for the card for comparison. */ private int myValue = -1; /** * Default constructor is private so that users don't create * new card values. */ private CardValue() { } /** * Constructor for use in class variables. * * @param val An integer used to specify which card value to use. */ private CardValue(int val) { while ( myValue < 1 || myValue > 13 ) { switch (val) { case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12: case 13: myValue = val; break; default: System.out.println("Error: Invalid card value 1-13."); } } } /** * Returns the CardValue object with the specified value. */ static CardValue getCardValue ( int val ) { switch ( val ) { case 1: return ACE; case 2: return TWO; case 3: return THREE; case 4: return FOUR; case 5: return FIVE; case 6: return SIX; case 7: return SEVEN; case 8: return EIGHT; case 9: return NINE; case 10: return TEN; case 11: return JACK; case 12: return QUEEN; case 13: return KING; default: return null; } } /** * Overrides the default toString() method to return a string * representation of this card's value. */ public String toString() { switch ( myValue ) { case 1: return "A "; case 2: return "2 "; case 3: return "3 "; case 4: return "4 "; case 5: return "5 "; case 6: return "6 "; case 7: return "7 "; case 8: return "8 "; case 9: return "9 "; case 10: return "10"; case 11: return "J "; case 12: return "Q "; case 13: return "K "; default: return "? "; } } /** * Returns true if the integer specified matches this card value. * * @param intValue An integer value to compare with the value * of this card. (e.g. 1=ACE, 2=TWO, 3=THREE, ... 10=TEN, * 11=JACK, 12=QUEEN, 13=KING.) * * @return Returns true if the integer given matches the value * of this card as specified above. */ public boolean equals(int intValue) { return ( myValue == intValue ); } /** * Returns true if the CardValue specified is the same as the * value of the CardValue object. * * @param value a CardValue to compare with this card. * * @return True if the specified card value equals the value * of this card. */ public boolean equals(CardValue value) { return ( myValue == value.myValue ); } } // end of CardValue class