简体   繁体   中英

How to get my ADT stack to show its contents

I am trying to create an ADT stack for a deck of cards that will perform different functions through the interface. I am trying to get the show function to work and have provided my code below. The only error I am getting is within the for statement in the line

deck [i] = cards [i %13] + suit [i / 13];

The error states that the required type is T and the type that is occurring is String. I understand this must be the card and suits array being of type string but when I change them to private type T the contents is underlined in red and says that the type of the contents is String and that type T is expected. I'm not sure what I am doing wrong and any guidance would be appreciated, thanks.

public final class deckOfCards<T> implements CardInterface<T> {


    String [] suit = {"Hearts", "Diamonds", "Spades", "Clubs"};
    String[] cards = {"2 of", "3 of", "4 of", "5 of", "6 of", "7 of", "8 of", 
    "9 of", "10 of", 
    "Jack of", "Queen of", "King of", "Ace of"};
    private T [] deck;
    private int topIndex;
    private boolean initialized = false;
    private static final int DECK_SIZE = 52;

    public deckOfCards(){this(DECK_SIZE);}

    public deckOfCards(int initialCapacity) {
    // Check the initial capacity:
    checkDeck(initialCapacity);

    T [] tempStack = (T[]) new Object[initialCapacity];
    deck = tempStack;
    topIndex = -1;
    initialized = true;
 }


    public T show() {




    T top = null;


    for (int i = 0; i < deck.length; i++) {

        deck[i] =  cards[i % 13] +  suit[i / 13];
        System.out.println(deck);
    }

    return top;
}

Although you said that you were trying to make an Abstract Data Type (ADT) for Stack, that doesn't appear to be what your code does. Your code has lots of desk-of-cards application specific code that doesn't belong in an Abstract Data Type.

If you wanted to write your own Stack class, perhaps as an academic project, you would want to write a class that could be used with different kinds of objects on different projects in the future. That's the purpose of the <T> in the class declaration - it lets you write the class now, and provide the actual element type later when the class is used.

For example, you could write a Stack class like this:

public class Stack<T> {

    // declare your fields here (an array of T would be a good idea)

    public Stack() {
        // write the body of your class constructor here
    }

    public void push(T item) {
        // write code to add a new item to the top of the stack
    }

    public T pop() {
        // write code to return and remove the top item on the stack
    }
}

Then, you could use your Stack in a card playing application by making a Stack of Cards:

Stack<Card> deck = new Stack<>();

or a Stack of Strings:

Stack<String> deck = new Stack<>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM