Results 1 to 16 of 16
- 04-06-2011, 06:03 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
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.
- 04-06-2011, 06:07 PM #2
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!
- 04-06-2011, 06:12 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
- 04-06-2011, 06:21 PM #4
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?How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-06-2011, 06:24 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
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;
}
- 04-06-2011, 06:28 PM #6
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?How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-06-2011, 06:31 PM #7
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
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.
- 04-06-2011, 07:13 PM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
If you're trying to find out how many cards were specified by the constructor argument 'numCards', the Card array 'cards' can tell you:
All arrays have a 'length' variable you can check.Java Code:int numberOfCards = cards.length;
- 04-06-2011, 07:19 PM #9
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-06-2011, 07:28 PM #10
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
- 04-06-2011, 10:52 PM #11
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
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
- 04-06-2011, 11:08 PM #12
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
- 04-06-2011, 11:46 PM #13
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
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.
- 04-07-2011, 12:09 AM #14
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.
- 04-07-2011, 01:26 PM #15
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!
- 04-07-2011, 11:34 PM #16
Similar Threads
-
DrJava Error
By pratzy in forum Other IDEsReplies: 7Last Post: 04-26-2010, 04:16 AM -
trouble understanding code help
By yasmin k in forum New To JavaReplies: 4Last Post: 11-16-2009, 09:46 PM -
help with drJava
By aw646475 in forum New To JavaReplies: 0Last Post: 03-15-2009, 07:32 PM -
Trouble implementing part of code into GUI
By Flaresplitz in forum New To JavaReplies: 1Last Post: 12-21-2008, 07:51 AM -
Trouble compiling code
By waelhelbawi in forum New To JavaReplies: 1Last Post: 05-12-2008, 04:25 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks