简体   繁体   中英

How to make a private node class located in its own file visible to my deck class (LINKEDLIST)?

So I have this project and basically my professor wants the node class to be in a separate file instead of being inside my actual deck class. As a result, my deck class methods can no longer use the node data types and won't work. My professor only gave me the tip of using get, set methods in order to allow my deck class to access the node class when it's in a different file. I'm not sure how to do this so any help to figure out how my get, set methods set up so they can be read by my Deck class would be appreciated!

Code from my deck class:

package card;

import java.io.*;
import java.util.*;

public class Deck<Card> implements DeckInterface<Card> {
    
    
    private Node frontOfDeck; // Cannot be final due to doubling
    private int numberOfEntries; 
         
    /**
     * Creates a deck of cards whose default capacity is 52. 
    */   
    public Deck() {
        frontOfDeck = null;
        numberOfEntries = 0;
    } // end default constructor
    
    
    //sets private data member
    public void setNumOfEntries(int numberOfEntries) {
        this.numberOfEntries = numberOfEntries;
    }
    //returns number of entries
    public int getNumOfEntries() {
        return numberOfEntries; 
    }
    
       
    /** Adds a new card to this deck.
     * @return True.
     * @param aCard
    */
    public boolean add(Card aCard) {
        //Beginning of chain
        Node newNode = new Node(aCard);
        newNode.setNextNode(frontOfDeck);
        //Make new node reference rest of chain
        //frontOfDeck is nulll if chain is empty
        frontOfDeck = newNode; //New node is at beginning of chain
        numberOfEntries++;
    
    return true;
        
    } //end add
    
    
    /**
     * Sees if deck is empty
     * @return true if empty, or false if not
     */
    public boolean isEmpty() {
        return numberOfEntries == 0;
    } //end isEmpty
    
    
    public int getFrequencyOf(Card aCard) {
        int frequency = 0;
        int loopCounter = 0;
        Node currentNode = frontOfDeck;
        
        while ((loopCounter < numberOfEntries) && (currentNode != null)) {
            if (aCard.toString().trim().equals(currentNode.data.toString().trim())){
                frequency++;}
            loopCounter++;
            currentNode = currentNode.next;
        }
        
        return frequency;
        
    }
    
    public int getCardDeck(Card aCard) {
        int numCards = 0;
        int loopCounter = 0;
        Node currentNode = frontOfDeck;
        
        while ((loopCounter < numberOfEntries) && (currentNode != null)) {
            //if (aCard.equals(currentNode.data))
            numCards++;
            System.out.println(currentNode.getData());
            loopCounter++;
            currentNode = currentNode.next;
        }

        return numCards;
        
    }
    
    public Card remove() {
        Card result = null;
        if(frontOfDeck != null) {
            result = frontOfDeck.data;
            frontOfDeck = frontOfDeck.next;
            numberOfEntries--;
        }
        
        return result;
        
    }
    
    public boolean removeSpecific(Card aCard) {
        boolean result = false;
        Node nodeN = getReferenceTo(aCard);
        if (nodeN != null) {
            nodeN.data = frontOfDeck.data;
            
            frontOfDeck = frontOfDeck.next;
            numberOfEntries--;
            result = true;
        }
        
        return result;
        
    }
    
    public boolean search(Card aCard) {
        boolean found = false;
        Node currentNode = frontOfDeck;
        
        while (!found && (currentNode != null)) {
            if(aCard.equals(currentNode.data))
                found = true;
            else
                currentNode = currentNode.next;
        } //end while
        
        return found;
        
    }
    
    
    public void clear() {
        frontOfDeck = null;
    } //end clear
    
    public int getNum() {
        return numberOfEntries;
    }
    
    public Card[] toArray() {
        @SuppressWarnings("unchecked")
        Card[] result = (Card[])new Object [numberOfEntries];
        
        int index = 0;
        Node currentNode = frontOfDeck;
        while ((index < numberOfEntries) && (currentNode != null)) {
            result[index] = currentNode.data;
            index++;
            currentNode = currentNode.next;
        }
        
        return result;
        
    }

private Node getReferenceTo(Card aCard) {
        boolean found = false;
        Node currentNode = frontOfDeck;
        while (!found && (currentNode != null)) {
            if (aCard.equals((currentNode.data)))
                found = true;
            else
                currentNode = currentNode.next;
        }
        
    return currentNode;
        
    }
}

Code from my Node Class:

package card;

public abstract class NodeClass<Card> implements DeckInterface<Card> {
    
     private class Node {
      private Card data; // Entry in bag
      private Node next; // Link to next node

        private Node(Card dataValue) {
            this(dataValue, null);  
        } // end constructor
        
        private Node(Card dataValue, Node nextValue) {
            data = dataValue;
            next = nextValue;   
        } // end constructor
                
        private void setData(Card dataValue){
            data = dataValue;
        }
        
        private Card getData() {
            return data;
        } 
        
        private void setNextNode(Node nodeValue) {
            next = nodeValue;
        }
        
    } // end Node
    
}

Correct Node Class:

package card;

/**
 *
 * @param <Card>
 
 */
class NodeClass<Card> {
    
      Card data; // Entry in bag
      NodeClass<Card> next; // Link to next node

        NodeClass(Card dataValue) {
            this(dataValue, null);  
        } // end constructor
        
        NodeClass(Card dataValue, NodeClass<Card> nextValue) {
            data = dataValue;
            next = nextValue;   
        } // end constructor
                
