473,466 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help with making playing cards

3 New Member
Hi, I have been working on this project for about three days now, and seem to be stuck on my conditional statements that specify the face and suit of the card when it is a Jack, Queen, King and Ace. Could someone take a look at my code and give me some helpful hints on what to change to get it to work? Any help is appreciated.

Expand|Select|Wrap|Line Numbers
  1. import java.util.Random;
  2. /**
  3.  * 
  4.  * @author jparrish
  5.  * 
  6.  * This class encapsulates a playing card.
  7.  *
  8.  */
  9. public class Card {
  10.     private int suit;
  11.     private int face;
  12.  
  13.     Random generator = new Random();
  14.  
  15.     public Card() {
  16.         suit = generator.nextInt(4) + 1;
  17.         face = generator.nextInt(12) + 2;
  18.     }
  19.  
  20.     public Card(int suit, int face) {
  21.         suit = this.suit;
  22.         face = this.face;
  23.     }
  24.  
  25.     public int getSuit() {
  26.         return suit;
  27.     }
  28.  
  29.     public void setSuit(int suit) {
  30.         this.suit = suit;
  31.     }
  32.  
  33.     public int getFace() {
  34.         return face;
  35.     }
  36.  
  37.     public void setFace(int face) {
  38.         this.face = face;
  39.     }
  40.  
  41.     public String toString() {
  42.         if (this.suit == 1) {
  43.             return this.face + " of Clubs";
  44.         }
  45.         if (this.suit == 2) {
  46.             return this.face + " of Diamonds";
  47.         }
  48.         if (this.suit == 3) {
  49.             return this.face + " of Hearts";
  50.         }
  51.         if (this.suit == 4) {
  52.             return this.face + " of Spades";
  53.         }
  54.         if (this.face == 11) {
  55.             return "Jack of " + this.suit;
  56.         }
  57.         if (this.face == 12) {
  58.             return "Queen of ";
  59.         }
  60.         if (this.face == 13) {
  61.             return "King of ";
  62.         }
  63.         if (this.face == 14) {
  64.             return "Ace of ";
  65.         }
  66.  
  67.  
  68.         return this.getFace() + " " + this.getSuit();
  69.     }
  70.  
  71.     public boolean equals(Card card) {
  72.         if (card.getSuit() == this.getSuit()
  73.                 && card.getFace() == this.getFace())
  74.             return true;
  75.         else
  76.             return false;
  77.     }
  78.  
  79. }
  80.  
Nov 24 '07 #1
5 2987
JosAH
11,448 Recognized Expert MVP
I'd say for starters change that 12 to 13 for your maximum face value.

kind regards,

Jos
Nov 24 '07 #2
slither101
3 New Member
Jos, thank you for the reply. I have changed my max face value to 13, but I still need help with my whole suit issue. If I put the suit statements before the face statements, then the suit will show up on all faces except it doesn't display the face values of jack, queen, king, and ace. instead it will just say 11 of spades, 12 of hearts, etc. I appreciate that tip, and all other advice is welcome. Thank you for your time.

Josh.

Oh, and here is my up to date code.

