0

I am attempting to create a Set game(www.setgame.com) as part of a project for school. This class is supposed to take a deck of 81 cards and assign them each a color, shape, number, and shading. I attempting to just do the color part first, so I know how to get on the right track and complete the rest, and have thus simplified it to making them all the color "red". I keep getting an error message saying java.lang.String cannot be converted to SetDeck. I don't know how I am to fix this issue. This leads me to the next part of the question how am I to blend my array to contain string elements and int elements. From what research I have done this is a no no and while it can be done, its much easier on you if they are separate. While I would love to do it easily, the project wants a printout in the end from the array saying something like array[0]= "red oval 1 filled". The code I have written this far in this class is below.

If you require any further explanation or clarity please ask. I have no prior coding experience so I will try my best to explain even further.

public class SetDeck{

private SetDeck [] deck;
private int top;
public SetDeck(){
    top = 0;
    deck = new SetDeck[81];
    for(int r = 0; r < 80; r++){
        deck[r] = "red";
    }
} 
2
  • Your deck array holds SetDeck objects, not String objects, and you're trying to fill it with String. But your design seems off -- if done correctly, if you did fill that array with valid SetDeck objects, the code would throw a StackOverflow. Why would a SetDeck hold an array of itself? Commented Dec 3, 2016 at 19:27
  • Your class is missing variables to hold the color, shape, number and shading for each card. Commented Dec 3, 2016 at 19:32

3 Answers 3

4
public class SetDeck{

    private SetDeck [] deck;

You are embeding the same class into itself. You should have a Card class, which has the color, shape, number, and shading. Then embed an array or ArrayList of Cards into the SetDeck class, and within the SetDeck constructor, fill this array or ArrayList.

Sign up to request clarification or add additional context in comments.

5 Comments

@HovercraftFullOfEels Yes. Now could we get along better in the future? I know you'll be around here on SO for as long as I will.
@BrandonIbbotson: I'm sorry, I honestly don't recall past interactions with you, or in particular bad interactions. If I was rude, I honestly do ask for forgiveness.
@HovercraftFullOfEels You weren't rude, but our opinions have clashed multiple times before. I hope for a better future. Regards.
@BrandonIbbotson: and I look forward to working together in the future.
God I've been struggling with this over a mere typo. I feel like the biggest idiot of all time. Thank you.
3

Your code has several problems:

public class SetDeck{
    private SetDeck [] deck;

First you create an instance array variable of SetDeck within the SetDeck class. Then within the constructor you try to almost fill this array with String. I say almost because you've declared the array to hold 81 items but you only fill 80 of them:

    public SetDeck() {
        top = 0;
        deck = new SetDeck[81];
        for(int r = 0; r < 80; r++){
            deck[r] = "red";
        }
    } 
}

Problems:

  1. The deck array does not hold String objects, and yet you're trying to put them into the array. The compiler is right to complain to you that this just isn't allowed.
  2. If you did what your code initially suggests that it wants to do -- create 80 SetDeck objects within the SetDeck constructor, this would cause infinite recursion, since the constructor for each of those 80 SetDeck objects would be called, and inside of these constructors, they would create 80 more SetDeck objects which would create 80 more SetDeck objects which would create 80 more SetDeck objects which .... until your program ran out of stack memory. Plus it just doesn't make sense for SetDeck to hold a bunch of itself in an instance field. A static field perhaps, but not in this situation.
  3. You mention that the deck will hold Card objects, but your code does not show a Card class.
  4. You state that the Card objects would have certain properties, including color, shape, filled or not, but you are only creating a color String when trying to fill your array.

Solutions:

  • Create a Card class, one that has fields for all the properties that you mention.
  • Create a Deck class and have it hold either an array of Card, or maybe better an ArrayList<Card> of Card.
  • Fill the collection of Cards in your Deck constructor.
  • Don't try to make Strings something that they're not.
  • Avoid using "magic" numbers when filling arrays or collections. Array has a length property that you will want to use in your for loops, and ArrayList has a size() method that serves a similar function.

2 Comments

God I've been struggling with this over a mere typo. I feel like the biggest idiot of all time. Thank you.
Not yet this class will field the properties I have mentioned above, I was just struggling to start it all. The professor insists I have to go in order so I could not do the rest until I resolved the issue with this first. I see now the fact I named the class and the constructor the same. I meant to use SetCard from my other class instead. Thank you for the very informative response as to what I have made mistakes on.
-1

Declaring

private String[] deck;

and

deck = new String[81];

will fix the issue

1 Comment

That will eliminate the error message, but it won't solve the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.