Hi, I have to write a program that plays out a whole game of the card game WAR. I have to create a bunch of classes that go along with some pre written classes so right now im trying to start out small and work my way up. We are given two enum classes of Rank and Suit which look like...

public enum Suit {

    /**
     * The suit CLUBS
     */
    CLUBS('c', "Clubs"),

    /**
     * The suit DIAMONDS
     */
    DIAMONDS('d', "Diamonds"),

    /**
     * The suit HEARTS
     */
    HEARTS('h', "Hearts"),

    /**
     * The suit SPADES
     */
    SPADES('s', "Spades");


public enum Rank {

    /**
     * The rank ACE
     */
    ACE('a', "Ace"),

    /**
     * The rank TWO (2)
     */
    TWO('2', "Two"),

    /**
     * The rank THREE (3)
     */

etc...

So far this is my Card class along with a main inserted just to test and see if my outputs are what I exspect.

    public class Card{

        private Rank rank;
        private Suit suit;
        //constructor for a single card: rank and suit
        Card( Rank rank, Suit suit){
            this.suit = suit;
            this.rank = rank;
        }
        public Rank getRank(){
            return rank;

        }

        public Suit getSuit(){
            return suit;
        }

        public String toString(){
            String temp = "";
            temp += rank;
            temp += " of ";
            temp += suit;
            return temp;

        }

        public static void main(String[] args){

            Card card1 = new Card(Rank.FOUR, Suit.CLUBS); 
            System.out.println(card1);
        }


    }

So far my output in this particular case looks like FOUR of CLUBS. My question is how do I proporly use enum values contained in completely separate classes in my current card class to get the output I want such as four of Clubs useing the values of the enum Suit and Rank.

Your enums store the text you need (eg "Ace", "Spades"), so all you need is an accessor method in the enums to return that text (or simply override toString in the enums to return it)

This article has been dead for over six months. Start a new discussion instead.