Expand|Select|Wrap|Line Numbers
  1. import java.util.Random;
  2. /**
  3.  * 
  4.  * @author jparrish
  5.  * 
  6.  * This class encapsulates a playing card.
  7.  *
  8.  */
  9. public class Card {
  10.     private int suit;
  11.     private int face;
  12.  
  13.     Random generator = new Random();
  14.  
  15.     public Card() {
  16.         suit = generator.nextInt(4) + 1;
  17.         face = generator.nextInt(13) + 2;
  18.     }
  19.  
  20.     public Card(int suit, int face) {
  21.         suit = this.suit;
  22.         face = this.face;
  23.     }
  24.  
  25.     public int getSuit() {
  26.         return suit;
  27.     }
  28.  
  29.     public void setSuit(int suit) {
  30.         this.suit = suit;
  31.     }
  32.  
  33.     public int getFace() {
  34.         return face;
  35.     }
  36.  
  37.     public void setFace(int face) {
  38.         this.face = face;
  39.     }
  40.  
  41.     public String toString() {
  42.         if (this.suit == 1) {
  43.             return this.face + " of Clubs";
  44.         }
  45.         if (this.suit == 2) {
  46.             return this.face + " of Diamonds";
  47.         }
  48.         if (this.suit == 3) {
  49.             return this.face + " of Hearts";
  50.         }
  51.         if (this.suit == 4) {
  52.             return this.face + " of Spades";
  53.         }
  54.         if (this.face == 11) {
  55.             return "Jack of " + this.suit;
  56.         }
  57.         if (this.face == 12) {
  58.             return "Queen of ";
  59.         }
  60.         if (this.face == 13) {
  61.             return "King of ";
  62.         }
  63.         if (this.face == 14) {
  64.             return "Ace of ";
  65.         }
  66.  
  67.  
  68.         return this.getFace() + " " + this.getSuit();
  69.     }
  70.  
  71.     public boolean equals(Card card) {
  72.         if (card.getSuit() == this.getSuit()
  73.                 && card.getFace() == this.getFace())
  74.             return true;
  75.         else
  76.             return false;
  77.     }
  78.  
  79. }
  80.  
Nov 25 '07 #3
JosAH
11,448 Recognized Expert MVP
Jos, thank you for the reply. I have changed my max face value to 13, but I still need help with my whole suit issue. If I put the suit statements before the face statements, then the suit will show up on all faces except it doesn't display the face values of jack, queen, king, and ace. instead it will just say 11 of spades, 12 of hearts, etc. I appreciate that tip, and all other advice is welcome. Thank you for your time.
Your toString() method is goofy, i.e. after you've found the suit (and you always)
find one of them) you simply return a String using the numerical value of the face
value. Better split that method up in a few methods: one for the face values and one
for the suit values. Your toString() method will be something like this then:

Expand|Select|Wrap|Line Numbers
  1. public String toString() { return getFaceName()+" of "+getSuitName(); }
  2.  
kind regards,

Jos
Nov 25 '07 #4
slither101
3 New Member
Hey Jos, thanks for the help! I got it working now. I appreciate you taking your time to help a new programmer out. Thank you so much. Here's my final code. Thanks!

Expand|Select|Wrap|Line Numbers
  1. import java.util.Random;
  2. /**
  3.  * 
  4.  * @author jparrish
  5.  * 
  6.  * This class encapsulates a playing card.
  7.  *
  8.  */
  9. public class Card {
  10.     private int suit;
  11.     private int face;
  12.  
  13.     Random generator = new Random();
  14.  
  15.     public Card() {
  16.         suit = generator.nextInt(4) + 1;
  17.         face = generator.nextInt(13) + 2;
  18.     }
  19.  
  20.     public Card(int suit, int face) {
  21.         suit = this.suit;
  22.         face = this.face;
  23.     }
  24.  
  25.     public int getSuit() {
  26.         return suit;
  27.     }
  28.  
  29.     public void setSuit(int suit) {
  30.         this.suit = suit;
  31.     }
  32.  
  33.     public int getFace() {
  34.         return face;
  35.     }
  36.  
  37.     public void setFace(int face) {
  38.         this.face = face;
  39.     }
  40.  
  41.     public String getFaceName(){
  42.         if(this.getFace() == 2){
  43.             return "2";
  44.         }
  45.         if(this.getFace() == 3){
  46.             return "3";
  47.         }
  48.         if(this.getFace() == 4){
  49.             return "4";
  50.         }
  51.         if(this.getFace() == 5){
  52.             return "5";
  53.         }
  54.         if(this.getFace() == 6){
  55.             return "6";
  56.         }
  57.         if(this.getFace() == 7){
  58.             return "7";
  59.         }
  60.         if(this.getFace() == 8){
  61.             return "8";
  62.         }
  63.         if(this.getFace() == 9){
  64.             return "9";
  65.         }
  66.         if(this.getFace() == 10){
  67.             return "10";
  68.         }
  69.         if(this.getFace() == 11){
  70.             return "Jack";
  71.         }
  72.         if(this.getFace() == 12){
  73.             return "Queen";
  74.         }
  75.         if(this.getFace() == 13){
  76.             return "King";
  77.         }
  78.         if(this.getFace() == 14){
  79.             return "Ace";
  80.         }
  81.         return this.getFaceName();
  82.  
  83.     }
  84.     public String getSuitName(){
  85.         if(this.getSuit() == 1){
  86.             return "Clubs";
  87.         }
  88.         if(this.getSuit() == 2){
  89.             return "Hearts";
  90.         }
  91.         if(this.getSuit() == 3){
  92.             return "Diamonds";
  93.         }
  94.         if(this.getSuit() == 4){
  95.             return "Spades";
  96.         }
  97.         return this.getSuitName();
  98.     }
  99.  
  100.     public String toString() {
  101.         return getFaceName() + " of " + getSuitName();
  102.     }
  103.  
  104.     public boolean equals(Card card) {
  105.         if (card.getSuit() == this.getSuit()
  106.                 && card.getFace() == this.getFace())
  107.             return true;
  108.         else
  109.             return false;
  110.     }
  111.  
  112. }
  113.  
