Results 1 to 14 of 14

Thread: Simple Question

  1. #1
    stackptr89 is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default Simple Question

    Java Code:
    public class DeckofCards 
    {
    	public Card[] deck;
    	int blah;
    	
    	public DeckofCards()
    	{	
    		deck = new Card[ blah];	
    	}
    		
    	public void initialize()
    	{
    		String [] faces = {" blah blah" };
    		String [] suits = {"blah blah"};
    		currentCard=0;
    		deck[currentCard]= new Card ( faces[currentCard], suits[currentCard]);
    		
    		for (blah blah blah)
    		{
    			deck[count]= new Card (faces[count%13], suits[count/13]);		
    		}	
    	}	
    		//End Method	
    }
    I wish to create a method within same class to print the deck array. It works when I place it within the same method but I wish to print the array in a separate method.

  2. #2
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    Java Code:
    public void printDeck(Card[] deck){
    blah blah blah
    }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by hosscomp View Post
    Java Code:
    public void printDeck(Card[] deck){
    blah blah blah
    }
    I wouldn't even give this method a Card[] parameter since it's an instance method of the class and would print the Card[] held by the current object.

  4. #4
    stackptr89 is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    how can I do that?
    public void printDeck(Card [] deck){
    blah
    }

    compiles without errors but when I try:
    DeckofCards myDeck = new DeckofCards();
    myDeck.printDeck();

    I get an error :(

  5. #5
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    I wouldn't even give this method a Card[] parameter since it's an instance method of the class and would print the Card[] held by the current object.
    I agree. Just use this.deck.

  6. #6
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    Quote Originally Posted by stackptr89 View Post
    how can I do that?
    public void printDeck(Card [] deck){
    blah
    }

    compiles without errors but when I try:
    DeckofCards myDeck = new DeckofCards();
    myDeck.printDeck();

    I get an error :(
    Declare it like this:
    Java Code:
    public void printDeck(){
            blah
        }

  7. #7
    stackptr89 is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by hosscomp View Post
    I agree. Just use this.deck.
    what do you mean???

  8. #8
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    Quote Originally Posted by stackptr89 View Post
    what do you mean???
    You can reference deck from any member method of the class.
    You may have to post your code for us to see what you are asking.

  9. #9
    stackptr89 is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    Java Code:
    public class Card
    {
    	public String face;
    	public String suit;
    	
    	public Card ( String cardFace, String cardSuit)
    		{
    			face = cardFace;
    			suit = cardSuit;
    		}
    	public String toString()
    		{
    			return face + " of " + suit;
    		}			
    }
    //End Class Card
    	 
    public class DeckofCards 
    {
    	public Card[] deck;
    	private int currentCard;
    	private static final int NUMBER_OF_CARDS = 52;
    	public String [] Array = new String[52];
    	
    	public DeckofCards()
    	{		
    		deck = new Card[ NUMBER_OF_CARDS];
    	}
    		
    	public void printer()
    	{
    		String [] faces = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
    		String [] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};
    		currentCard=0;
    		deck[currentCard]= new Card ( faces[currentCard], suits[currentCard]);
    		
    		for (int count=0; count <deck.length; count++)
    		{
    			deck[count]= new Card (faces[count%13], suits[count/13]);			
    		}		
    	}	
    	
    	public void printDeck()
           {
    		System.out.printf("%-19s\n", deck[0]);
    	}	
    }
    //End DeckofCards	 
    public class DeckofCardsTest
    {
    	public static void main (String [] args)
            {
    	     Card [] deck;
    	     DeckofCards myDeck = new DeckofCards();
    	     myDeck.printDeck();
             }
    }
    Last edited by stackptr89; 01-29-2011 at 07:58 AM.

  10. #10
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    printDeck(deck[]) in DeckofCards cannot be applied to ()
    Because you declare it as taking (deck[]) as a parameter.
    Declare it with empty parens:
    public void printDeck()

    You also have other problems. The method called printer() is actually the constructor for DeckOfCards. Its contents should be in constructor instead of the one line that is there.

  11. #11
    stackptr89 is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    so everything compiles fine. the only problem is that it prints a null array instead of the card objects
    Last edited by stackptr89; 01-29-2011 at 07:59 AM.

  12. #12
    stackptr89 is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    how can I get it so it prints out the card objects?

  13. #13
    ThrashingBoy is offline Member
    Join Date
    Apr 2010
    Location
    Phoenix, AZ
    Posts
    25
    Rep Power
    0

    Default

    My first program was a poker game which I MOSTLY finished lol. But I trust me, you don't want to create your deck with 2 arrays. Look up what an ArrayList<> is. Trust me, it works so well with a deck of cards. Also, you will need to set the suit and rank as integers if you are going to want to compare the hand strengths eventually. Using an ArrayList should also help out with your ability to print out the cards as well too. And I messed with you code a bit more and don't know of a way to make it print on the screen the way you want doing it the way you are doing it. But I am not a greatly experienced programmer either.
    Last edited by ThrashingBoy; 01-29-2011 at 09:28 AM.

  14. #14
    stackptr89 is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    thanks for the suggestion. will definitely start over today

Similar Threads

  1. simple question. thanks in advance fro your help
    By farahm in forum Advanced Java
    Replies: 3
    Last Post: 11-22-2010, 11:58 AM
  2. very simple Question
    By arsenal4ever_11 in forum NetBeans
    Replies: 2
    Last Post: 05-27-2010, 09:51 PM
  3. some simple question?
    By jperson in forum New To Java
    Replies: 4
    Last Post: 05-03-2010, 06:32 PM
  4. Simple Question
    By barusk in forum Networking
    Replies: 13
    Last Post: 03-04-2009, 08:33 PM
  5. Probably a really simple question...
    By ibanez270dx in forum New To Java
    Replies: 0
    Last Post: 11-16-2007, 02:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •