d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Deck Of Cards Class > Im New To This :(
Add Reply New Topic New Poll
Member
Posts: 4,598
Joined: Jul 22 2011
Gold: 3.00
Dec 4 2013 12:23pm
Im currently taking an intro to programming class for java, and I have to make a program for a user to play video poker. The Card class is required (in my case Cards) with methods getSuit getNumber and toString. This is what i have so far, but i dont know hot to put into the toString method to make it work. I made 2 arrays 0-12 (13#s for the 13 card possibilities?) and 0-3 for the 4 suit possibilities.

The problem is that I honestly cant think of what to put into the toString method
NOTE: We just learned how to create an array 2 days ago so I'm not sure if these are even right but i think they kinda make sense :(

2ldr: cant figure out what to put into toString method to make it print (Number) + " of " + (Suit);
numbers[number] + " of " + suits[suit]; (wanted to try using a line like this but i couldnt figure it out so i changed everything to try a different approach)
Code
public class Cards
{
private int number, suit;

public Cards()
{
int[] numbers = new int[12];
int[] suits = new int[3];
}
public int getSuit(int[] suits)
{
int randSuit;
Random gen = new Random();
randSuit = suits[gen.nextInt(4)];
return randSuit;
}
public int getNumber(int[] numbers)
{
int randNumber;
Random gen2 = new Random();
randNumber = numbers[gen2.nextInt(13)];
return randNumber;
}
public String toString()
{
return ?;
}
}


This post was edited by pwnage007 on Dec 4 2013 12:36pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 4 2013 12:42pm
i recommend showing the suit + number. perhaps you should map the array index to a string form for suit.
Member
Posts: 4,598
Joined: Jul 22 2011
Gold: 3.00
Dec 4 2013 12:55pm
Quote (carteblanche @ Dec 4 2013 11:42am)
i recommend showing the suit + number. perhaps you should map the array index to a string form for suit.


Having trouble understanding what you mean. Still new tho the whole idea of arrays :(
Member
Posts: 20,928
Joined: Mar 18 2009
Gold: 435,910.13
Dec 4 2013 01:55pm
is that how your instructor told you to write those classes?
if it was me i would have the constructor generating the suit/number from random and storing the result in variables i.e number/suit
then you can simply return those variables with getSuit()/getNumber() and for getString() something like

Code
return number + " of " + suit;


also why do you need array for this? Just do

Code
gen.nextInt(4);//for suit

gen.nextInt(13);//for number


nextInt(4) will return either 0,1,2,3 randomly etc
i would double check with your teacher if getSuit() is suppose to return int though, if not you may still need to make array

This post was edited by bakalolo on Dec 4 2013 02:06pm
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Dec 4 2013 02:25pm
Your entire class makes no sense at all. Have you read your own code to make sure that it makes sense or you just hammering away until something appears to work?
Member
Posts: 4,598
Joined: Jul 22 2011
Gold: 3.00
Dec 4 2013 02:49pm
Quote (rockonkenshin @ Dec 4 2013 01:25pm)
Your entire class makes no sense at all. Have you read your own code to make spray that it makes sense or you just hammering away until something appears to work?


I thought it would work. The idea I had was to create 2 arrays one for the suits one for the card numbers. Then i would use a random number generator to pick a suit and a number and add them together as mentioned by bakalolo but I was having trouble actually putting it together within the e to string Method.

I've been working on a series of if statements so that if, say the random number generator chose a 0 from the suits array it would assign it the actual suit and then it would randomly generate a number in the numbers array which would have since been assigned they're ranks (I. E 0 would represent ace 1 would represent the 2 etc

I figured it made sense :( idk tho

This post was edited by pwnage007 on Dec 4 2013 02:51pm
Member
Posts: 2,478
Joined: Jan 4 2007
Gold: 7,545.00
Dec 4 2013 02:59pm
Cards toString should be going through all of the cards and calling the Card.toString() method in my opinion.

the Card.ToString() method shouuld be printing out 'Ace of Spades'

I usually frown upon just handing out code... but here's my blackjack game I made in college way back when: https://code.google.com/p/cisc370-finalproject/source/browse/#hggameserversrccards

You can see my Card class and Deck class. I'm guessing my Deck class will be similar to your Cards...
And you can see my Shoe class for making the Decks to play Blackjack

Please do not just copy paste, look at the code and ask questions. You'll get no where if you just copy/paste.

This post was edited by DirtyRasa on Dec 4 2013 03:02pm
Member
Posts: 4,598
Joined: Jul 22 2011
Gold: 3.00
Dec 4 2013 03:16pm
Quote (DirtyRasa @ Dec 4 2013 01:59pm)
Cards toString should be going through all of the cards and calling the Card.toString() method in my opinion.

the Card.ToString() method shouuld be printing out 'Ace of Spades'

I usually frown upon just handing out code... but here's my blackjack game I made in college way back when:  https://code.google.com/p/cisc370-finalproject/source/browse/#hggameserversrccards

You can see my Card class and Deck class.  I'm guessing my Deck class will be similar to your Cards...
And you can see my Shoe class for making the Decks to play Blackjack
Please do not just copy paste, look at the code and ask questions.  You'll get no where if you just copy/paste.



thank you, taking a look at it right now. Wont copy paste, i'd like to learn this stuff and create my own :)

my plan as of right now is to do it the long way by setting a new constructor

Code
public Cards(int numbers, int suits)
{
number = numbers;
suit = suits;
}


and several if statements within the toString() method such as

Code
String a = "";
String b = "";
if(number == 1)
{
a += " Ace ";
}
if(suit == 1)
{
b += "Spades";
}
return a + " of " + b;


Should return Ace of Spades. The get suit and Get number methods will just return suit and number respectively instead as well

for each card. Ill implement the random # generator within the actual testerclass in the main method that inputs a random # for the constructor's arguments. hoping itll work, and if i can get that done then i can get to trying to build a better program


tyvm for the help btw guys

This post was edited by pwnage007 on Dec 4 2013 03:24pm
Member
Posts: 6,457
Joined: Mar 12 2007
Gold: 6,745.00
Dec 5 2013 02:28am
A simpler/cleaner way to do the to string method would be to create a string array for the suit and card value. Declare 2 string arrays and fill it with {"hearts", "spades", etc}, same for card vaue, {"ace", "king", etc}

Then instead of having a bunch of if staements or a switch you can just
return value[a] + " of " + suit[b];
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll