-2

I am doing an exercise, which needs the DogSchool to implement the PetSchool. I intend to make a array list of the animals that registered in the pet school, and the dog school need to distinguish dogs from other animals. The characterisation of dogs is their shouting "Wau! Wau!". I have corrected. But still it can't distinguish dogs from cats.

Tier = Animal

This is the code of interface.

      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);

    }
}

Well, the problem happens in the 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());
        }

  }

After compiling, it shows:

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

which is better, but still it can't tell distinguish dogs from cats.

Anthony
  • 51
  • 7

2 Answers2

2

Basically everything is wrong with your code. None of your implementations modify your tiere. Additionally testing the sound of the animal might not be the safest way of checking.

Assuming you have a Dog class

class Dog implements Tier {
}

This may work though your description is missing some details.

public class DogSchool implements PetSchool {
  // make your internal list private to prevent unqualidfied modification
  private ArrayList<Tier> dogs = new ArrayList<>();

  public boolean add(Tier tier) {
    // check the type of the animal and add it to your internal list
    if (tier instanceof Dog) {
      return this.dogs.add(tier);
    }
    return false;
  }

  public boolean addAll(ArrayList<Tier> tiere) {
    // only add the dogs if every animal in the list is a dog
    for (Tier t: tiere) {
      if (!(t instanceof Dog))
        return false;
    }
    return this.dogs.addAll(tiere);
  }

  public boolen remove(Tier tier) {
    return this.dogs.remove(tier);
  }

  public ArrayList<Tier> getTiere() {
    // return a copy so no one can modify your internal list
    return new ArrayList<>(this.dogs);
  }
}
Longhup
  • 101
  • 6
  • I still have a question. Why does boolean remove(Tier tier) method not have a check? But it still works. Thank you – Anthony Dec 15 '15 at 12:58
  • @Anthony Have a look at the [JavaDoc](http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#remove-java.lang.Object-) for the `remove` method – Longhup Dec 15 '15 at 13:18
1

You have many errors. The main thing to note is that you should initialize the tiere List and use it in all your methods instead of creating new ArrayList in each method.

 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))

}
Eran
  • 387,369
  • 54
  • 702
  • 768