-Josh
Nov 26 '07 #5
JosAH
11,448 Recognized Expert MVP
Hey Jos, thanks for the help! I got it working now. I appreciate you taking your time to help a new programmer out. Thank you so much. Here's my final code. Thanks!
You're welcome of course. Hint: you can use switch statements for the suit and
face names; the face method can even be this:

Expand|Select|Wrap|Line Numbers
  1. switch (face) {
  2.    case 11: return "jack";
  3.    case 12: return "queen";
  4.    case 13: return "king";
  5.    case 14: return "ace";
  6.    default: return ""+face;
  7. }
  8.  
kind regards,

Jos
Nov 26 '07 #6

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

Similar topics

10
by: Case Nelson | last post by:
Hi there I've just been playing around with some python code and I've got a fun little optimization problem I could use some help with. Basically, the program needs to take in a random list of no...
4
by: Greg Baker | last post by:
I don't know what standard protocol is in this newsgroup. Am I allowed to post code and ask for help? I hope so.. :) Here's my problem: I am trying problem 127 of the valladolid online...
4
by: N3TB1N | last post by:
Here is my assignment. I am hoping that someone here quickly knows all of the correct answers... especially for question #5 and everything after. Thanks in advance. ...
2
by: shirin.no87 | last post by:
how to make solitaire game of win xp with c++? the game should be in this way: 1:deal:to begin the game 28 cards are dealt into seven piles the leftmost pile has one card the next tow cards...
1
by: Nefrit | last post by:
OK this is my situation: This coming Friday I will be getting Telewest broadband installed in my house, along with my PC there are 3 other PC's and 1 Laptop, making a total of 5 all using Windows....
2
by: DesiShaddy | last post by:
Hi Guys, I have all my code working except sort function . I need to sort the cards in hand......and I am having hard time with that.... Any help would be really helpful ;) class hand{...
8
by: garyrowell | last post by:
I have been at this programme for hours trying to work out what is wrong. Any help would be very much appricated. Here is the breif I received. The program This week you are going to write three...
32
by: falconsx23 | last post by:
I am making a game called Set, it is a card game: here is a brief description of the rules: The game of Set is played with 81 cards, each of which has 4 properties (number, pattern, color and...
3
by: mProgramz | last post by:
//Specification: This program plays a version of //the card game of 21. //A human player is pitted against the computer. //The player who is the closest to 21 without //going over wins the hand. ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.