Click here to Skip to main content
15,887,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

Java
------------------ Node.java -------------------

package BagPackage;

class Node<T>
{
   private T data;      // Entry in bag
   private Node<T> next;   // Link to next node
   
   Node(T dataPortion)  // The constructor's name is Node, not Node<T> 
   {
      this(dataPortion, null);
   } // end constructor
   
   Node(T dataPortion, Node<T> nextNode)
   {
      data = dataPortion;
      next = nextNode;
   } // end constructor
   
   T getData()
   {
      return data;
   } // end getData
   
   void setData(T newData)
   {
      data = newData;
   } // end setData
   
   Node<T> getNextNode()
   {
      return next;
   } // end getNextNode
   
   void setNextNode(Node<T> nextNode)
   {
      next = nextNode;
   } // end setNextNode
   
} // end Node


------------------ 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<>(); //change BagInterface to LinkedBag1 Why?
      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());

	} // end main
   
   // Tests the method isEmpty.
   // Precondition: If the bag is empty, the parameter empty should be true;
   // otherwise, it should be false.
	private static void testIsEmpty(LinkedBag1<String> bag, boolean empty) //change BagInterface to LinkedBag1
   {
      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.");      
	} // end testIsEmpty

   // Tests the method add.
	private static void testAdd(LinkedBag1<String> aBag, String[] content) //change BagInterface to LinkedBag1
	{
		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.");
		} // end for
      System.out.println();
      
		displayBag(aBag);
	} // end testAdd
   
   // Tests the method toArray while displaying the bag.
	private static void displayBag(LinkedBag1<String> aBag)     //change BagInterface to LinkedBag1
	{
		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] + " ");
		} // end for
		
		System.out.println();
	} // end displayBag
   
} // end LinkedBagDemo1


------------------ LinkedBag1.java -------------------

package BagPackage;

public final class LinkedBag1<T extends Comparable> implements BagInterface<T>
{
	private Node<T> firstNode;       // Reference to first node
	private int numberOfEntries;

	public LinkedBag1()
	{
		firstNode = null;
      numberOfEntries = 0;
	} // end default constructor

	/** Adds a new entry to this bag.
	    @param newEntry  The object to be added as a new entry.
	    @return  True. */
	public boolean add(T newEntry) // OutOfMemoryError possible
	{
      // Add to beginning of chain:
		Node<T> newNode = new Node<T>(newEntry);
		newNode.next = firstNode;  // Make new node reference rest of chain
                                       // (firstNode is null if chain is empty)
      firstNode = newNode;             // New node is at beginning of chain
		numberOfEntries++;
      
		return true;
	} // end add

	/** Retrieves all entries that are in this bag.
       @return  A newly allocated array of all the entries in this bag. */
	public T[] toArray()
	{
      // The cast is safe because the new array contains null entries.
      @SuppressWarnings("unchecked")
      T[] result = (T[])new Comparable[numberOfEntries]; // Unchecked cast
      
      int index = 0;
      Node currentNode = firstNode;
      while ((index < numberOfEntries) && (currentNode != null))
      {
         result[index] = currentNode.data;
         index++;
         currentNode = currentNode.next;
      } // end while
      
      return result;
      // Note: The body of this method could consist of one return statement,
      // if you call Arrays.copyOf
	} // end toArray
   
	/** Sees whether this bag is empty.
       @return  True if the bag is empty, or false if not. */
	public boolean isEmpty()
	{
		return numberOfEntries == 0;
	} // end isEmpty
   
	/** Gets the number of entries currently in this bag.
       @return  The integer number of entries currently in the bag. */
	public int getCurrentSize()
	{
		return numberOfEntries;
	} // end getCurrentSize
   

	/** Removes one unspecified entry from this bag, if possible.
       @return  Either the removed entry, if the removal
                was successful, or null. */
	public T remove()
	{
		T result = null;
      if (firstNode != null)
      {
         result = firstNode.data; 
         firstNode = firstNode.next; // Remove first node from chain
         numberOfEntries--;
      } // end if

		return result;
	} // end remove
   
 	// Locates a given entry within this bag.
	// Returns a reference to the node containing the entry, if located,
	// or null otherwise.
	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;
		} // end while
     
		return currentNode;
	} // end getReferenceTo

	/** Removes one occurrence of a given entry from this bag, if possible.
       @param anEntry  The entry to be removed.
       @return  True if the removal was successful, or false otherwise. */
   
   public boolean remove(T anEntry) 
	{
		boolean result = false;
      Node nodeN = getReferenceTo(anEntry);
      
      if (nodeN != null)
      {
         nodeN.data = firstNode.data; // Replace located entry with entry in first node
         firstNode = firstNode.next;  // Remove first node
         numberOfEntries--;
         result = true;
      } // end if
         
		return result;
	} // end remove

	
   /** Removes all entries from this bag. */
	public void clear() 
	{
		while (!isEmpty()) 
         remove();
	} // end clear
	
   /** Counts the number of times a given entry appears in this bag.
		 @param anEntry  The entry to be counted.
		 @return  The number of times anEntry appears in the bag. */
	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++;
         } // end if
         
         loopCounter++;
         currentNode = currentNode.next;
      } // end while

		return frequency;
	} // end getFrequencyOf

	
	/** Tests whether this bag contains a given entry.
		 @param anEntry  The entry to locate.
		 @return  True if the bag contains anEntry, or false otherwise. */
	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;
      } // end while
      
      return found;
   } // end contains
   
   /*
   • The method removeMin that removes and returns the smallest object in a bag
   • The method removeMax that removes and returns the largest object in a bag
   */
   
   // • The method getMin that returns the smallest object in a bag
   @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)  // if val 1 > val 2
				currentMin = currentNode.data;
		} // end while
		return currentMin;
	} // end getMin
   
   // • The method getMax that returns the largest object in a bag
   @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)  // if val 1 > val 2
				currentMax = currentNode.data;
		} // end while
		return currentMax;
	} // end getMax
   
} // end LinkedBag1


------------------ BagInterface.java -------------------

//package BagPackage;

public interface BagInterface<T>
{
	/** Gets the current number of entries in this bag.
		 @return  The integer number of entries currently in the bag. */
	public int getCurrentSize();
	
	/** Sees whether this bag is empty.
		 @return  True if the bag is empty, or false if not. */
	public boolean isEmpty();
	
	/** Adds a new entry to this bag.
	    @param newEntry  The object to be added as a new entry.
	    @return  True if the addition is successful, or false if not. */
	public boolean add(T newEntry);

	/** Removes one unspecified entry from this bag, if possible.
       @return  Either the removed entry, if the removal.
                was successful, or null. */
	public T remove();

   
	/** Removes one occurrence of a given entry from this bag.
       @param anEntry  The entry to be removed.
       @return  True if the removal was successful, or false if not. */
   public boolean remove(T anEntry);
	
	/** Removes all entries from this bag. */
	public void clear();

	
	/** Counts the number of times a given entry appears in this bag.
		 @param anEntry  The entry to be counted.
		 @return  The number of times anEntry appears in the bag. */
	public int getFrequencyOf(T anEntry);
	
	/** Tests whether this bag contains a given entry.
		 @param anEntry  The entry to locate.
		 @return  True if the bag contains anEntry, or false if not. */
	public boolean contains(T anEntry);
   
	/** Retrieves all entries that are in this bag.
		 @return  A newly allocated array of all the entries in the bag.
                Note: If the bag is empty, the returned array is empty. */
	public T[] toArray();
} // end BagInterface




<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
Posted
Updated 23-Sep-17 9:44am
Comments
Richard MacCutchan 23-Sep-17 10:21am    
You need to import LinkedBag1 into the other modules. In future please do not dump so much code, just show the part relevant to the problem.
Member 13425520 23-Sep-17 15:58pm    
Thanks for the advice. I will not dump so much code in the future.

After I imported
package bagPackage;
to LinkedBag1.java using the code
import bagPackage.*;


The Error came up.
LinkedBag1.java: error: Node is not public in bagPackage; cannot be accessed from outside package


Do you know whether there is a way to access to private class from outside package?
Richard MacCutchan 24-Sep-17 2:56am    
No, of course not, that is why you declare it private. Go to The Java™ Tutorials[^] and study the rules of the language.
Member 13425520 24-Sep-17 9:58am    
Thanks. I reviewed some stuffs about it on Internet. Then I understood.

1 solution

Quote:
Do you know whether there is a way to access to private class from outside package?
You have to modify the bagPackage source. You could, for instance, use a public interface to expose the private class, see java - Exporting non-public type through public API - Stack Overflow[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900