Hi,
I'm trying to make "Node" class outside of LinkedBag using package from an inner class of LinkedBag1.java(I attached the code as below).
But It did not work.
I could compile Node.java, but not LinkedBagDemo1.java and LinkedBag1.java.
The error as below came up.
How could I fix the problem to make "Node" class outside of LinkedBag using package.
Although I googled some stuff, It did not help me a lot.
I hope you guys advise me some good ways. Thanks!
What I have tried:
------------------ Node.java -------------------
package BagPackage;
class Node<T>
{
private T data;
private Node<T> next;
Node(T dataPortion)
{
this(dataPortion, null);
}
Node(T dataPortion, Node<T> nextNode)
{
data = dataPortion;
next = nextNode;
}
T getData()
{
return data;
}
void setData(T newData)
{
data = newData;
}
Node<T> getNextNode()
{
return next;
}
void setNextNode(Node<T> nextNode)
{
next = nextNode;
}
}
------------------ LinkedBagDemo1.java -------------------
package BagPackage;
import java.util.*;
public class LinkedBagDemo1
{
public static void main(String[] args)
{
System.out.println("Creating an empty bag.");
LinkedBag1<String> aBag = new LinkedBag1<>();
testIsEmpty(aBag, true);
displayBag(aBag);
String[] contentsOfBag = {"A", "D", "B", "A", "C", "A", "D"};
testAdd(aBag, contentsOfBag);
testIsEmpty(aBag, false);
System.out.println("Minimum value in contentsOfBag is " + aBag.getMin());
}
private static void testIsEmpty(LinkedBag1<String> bag, boolean empty)
{
System.out.print("\nTesting isEmpty with ");
if (empty)
System.out.println("an empty bag:");
else
System.out.println("a bag that is not empty:");
System.out.print("isEmpty finds the bag ");
if (empty && bag.isEmpty())
System.out.println("empty: OK.");
else if (empty)
System.out.println("not empty, but it is: ERROR.");
else if (!empty && bag.isEmpty())
System.out.println("empty, but it is not empty: ERROR.");
else
System.out.println("not empty: OK.");
}
private static void testAdd(LinkedBag1<String> aBag, String[] content)
{
System.out.print("Adding the following " + content.length +
" strings to the bag: ");
for (int index = 0; index < content.length; index++)
{
if (aBag.add(content[index]))
System.out.print(content[index] + " ");
else
System.out.print("\nUnable to add " + content[index] +
" to the bag.");
}
System.out.println();
displayBag(aBag);
}
private static void displayBag(LinkedBag1<String> aBag)
{
System.out.println("The bag contains the following string(s):");
Object[] bagArray = aBag.toArray();
for (int index = 0; index < bagArray.length; index++)
{
System.out.print(bagArray[index] + " ");
}
System.out.println();
}
}
------------------ LinkedBag1.java -------------------
package BagPackage;
public final class LinkedBag1<T extends Comparable> implements BagInterface<T>
{
private Node<T> firstNode;
private int numberOfEntries;
public LinkedBag1()
{
firstNode = null;
numberOfEntries = 0;
}
public boolean add(T newEntry)
{
Node<T> newNode = new Node<T>(newEntry);
newNode.next = firstNode;
firstNode = newNode;
numberOfEntries++;
return true;
}
public T[] toArray()
{
@SuppressWarnings("unchecked")
T[] result = (T[])new Comparable[numberOfEntries];
int index = 0;
Node currentNode = firstNode;
while ((index < numberOfEntries) && (currentNode != null))
{
result[index] = currentNode.data;
index++;
currentNode = currentNode.next;
}
return result;
}
public boolean isEmpty()
{
return numberOfEntries == 0;
}
public int getCurrentSize()
{
return numberOfEntries;
}
public T remove()
{
T result = null;
if (firstNode != null)
{
result = firstNode.data;
firstNode = firstNode.next;
numberOfEntries--;
}
return result;
}
private Node getReferenceTo(T anEntry)
{
boolean found = false;
Node currentNode = firstNode;
while (!found && (currentNode != null))
{
if (anEntry.equals(currentNode.data))
found = true;
else
currentNode = currentNode.next;
}
return currentNode;
}
public boolean remove(T anEntry)
{
boolean result = false;
Node nodeN = getReferenceTo(anEntry);
if (nodeN != null)
{
nodeN.data = firstNode.data;
firstNode = firstNode.next;
numberOfEntries--;
result = true;
}
return result;
}
public void clear()
{
while (!isEmpty())
remove();
}
public int getFrequencyOf(T anEntry)
{
int frequency = 0;
int loopCounter = 0;
Node currentNode = firstNode;
while ((loopCounter < numberOfEntries) && (currentNode != null))
{
if (anEntry.equals(currentNode.data))
{
frequency++;
}
loopCounter++;
currentNode = currentNode.next;
}
return frequency;
}
public boolean contains(T anEntry)
{
boolean found = false;
Node currentNode = firstNode;
while (!found && (currentNode != null))
{
if (anEntry.equals(currentNode.data))
found = true;
else
currentNode = currentNode.next;
}
return found;
}
@SuppressWarnings("unchecked")
public T getMin()
{
Node currentNode = firstNode;
T currentMin = firstNode.data;
while (currentNode != null)
{
currentNode = currentNode.next;
if (currentNode != null
&& currentNode.data.compareTo(currentMin) == -1)
currentMin = currentNode.data;
}
return currentMin;
}
@SuppressWarnings("unchecked")
public T getMax()
{
Node currentNode = firstNode;
T currentMax = firstNode.data;
while (currentNode != null)
{
currentNode = currentNode.next;
if (currentNode != null
&& currentNode.data.compareTo(currentMax) == 1)
currentMax = currentNode.data;
}
return currentMax;
}
}
------------------ BagInterface.java -------------------
public interface BagInterface<T>
{
public int getCurrentSize();
public boolean isEmpty();
public boolean add(T newEntry);
public T remove();
public boolean remove(T anEntry);
public void clear();
public int getFrequencyOf(T anEntry);
public boolean contains(T anEntry);
public T[] toArray();
}
<Error on LinkedBagDemo1.java>
LinkedBagDemo1.java:40: error: cannot find symbol
private static void testIsEmpty(LinkedBag1<string> bag, boolean empty) //change BagInterface to LinkedBag1
^
symbol: class LinkedBag1
location: class LinkedBagDemo1
LinkedBagDemo1.java:60: error: cannot find symbol
private static void testAdd(LinkedBag1<string> aBag, String[] content) //change BagInterface to LinkedBag1
^
symbol: class LinkedBag1
location: class LinkedBagDemo1
LinkedBagDemo1.java:78: error: cannot find symbol
private static void displayBag(LinkedBag1<string> aBag) //change BagInterface to LinkedBag1
^
symbol: class LinkedBag1
location: class LinkedBagDemo1
LinkedBagDemo1.java:25: error: cannot find symbol
LinkedBag1<string> aBag = new LinkedBag1<>(); //change BagInterface to LinkedBag1 Why?
^
symbol: class LinkedBag1
location: class LinkedBagDemo1
LinkedBagDemo1.java:25: error: cannot find symbol
LinkedBag1<string> aBag = new LinkedBag1<>(); //change BagInterface to LinkedBag1 Why?
^
symbol: class LinkedBag1
location: class LinkedBagDemo1
5 errors
<Error on LinkedBag1.java>
LinkedBag1.java:9: error: cannot find symbol
public final class LinkedBag1<T extends Comparable> implements BagInterface<t>
^
symbol: class BagInterface
LinkedBag1.java:11: error: cannot find symbol
private Node<t> firstNode; // Reference to first node
^
symbol: class Node
location: class LinkedBag1<t>
where T is a type-variable:
T extends Comparable declared in class LinkedBag1
LinkedBag1.java:91: error: cannot find symbol
private Node getReferenceTo(T anEntry)
^
symbol: class Node
location: class LinkedBag1<t>
where T is a type-variable:
T extends Comparable declared in class LinkedBag1
LinkedBag1.java:26: error: cannot find symbol
Node<t> newNode = new Node<t>(newEntry);
^
symbol: class Node
location: class LinkedBag1<t>
where T is a type-variable:
T extends Comparable declared in class LinkedBag1
LinkedBag1.java:26: error: cannot find symbol
Node<t> newNode = new Node<t>(newEntry);
^
symbol: class Node
location: class LinkedBag1<t>
where T is a type-variable:
T extends Comparable declared in class LinkedBag1
LinkedBag1.java:44: error: cannot find symbol
Node currentNode = firstNode;
^
symbol: class Node
location: class LinkedBag1<t>
where T is a type-variable:
T extends Comparable declared in class LinkedBag1
LinkedBag1.java:94: error: cannot find symbol
Node currentNode = firstNode;
^
symbol: class Node
location: class LinkedBag1<t>
where T is a type-variable:
T extends Comparable declared in class LinkedBag1
LinkedBag1.java:114: error: cannot find symbol
Node nodeN = getReferenceTo(anEntry);
^
symbol: class Node
location: class LinkedBag1<t>
where T is a type-variable:
T extends Comparable declared in class LinkedBag1
LinkedBag1.java:142: error: cannot find symbol
Node currentNode = firstNode;
^
symbol: class Node
location: class LinkedBag1<t>
where T is a type-variable:
T extends Comparable declared in class LinkedBag1
LinkedBag1.java:165: error: cannot find symbol
Node currentNode = firstNode;
^
symbol: class Node
location: class LinkedBag1<t>
where T is a type-variable:
T extends Comparable declared in class LinkedBag1
LinkedBag1.java:187: error: cannot find symbol
Node currentNode = firstNode;
^
symbol: class Node
location: class LinkedBag1<t>
where T is a type-variable:
T extends Comparable declared in class LinkedBag1
LinkedBag1.java:205: error: cannot find symbol
Node currentNode = firstNode;
^
symbol: class Node
location: class LinkedBag1<t>
where T is a type-variable:
T extends Comparable declared in class LinkedBag1