My deck create an array of Card objects, and it prints out successfully that it is updated. I want to make a third class to implement the program: create a deck object, ask players how many card they want and then pass them out randomly. In the PlayingTable class at the bottom I try to create an instance of a deck and scan it to make all the information in the deck is there. It says I can’t scan a deck object because it’s not an array, and I’m not quite sure where to go from here….
Here’s my code for the two classes:
public class Card
{
//Instance properties//
private String cardNum;
private String cardSuit;
//Instance Methods //
//Constructor, create an instance of card
public Card( String num , String suit )
{
System.out.println( "Creating a card.");
cardNum = num ;
cardSuit = suit;
}
public void display()
{
System.out.println("This card is a" + " "+ cardNum + cardSuit);
}
}
public class Deck
{
// Instance properties//
//Number of cards in a deck
public static int numCards = 52;
private static Card [ ] deck= new Card [numCards];
private static String [ ] cardValue = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" } ;
private static String [ ] cardSuit = { " of Hearts" , " of Spades", " of Clubs", " of Diamonds" };
//Instance methods and Constructor//
/*public void Deck ( )
{
}*/
public static void fillDeck ( )
{
int eachCard = 0 ;
for ( int i = 0; i < cardValue.length ; i++ )
{
for ( int j = 0 ; j < cardSuit.length ; j++ )
{
deck[eachCard++] = new Card ( cardValue [i] , cardSuit [j] );
}
}
}
public static void getDeck ()
{
for ( int i = 0; i < numCards ; i++ )
{
fillDeck();
//System.out.println( " This is card" + i + " " + " of the deck" );
deck[i].display();
}
}
public static void main (String [] args )
{
getDeck();
}
}
public class PlayingTable
{
static Deck dealersDeck = new Deck();
public static void main ( String [] args )
{
for ( int i = 0 ; i < 52; i++)
{
System.out.println ( dealersDeck[i].getDeck());
}
}
}
Q: How do I display information form one class … [from] … another class?
A: Provide public “getter” methods (or, if you’re programming #, “properties”) in the “other class”.
Here’s a good tutorial on C# properties:
And a discussion on Java “getter/setter” methods:
Finally, here’s a discussion on OO “encapsulation”, an issue that actually transcends “properties”, “getter/setter methods” … AND … “displaying information in one class from another”: