-2

PetSchool を実装するために DogSchool が必要な演習を行っています。私はペット学校に登録された動物の配列リストを作成するつもりであり、犬学校は犬を他の動物と区別する必要があります。犬の特徴は「わーわー」という鳴き声です。修正しました。しかし、それでも犬と猫を区別することはできません。

ティア = 動物

これはインターフェースのコードです。

      import java.util.ArrayList;


        public interface PetSchool {

             boolean add(Tier tier);
             boolean addAll(ArrayList<Tier> tiere);
             boolean remove(Tier tier);
             ArrayList<Tier> getTiere();

        }

This is the code of Implementation.
Please tell me what's wrong with it.

import java.util.ArrayList;

public class DogSchool implements PetSchool {

     public  ArrayList<Tier> tiere= new ArrayList<Tier>();

      @Override
      public boolean add(Tier t){
          if(t.gibLaut().equalsIgnoreCase("Wau! Wau!")){
              return tiere.add(t);
              }
          else {
              return false;
          } }


      @ Override 
      public boolean addAll(ArrayList<Tier> tiere){
             return this.tiere.addAll(tiere);

      }

      @Override
      public boolean remove(Tier t){
          if(!t.gibLaut().equalsIgnoreCase("Wau! Wau!")){
              return tiere.remove(t);
          }
          else{
              return false;
          }

      }

    @Override
    public ArrayList<Tier> getTiere() {
        return new ArrayList<Tier>(this.tiere);

    }
}

問題は demoTest で発生します。

import java.util.ArrayList;

public class TierDemo {
 public static void main(String[] args) {
 System.out.println("Test Teil 2:");

        DogSchool schule = new DogSchool();
        schule.add(new Hund());
        schule.add(new Katze());
        schule.add(new Hund());
        schule.add(new Katze());
        schule.addAll(tiere);
        for (Tier t : schule.getTiere()) {
            System.out.println(t.gibLaut());
        }

  }

コンパイル後、次のように表示されます。

Test Teil 2:
Wau! Wau!
Wau! Wau!
Miau!
Wau! Wau!

どちらの方が良いですが、それでも犬と猫を区別することはできません。

4

2 に答える 2

1

多くのエラーがあります。主な注意点は、各メソッドで新しい ArrayList を作成するのではなく、tiere List を初期化し、すべてのメソッドで使用する必要があることです。

 public ArrayList<Tier> tiere; // you forgot to initialize this ArrayList

 @Override
  public boolean add(Tier t){
      ArrayList<Tier> neu= new ArrayList<Tier>(); 
      if(t.gibLaut().equalsIgnoreCase("Wau! Wau!")){
          return neu.add(t); // this list is local to the method, you should be adding to tiere
          }
      else {
          return false;
      } }


  @ Override 
  public boolean addAll(ArrayList<Tier> tiere){
      ArrayList<Tier> neu= new ArrayList<Tier>(); // remove this list
     return tiere.addAll(neu); // should be this.tiere.addAll(tiere);

  }

  @Override
  public boolean remove(Tier t){
      ArrayList<Tier> neu= new ArrayList<Tier>(tiere.size()); // remove this
      if(!t.gibLaut().equalsIgnoreCase("Wau! Wau!")){
          return neu.remove(t); // should remove from tiere
      }
      else{
          return false;
      }

  }

@Override
public ArrayList<Tier> getTiere() {
    return new ArrayList<Tier>(); // should either return tiere or a copy of it (i.e. new ArrayList<Tier>(tiere))

}
于 2015-12-15T10:59:24.587 に答える