help with card program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sallyk07
    New Member
    • Dec 2006
    • 5

    #1

    help with card program

    The question says: Design and implement a class called Card that represents a standard playing card. Each card has a suit and a face value. Create a program that deals 20 random cards.

    this is the card class i made but i dont know if it right...i also don't know how to creatre a program that deals random cards.

    public class Card
    {
    public final static int spades = 0,hearts = 1, diamonds = 2, clubs = 3;
    public final static int ace = 1, jack= 11, queen = 12, king= 13;
    private final int suit;
    private final int value;
    public Card(int theValue, int theSuit)
    {
    value = theValue;
    suit = theSuit;
    }
    public int getSuit()
    {
    if (suit==0)
    return spades;
    else if (suit==1)
    return hearts;
    else if (suit==2)
    return diamonds;
    else if (suit==3)
    return clubs;
    return suit;
    }
    public int getValue()
    {
    if (value==1)
    return ace;
    else if (value==11)
    return jack;
    else if (value==12)
    return queen;
    else if (value==13)
    return king;
    return value;
    }
    public String toString()
    {
    return getSuit() + " " + getValue();
    }
    }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    rather than getSuit() and getValue() returning an integer they could return a String such as clubs, king, etc. e.g. getSuit() could be
    Code:
    // return a string representinga suit
    public String getSuit()
    {
    if (suit==0)
    return "spades";
    else if (suit==1)
    return "hearts";
    else if (suit==2)
    return "diamonds";
    else if (suit==3)
    return "clubs";
    return "error" + suit ;
    }
    to generate your random card you can use Math.random(), e.g.to get a card value between 1 and 13 inclusive
    value = (int) (Math.floor(Mat h.random()*13)) + 1; // random number between 1 and 13 inclusive

    Math.random() returns a double value greater than or equal to 0.0 and less than 1.0, multiply by 13 gives a value >=0 and < 13, floor gives us a value between 0 and 12 inclusive, it is converted to an int and 1 added to give value betwen 1 and 13 inclusive
    for example you could have a default constructor which generates a radom card

    Comment

    • sallyk07
      New Member
      • Dec 2006
      • 5

      #3
      i tried it this way but it gives me an error and i dont know how to fix it
      Card.java:19: unreachable statement
      suit = generator.nextI nt(4) + 1;
      ^
      1 error


      import java.util.Rando m;
      public class Card
      {
      private static Random generator = new Random();

      public final static int spades = 0,hearts = 1, diamonds = 2, clubs = 3;
      public final static int ace = 1, jack= 11, queen = 12, king= 13;
      private int suit;
      private int value;
      public Card(int theValue, int theSuit)
      {
      value = theValue;
      suit = theSuit;
      }
      public String getSuit()
      {
      if (suit==1)
      return "spades";
      else if (suit==2)
      return "hearts";
      else if (suit==3)
      return "diamonds";
      else if (suit==4)
      return "clubs";
      return "error" + suit;
      }
      public String getValue()
      {
      if (value==1)
      return "ace";
      if (value==2)
      return "2";
      if (value==3)
      return "3";
      if (value==4)
      return "4";
      if (value==5)
      return "5";
      if (value==6)
      return "6";
      if (value==7)
      return "7";
      if (value==8)
      return "8";
      if (value==9)
      return "9";
      if (value==10)
      return "10";
      else if (value==11)
      return "jack";
      else if (value==12)
      return "queen";
      else if (value==13)
      return "king";
      return "error" + value ;
      }
      public String toString()
      {
      return getSuit() + " " + getValue();
      }
      public int draw ()
      {
      value = generator.nextI nt(13) + 1;
      return value;
      suit = generator.nextI nt(4) + 1;
      return suit;
      }

      }

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        you were trying to return two values from function draw() which is not easy to do - why not make a default constructor which creates a random card, e.g.
        Code:
        import java.util.Random;
        public class Card
        {
        private static Random generator = new Random();
        
        public final static int spades = 0,hearts = 1, diamonds = 2, clubs = 3;
        public final static int ace = 1, jack= 11, queen = 12, king= 13;
        private int suit;
        private int value;
        
        // ** default constructor
        public Card ()
        {
        value = generator.nextInt(13) + 1;
        suit = generator.nextInt(4) + 1;
        }
        
        
        public Card(int theValue, int theSuit)
        {
        value = theValue;
        suit = theSuit;
        }
        public String getSuit()
        {
        if (suit==1)
        return "spades";
        else if (suit==2)
        return "hearts";
        else if (suit==3)
        return "diamonds";
        else if (suit==4)
        return "clubs";
        return "error" + suit;
        }
        public String getValue()
        {
        if (value==1)
        return "ace";
        if (value==2)
        return "2";
        if (value==3)
        return "3";
        if (value==4)
        return "4";
        if (value==5)
        return "5";
        if (value==6)
        return "6";
        if (value==7)
        return "7";
        if (value==8)
        return "8";
        if (value==9)
        return "9";
        if (value==10)
        return "10";
        else if (value==11)
        return "jack";
        else if (value==12)
        return "queen";
        else if (value==13)
        return "king";
        return "error" + value ;
        }
        public String toString()
        {
        return getSuit() + " " + getValue();
        }
        
        public static void main(String s[])
        {
            for (int i=1; i< 200; i++)
               {
                Card c= new Card();
                System.out.println("card " + c);
               }
        }
        }

        Comment

        • sallyk07
          New Member
          • Dec 2006
          • 5

          #5
          thanks for the help

          Comment

          Working...