Reputation: 55
When I run this code from my textbook. I get the following error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
BagInterface cannot be resolved to a type
ArrayBag cannot be resolved to a type
ArrayBag cannot be resolved to a type
at ArrayBagDemo1.main(ArrayBagDemo1.java:12)
Here is the code that is not compiling:
/**
A test of the methods add, toArray, and isFull, as defined
in the first draft of the class ArrayBag.
@author Frank M. Carrano
*/
public class ArrayBagDemo1
{
public static void main(String[] args)
{
// a bag that is not full
BagInterface<String> aBag = new ArrayBag<String>();
// tests on an empty bag
testIsFull(aBag, false);
// adding strings
String[] contentsOfBag1 = {"A", "A", "B", "A", "C", "A"};
testAdd(aBag, contentsOfBag1);
testIsFull(aBag, false);
// a bag that will be full
aBag = new ArrayBag<String>(7);
System.out.println("\nA new empty bag:");
// tests on an empty bag
testIsFull(aBag, false);
// adding strings
String[] contentsOfBag2 = {"A", "B", "A", "C", "B", "C", "D"};
testAdd(aBag, contentsOfBag2);
testIsFull(aBag, true);
} // end main
// Tests the method add.
private static void testAdd(BagInterface<String> aBag, String[] content)
{
System.out.print("Adding to the bag: ");
for (int index = 0; index < content.length; index++)
{
aBag.add(content[index]);
System.out.print(content[index] + " ");
} // end for
System.out.println();
displayBag(aBag);
} // end testAdd
// Tests the method isFull.
// correctResult indicates what isFull should return.
private static void testIsFull(BagInterface<String> aBag,boolean correctResult)
{
System.out.print("\nTesting the method isFull with ");
if (correctResult)
System.out.println("a full bag:");
else
System.out.println("a bag that is not full:");
System.out.print("isFull finds the bag ");
if (correctResult && aBag.isFull())
System.out.println("full: OK.");
else if (correctResult)
System.out.println("not full, but it is full: ERROR.");
else if (!correctResult && aBag.isFull())
System.out.println("full, but it is not full: ERROR.");
else
System.out.println("not full: OK.");
} // end testIsFull
// Tests the method toArray while displaying the bag.
private static void displayBag(BagInterface<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] + " ");
} // end for
System.out.println();
} // end displayBag
} // end ArrayBagDemo1
How do I fix this and what does the error mean?
The code is a test of the methods add, toArray, and isFull, as defined in the first draft of the class ArrayBag.
Upvotes: 0
Views: 444
Reputation: 153
I cannot find the interface BagInterface or the class ArrayBag online, so I assume they are somewhere else in your book. I took the liberty of writing how that interface and class would look (note that this may not work for other examples using this interface and class). You can paste the following into the bottom of your java file (outside of any other classes).
One final note: it is unsafe to cast an Object array to a generic type. You will see a warning for it when you compile.
interface BagInterface<T>{
public boolean isFull();
public T[] toArray();
public void add(T object);
}
class ArrayBag<T> implements BagInterface<T>{
T[] bag;
public ArrayBag()
{
bag = (T[]) new Object[10]; //some arbitrary default size
}
public ArrayBag(int size)
{
bag = (T[]) new Object[size];
}
public boolean isFull(){
//Check that every slot is occupied
for(int i = 0; i < bag.length; i++)
{
if(bag[i] == null)
return false;
}
return true;
}
public T[] toArray(){
return bag;
}
public void add(T object){
//Find first empty slot to add item
for(int i = 0; i < bag.length; i++)
{
if(bag[i] == null)
{
bag[i] = object;
return;
}
}
//otherwise bag is full
}
}
Upvotes: 1
Reputation: 73
This means either you did not create the class BagInterface
or you did not import it. same is true for ArrayBag
. You should import those classes
Upvotes: 0
Reputation: 1
Based on this, it looks like you either need to create the BagInterface
and ArrayBag
classes, or you need to import them.
Upvotes: 0