By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,034 Members | 819 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,034 IT Pros & Developers. It's quick & easy.

help with card program

P: 5
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();
}
}
Dec 31 '06 #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 1,510
rather than getSuit() and getValue() returning an integer they could return a String such as clubs, king, etc. e.g. getSuit() could be
Expand|Select|Wrap|Line Numbers
  1. // return a string representinga suit
  2. public String getSuit()
  3. {
  4. if (suit==0)
  5. return "spades";
  6. else if (suit==1)
  7. return "hearts";
  8. else if (suit==2)
  9. return "diamonds";
  10. else if (suit==3)
  11. return "clubs";
  12. return "error" + suit ;
  13. }
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(Math.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
Dec 31 '06 #2

P: 5
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.nextInt(4) + 1;
^
1 error


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;
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.nextInt(13) + 1;
return value;
suit = generator.nextInt(4) + 1;
return suit;
}

}
Dec 31 '06 #3

Expert 100+
P: 1,510
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.
Expand|Select|Wrap|Line Numbers
  1. import java.util.Random;
  2. public class Card
  3. {
  4. private static Random generator = new Random();
  5.  
  6. public final static int spades = 0,hearts = 1, diamonds = 2, clubs = 3;
  7. public final static int ace = 1, jack= 11, queen = 12, king= 13;
  8. private int suit;
  9. private int value;
  10.  
  11. // ** default constructor
  12. public Card ()
  13. {
  14. value = generator.nextInt(13) + 1;
  15. suit = generator.nextInt(4) + 1;
  16. }
  17.  
  18.  
  19. public Card(int theValue, int theSuit)
  20. {
  21. value = theValue;
  22. suit = theSuit;
  23. }
  24. public String getSuit()
  25. {
  26. if (suit==1)
  27. return "spades";
  28. else if (suit==2)
  29. return "hearts";
  30. else if (suit==3)
  31. return "diamonds";
  32. else if (suit==4)
  33. return "clubs";
  34. return "error" + suit;
  35. }
  36. public String getValue()
  37. {
  38. if (value==1)
  39. return "ace";
  40. if (value==2)
  41. return "2";
  42. if (value==3)
  43. return "3";
  44. if (value==4)
  45. return "4";
  46. if (value==5)
  47. return "5";
  48. if (value==6)
  49. return "6";
  50. if (value==7)
  51. return "7";
  52. if (value==8)
  53. return "8";
  54. if (value==9)
  55. return "9";
  56. if (value==10)
  57. return "10";
  58. else if (value==11)
  59. return "jack";
  60. else if (value==12)
  61. return "queen";
  62. else if (value==13)
  63. return "king";
  64. return "error" + value ;
  65. }
  66. public String toString()
  67. {
  68. return getSuit() + " " + getValue();
  69. }
  70.  
  71. public static void main(String s[])
  72. {
  73.     for (int i=1; i< 200; i++)
  74.        {
  75.         Card c= new Card();
  76.         System.out.println("card " + c);
  77.        }
  78. }
  79. }
  80.  
  81.  
  82.  
Jan 1 '07 #4

P: 5
thanks for the help
Jan 1 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.