2016-12-02 5 views
0

私は教科書からこのコードを実行します。どのように私はこの問題を解決するか、エラーが何を意味しこれは教科書のサンプルコードです。それはコンパイルされません

/** 
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 

:ここ

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) 

コードにコンパイルされていないされています。私は次のエラーを取得しますか?

コードは、ArrayBagクラスの最初の草稿で と定義されているadd、toArray、およびisFullのメソッドのテストです。

+3

「BagInterface」はどこから来たのですか? 'ArrayBag'を含んでいないようにも見えます –

+0

あなたはクラスがない必要があります。それらを投稿に含めてください。 – Ajay

+0

[本当に](https://xkcd.com/1742/)..冗談はさておき、クラスの部分だけでなくファイル全体の内容も含めてください。 'BagInterface'と' ArrayBag'について知る必要があります。可能であれば、コードも提供してください。また、['型に解決できない]に関するいくつかの答えがあります(http://stackoverflow.com/questions/5125107/java-class-cannot-be-resolved-to-a-type)。 –

答えて

0

これに基づいて、BagInterfaceクラスとArrayBagクラスを作成するか、インポートする必要があるようです。

0

これは、クラスBagInterfaceを作成しなかったか、インポートしなかったことを意味します。 ArrayBagについても同様です。あなたはそれらのクラスをインポートする必要があります

1

私はインターフェイスBagInterfaceまたはクラスArrayBagオンラインを見つけることができないので、あなたの本のどこかにあると仮定します。私はインターフェイスとクラスがどのように見えるかを書いて自由にしました(このインターフェイスとクラスを使用する他の例ではうまくいかないかもしれません)。次のものをjavaファイルの末尾に貼り付けることができます(他のクラスの外に)。

最後の注意:Object配列をジェネリック型にキャストすることは安全ではありません。コンパイル時に警告が表示されます。

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 
    } 
} 
+0

ありがとうございました! –

関連する問題