438,034 Members | 819 Online 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
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 // 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(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 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);        } } }       Jan 1 '07 #4

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