Results 1 to 16 of 16
  1. #1
    jonstewart is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default Having trouble with code in DrJava

    So in my csi class my assignment is to create a method called getCardCount() inside a class called Deck, that has an array of cards called cards, which then needs to be initialized.
    so, the method getCardCount() has to be an integer and it needs to return the amount of cards currently in a deck.
    So right now i have this :

    public class Deck
    {
    private Card[] cards;
    public Deck(int numCards)
    {
    this.cards = new Card[(int)numCards];
    }
    public int getCardCount()
    {
    int cardCount = 0;
    for(int index = 0; index < cards.length; index++)
    {
    cardCount = cardCount + cards[index];
    }
    }
    }

    but, whenever i try to compile it keeps telling me that "13: operator + cannot be applied to int,Card"

    I'm not sure what else i should try because that looks right to me..

    unless...should i be looping through strictly 52 (ex : for(int index = 0; index < 52; index++) etc?
    Last edited by jonstewart; 04-06-2011 at 06:05 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default

    What does it mean to add a Card to an integer count?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    jonstewart is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    What does it mean to add a Card to an integer count?
    Wouldn't that be the way to find out how many cards are currently in the deck? Also, instead of doing what i did up there, should i just be adding one to the cardCount while it's looping?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default

    Quote Originally Posted by jonstewart View Post
    Wouldn't that be the way to find out how many cards are currently in the deck?
    Well, when you're counting a deck of cards in real life, do you add the face value of each card to your count? What happens when you find a Queen?

    And other than that, Java doesn't know how to use mathematical operators on Objects like that. So even if you wanted to use the face values for some reason, you'd have to access the values first, whatever they are.

    Quote Originally Posted by jonstewart View Post
    Also, instead of doing what i did up there, should i just be adding one to the cardCount while it's looping?
    What happened when you tried that?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    jonstewart is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    Well, when you're counting a deck of cards in real life, do you add the face value of each card to your count? What happens when you find a Queen?

    And other than that, Java doesn't know how to use mathematical operators on Objects like that. So even if you wanted to use the face values for some reason, you'd have to access the values first, whatever they are.



    What happened when you tried that?
    Alright i get that, it doesn't make any sense to be adding numbers to an array of objects..right? So i changed it to this, it complies but im not sure if its correct :
    public int getCardCount()
    {
    int cardCount = 0;
    for(int index = 0; index < cards.length; index++)
    {
    cardCount = cardCount + 1;
    }
    return cardCount;
    }

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default

    Quote Originally Posted by jonstewart View Post
    Alright i get that, it doesn't make any sense to be adding numbers to an array of objects..right?
    Sort of. What I'm saying is that it doesn't make sense to add an Object to an integer. That Object might contain an integer you want to be adding, but you can't just add an Object to an int.

    Quote Originally Posted by jonstewart View Post
    i changed it to this, it complies but im not sure if its correct :
    public int getCardCount()
    {
    int cardCount = 0;
    for(int index = 0; index < cards.length; index++)
    {
    cardCount = cardCount + 1;
    }
    return cardCount;
    }
    Again, what happened when you tried it? Did you run it, or debug it at all? Are you getting the output you would expect?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    jonstewart is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    Sort of. What I'm saying is that it doesn't make sense to add an Object to an integer. That Object might contain an integer you want to be adding, but you can't just add an Object to an int.



    Again, what happened when you tried it? Did you run it, or debug it at all? Are you getting the output you would expect?
    When i run it, i get a compiled completed because there's a lot more to this assignment than this but im stuck here. For example, the next method i need is a boolean type called addCard(Card c). Again, im not adding numbers but apparently letters so it's kind of stumping me on what i should be doing instead.

  8. #8
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    If you're trying to find out how many cards were specified by the constructor argument 'numCards', the Card array 'cards' can tell you:
    Java Code:
    int numberOfCards = cards.length;
    All arrays have a 'length' variable you can check.

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default

    Quote Originally Posted by dlorde View Post
    If you're trying to find out how many cards were specified by the constructor argument 'numCards', the Card array 'cards' can tell you:
    Java Code:
    int numberOfCards = cards.length;
    All arrays have a 'length' variable you can check.
    He actually already uses this in his loop. I was going to point that out to him after he got everything sorted out a bit further.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by KevinWorkman View Post
    He actually already uses this in his loop. I was going to point that out to him after he got everything sorted out a bit further.
    Oops! so he does. Silly me... sorry.

    [He might also need to be reminded that the array has numCard spaces allocated, but doesn't hold any cards until they are explicitly inserted.]

  11. #11
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default

    I'm doing the assignment too, and you should do getCardCount after addCard because getCardCount wants to know how many cards are currently in the deck. So, what I think you might need to do is after the addCard method put the getCardCount taking into consideration any added cards. This means you have to have a counter for addCard or something like that

  12. #12
    jonstewart is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by adjit View Post
    I'm doing the assignment too, and you should do getCardCount after addCard because getCardCount wants to know how many cards are currently in the deck. So, what I think you might need to do is after the addCard method put the getCardCount taking into consideration any added cards. This means you have to have a counter for addCard or something like that
    really? in the pdf it says to do addcard after get card count. anyway i think im just going to go ask for some help during my ta's office hours because deck.java is giving me some huge problems.

  13. #13
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default

    okay, well, I don't think the order really matters because she told us to do the toString first in Card.java. Yeah, I'm gonna go to office hours just to clear some things up. But I actually don't know if the order matters, so long as the code makes sense. Taking the toString as an example, because you are calling on things below.
    But, regardless, I am using lab 9 to help me out a lot as it is pretty much the same thing, except in involves some slightly easier things concepts to do.

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    The order that you write the methods doesn't matter until you attempt to compile.

    To make your life easier add an instancve variable that keeps track of how many Cards are in the Deck. If you add a Card increase variable. Remove a Card decrease variable. Then all the getCardCount method does is return that variable.

  15. #15
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default

    Quote Originally Posted by Junky View Post
    The order that you write the methods doesn't matter until you attempt to compile.
    It does matter in that it might be easier to test if the methods are written in a particular order. For example, say you have to write two methods: a toString() method and someMethod() that uses the toString() method to do something else. It will be much easier to write the toString() method first, test it to make sure it works, then write someMethod() after you're sure toString() works than it would be to write someMethod(), have no way to test it, then write toString(), then test the whole thing, and not know why it doesn't work.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Yeah, that is why I add the qualifier "until you attempt to compile"

Similar Threads

  1. DrJava Error
    By pratzy in forum Other IDEs
    Replies: 7
    Last Post: 04-26-2010, 04:16 AM
  2. trouble understanding code help
    By yasmin k in forum New To Java
    Replies: 4
    Last Post: 11-16-2009, 09:46 PM
  3. help with drJava
    By aw646475 in forum New To Java
    Replies: 0
    Last Post: 03-15-2009, 07:32 PM
  4. Trouble implementing part of code into GUI
    By Flaresplitz in forum New To Java
    Replies: 1
    Last Post: 12-21-2008, 07:51 AM
  5. Trouble compiling code
    By waelhelbawi in forum New To Java
    Replies: 1
    Last Post: 05-12-2008, 04:25 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
  •