                void setData(Card dataValue){
                    data = dataValue;
                }
                
                Card getData() {
                    return data;
                } 
                
                NodeClass<Card> getNextNode() {
                    return next;
                }
                
                void setNextNode(NodeClass nodeValue) {
                    next = nodeValue;
                }
                
                
                
            } // end Node

Correct Deck Class:

package card;


import java.io.*;
import java.util.*;

public class Deck<Card> implements DeckInterface<Card> {
    
    
    private NodeClass frontOfDeck; // Cannot be final due to doubling
    private int numberOfEntries; 
         
    /**
     * Creates a deck of cards whose default capacity is 52. 
    */   
    public Deck() {
        frontOfDeck = null;
        numberOfEntries = 0;
    } // end default constructor
    
    
    //sets private data member
    public void setNumOfEntries(int numberOfEntries) {
        this.numberOfEntries = numberOfEntries;
    }
    //returns number of entries
    public int getNumOfEntries() {
        return numberOfEntries; 
    }
    
       
    /** Adds a new card to this deck.
     * @return True.
     * @param aCard
    */
    public boolean add(Card aCard) {
        //Beginning of chain
        NodeClass newNode = new NodeClass(aCard);
        newNode.setNextNode(frontOfDeck);
        //Make new node reference rest of chain
        //frontOfDeck is nulll if chain is empty
        frontOfDeck = newNode; //New node is at beginning of chain
        numberOfEntries++;
    
    return true;
        
    } //end add
    
    
    /**
     * Sees if deck is empty
     * @return true if empty, or false if not
     */
    public boolean isEmpty() {
        return numberOfEntries == 0;
    } //end isEmpty
    
    
    public int getFrequencyOf(Card aCard) {
        int frequency = 0;
        int loopCounter = 0;
        NodeClass currentNode = frontOfDeck;
        
        while ((loopCounter < numberOfEntries) && (currentNode != null)) {
            if (aCard.toString().trim().equals(currentNode.data.toString().trim())){
                frequency++;}
            loopCounter++;
            currentNode = currentNode.next;
        }
        
        return frequency;
        
    }
    
    public int getCardDeck(Card aCard) {
        int numCards = 0;
        int loopCounter = 0;
        NodeClass currentNode = frontOfDeck;
        
        while ((loopCounter < numberOfEntries) && (currentNode != null)) {
            //if (aCard.equals(currentNode.data))
            numCards++;
            System.out.println(currentNode.getData());
            loopCounter++;
            currentNode = currentNode.next;
        }

        return numCards;
        
    }
    
    public Card remove() {
        Card result = null;
        if(frontOfDeck != null) {
            result = (Card) frontOfDeck.data;
            frontOfDeck = frontOfDeck.next;
            numberOfEntries--;
        }
        
        return result;
        
    }
    
    public boolean removeSpecific(Card aCard) {
        boolean result = false;
        NodeClass nodeN = getReferenceTo(aCard);
        if (nodeN != null) {
            nodeN.data = frontOfDeck.data;
            
            frontOfDeck = frontOfDeck.next;
            numberOfEntries--;
            result = true;
        }
        
        return result;
        
    }
    
    public boolean search(Card aCard) {
        boolean found = false;
        NodeClass currentNode = frontOfDeck;
        
        while (!found && (currentNode != null)) {
            if(aCard.equals(currentNode.data))
                found = true;
            else
                currentNode = currentNode.next;
        } //end while
        
        return found;
        
    }
    
    
    public void clear() {
        frontOfDeck = null;
    } //end clear
    
    
    public int getNum() {
        return numberOfEntries;
    }
    
    public Card[] toArray() {
        @SuppressWarnings("unchecked")
        Card[] result = (Card[])new Object [numberOfEntries];
        
        int index = 0;
        NodeClass currentNode = frontOfDeck;
        while ((index < numberOfEntries) && (currentNode != null)) {
            result[index] = (Card) currentNode.data;
            index++;
            currentNode = currentNode.next;
        }
        
        return result;
        
    }

    
    private NodeClass getReferenceTo(Card aCard) {
        boolean found = false;
        NodeClass currentNode = frontOfDeck;
        while (!found && (currentNode != null)) {
            if (aCard.equals((currentNode.data)))
                found = true;
            else
                currentNode = currentNode.next;
        }
        
    return currentNode;
        
    }
}

Card Class for reference:

import java.util.Objects;

public class Card {
    private String cardSuit; //declares variables as private
    private int cardValue;
    
public Card() {    
}

/**
* @param suit
* @param value
*/
public Card(String suit, int value) { 
    this.cardSuit = suit;
    this.cardValue = value;
}

//returns cardSuit
public String getCardSuit() {
    return cardSuit;
}

//sets private data member
public void setCardSuit(String cardSuit) {
    this.cardSuit = cardSuit;
}

//returns cardValue
public int getCardValue() {
    return cardValue;
}

//sets private data member
public void setCardValue(int cardValue) {
    this.cardValue = cardValue;
}

//returns class data as string
@Override
public String toString() {
    return "Card " + cardValue + " of " + cardSuit.toUpperCase();
}


@Override
public boolean equals(Object obj) {
    //check is obj are referenced
    if (this == obj) return true;
    if (obj == null || getClass() != obj.getClass()) return false;
        Card card = (Card) obj;
            return cardValue == card.cardValue && cardSuit.equals(card.cardSuit);
}

//returns intgers 
@Override
public int hashCode() {
    return Objects.hash(cardSuit, cardValue);
}

}
    

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