We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,174 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Card game WAR card class and enum classes

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.

2
Contributors
2
Replies
17 Hours
Discussion Span
7 Months Ago
Last Updated
6
Views
Yoink
Light Poster
26 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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)

JamesCherrill
... trying to help
Moderator
8,519 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

I got it working now, thank you

Yoink
Light Poster
26 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0871 seconds using 2.72MB