Java 如何使位于自己文件中的私有节点类对my deck类(LINKEDLIST)可见?
所以我有这个项目,基本上我的教授希望node类在一个单独的文件中,而不是在我实际的deck类中。因此,我的deck类方法无法再使用节点数据类型,也无法工作。我的教授只给了我使用get、set方法的技巧,以便允许我的deck类在节点类位于不同文件中时访问它。我不知道如何做到这一点,所以任何帮助我找出如何设置get、set方法,以便我的Deck类可以阅读它们的方法都将不胜感激 我的甲板类代码:Java 如何使位于自己文件中的私有节点类对my deck类(LINKEDLIST)可见?,java,nodes,Java,Nodes,所以我有这个项目,基本上我的教授希望node类在一个单独的文件中,而不是在我实际的deck类中。因此,我的deck类方法无法再使用节点数据类型,也无法工作。我的教授只给了我使用get、set方法的技巧,以便允许我的deck类在节点类位于不同文件中时访问它。我不知道如何做到这一点,所以任何帮助我找出如何设置get、set方法,以便我的Deck类可以阅读它们的方法都将不胜感激 我的甲板类代码: package card; import java.io.*; import java.util.*;
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;
}
}
包装卡;
导入java.io.*;
导入java.util.*;
公共类Deck实现Deck接口{
私有节点frontOfDeck;//由于加倍,无法为最终节点
私人int numberOfEntries;
/**
*创建一组默认容量为52的卡。
*/
公共甲板(){
frontOfDeck=null;
numberOfEntries=0;
}//结束默认构造函数
//设置私有数据成员
公共无效setNumOfEntries(int numberOfEntries){
this.numberOfEntries=numberOfEntries;
}
//返回条目数
public int getNumOfEntries(){
返回条目的数目;
}
/**将一张新卡添加到此牌组。
*@返回True。
*@param aCard
*/
公共布尔添加(卡aCard){
//链的起点
节点newNode=新节点(aCard);
newNode.setNextNode(frontOfDeck);
//使新节点引用链的其余部分
//若链为空,则frontOfDeck为空
frontOfDeck=newNode;//新节点位于链的开头
numberOfEntries++;
返回true;
}//结束添加
/**
*看看甲板是否是空的
*@如果为空则返回true,否则返回false
*/
公共布尔值为空(){
返回numberOfEntries==0;
}//结束是空的
公共int getFrequencyOf(卡aCard){
整数频率=0;
int循环计数器=0;
节点currentNode=frontOfDeck;
while((loopCounter
我的节点类中的代码:
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
}
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
包装卡;
公共抽象类NodeClass实现了DeckInterface{
私有类节点{
私人卡数据;//包中的条目
私有节点下一步;//链接到下一个节点
专用节点(卡数据值){
此参数为(数据值,null);
}//结束构造函数
专用节点(卡数据值、节点下一个值){
数据=数据值;
next=nextValue;
}//结束构造函数
私有无效设置数据(卡数据值){
数据=数据值;
}
私人卡getData(){
返回数据;
}
私有void setNextNode(节点nodeValue){
next=节点值;
}
}//结束节点
}
正确的节点类别:
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
}
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
为什么定义
NodeClass
只是为了包装节点
类?另外,节点
类被标记为私有
,因此没有代码能够使用它。在我看来