在类中添加实例变量的方法?

时间:2011-11-25 23:37:56

标签: java

您好我正在尝试添加动物(虎,长颈鹿,企鹅)的每个子类的所有空间要求,以便我可以在动物园类中打印出来。使用我当前的程序(下面),我得到每个大小要求是0.加上一些额外的随机零。救命??拜托,谢谢!?

  public class Zoo {

    private Animal animal;  
    public int number_animals;//# animals allowed in zoo
    private List<Animal> animal_list;
    private String zoo_name;


    public Zoo(String name){
        this.zoo_name = name;
        animal_list = new ArrayList<Animal>();
    }

     public void addAnimal(Animal obj) {
            animal_list.add(obj);
     }
     public String toString(){
        String s = "In zoo " + zoo_name + " there are the following animals:\n";
        for (int i = 0; i < animal_list.size(); i++){
            s += animal_list.get(i).printDetails() + "\n";
        }
        return s;
     }

     public String countAnimals(){       
         return "There are " + animal_list.size() + " animals in " + zoo_name + "."; 
     }
     public String spaceRequirement(){
         int s = 0;
         int t = 0;
         int k = 0;
         for (int i = 0; i < animal_list.size(); i++){
             if (animal_list.get(i) instanceof Giraffe){
                while (! (animal_list.get(i) instanceof Giraffe))
                s += animal_list.get(i).getSpaceRequired();
            }
             System.out.println(s);
             if (animal_list.get(i) instanceof Penguin){
                    while (! (animal_list.get(i) instanceof Penguin))
                    t += animal_list.get(i).getSpaceRequired();
             }
             if (animal_list.get(i) instanceof Tiger){
                    while (! (animal_list.get(i) instanceof Tiger))
                    k += animal_list.get(i).getSpaceRequired();
             }

         }
         return "Total tree height needed: " + t + "\nTotal water volume needed: " + s + "\nTotal grass area needed: " + k;
     }
     public void printDetails(){
         System.out.println("In zoo " + zoo_name + " there are the following animals:\n" + animal);
     }


public class Animal {

    public String name;
    public int weight;
    private String food_type;
    private int space_area = 0;
    private Zoo zoo;
    private Animal animal;

    public Animal(String name, int weight){
        this.name = name;
        this.weight = weight;
    }    
    public String getName(){
        return this.name;
    }
    public int getWeight(){
        return this.weight;
    }

    public int getSpaceRequired() {
        return this.space_area;
    }

    public void printDailyFoodOrder() {
        return;
    }

    public void setZoo(Zoo zoo){
        this.zoo = zoo;
        this.zoo.addAnimal(this);
    }

    public String printDetails() {
        String a = getName() + " who weighs " + getWeight() + " kg,";
        return a;
        }


}

    public class Giraffe extends Animal {

    private String food_type = "leaves";
    private int leaves;
    private int space_area;
    private int number_giraffes = 0;
    public Giraffe[] giraffe_array;

        public Giraffe(String name, int weight, String food_type, int space_area, int leaves){
        super(name, weight);
        this.food_type = food_type;
        this.space_area = space_area;
        this.leaves = leaves;       
    }       
    public int getNumberGiraffes(){
        return this.number_giraffes;
    }

    public String printDetails(){
        return super.printDetails() + " eats " + this.food_type + ", and needs a tree with a mininum height " + this.space_area + " meters.";
    }

    public int getSpaceRequired() {
        return this.space_area;
    }
}

    public class Penguin extends Animal {

    private String food_type = "fish";
    private int food;
    private int space_area;

    public Penguin(String name, int weight, int food, int space_area){
        super(name, weight);
        this.food = food;
        this.space_area = space_area;
    }
    public int getSpaceRequired() {
        return this.space_area;
    }



    public void calcPoolSize() {

    }
    public String printDetails(){
        return super.printDetails() + " eats " + this.food_type + ", and needs a pool sized " + this.space_area + " cubic meters.";
    }

}


public class Test {

    public static void main(String[] args) {

        Zoo diego = new Zoo("San Diego");
        Animal giffy = new Animal("Giffy", 950);
        giffy = new Giraffe("Giffy", 950, "Leaves", 15, 100);       
        Animal gunther = new Animal("Gunther", 950);
        gunther = new Giraffe("Gunther", 950, "Leaves", 15, 100);
        Animal kevin = new Animal("Kevin", 225);
        kevin = new Tiger("Kevin", 225, 10, 10);
        Animal hobbes = new Animal("Hobbes", 200);
        hobbes = new Tiger("Hobbes", 200, 8, 20);
        Animal wobbles = new Animal("Wobbles", 25);
        wobbles = new Penguin("Wobbles", 25, 5, 125);
        Animal gretchin = new Animal("Gretchin", 15);
        gretchin = new Penguin("Gretchin", 15, 3, 125);
        diego.addAnimal(giffy);
        diego.addAnimal(gunther);
        diego.addAnimal(wobbles);
        diego.addAnimal(hobbes);
        diego.addAnimal(kevin);
        diego.addAnimal(gretchin);
        System.out.println(diego);
        System.out.println(diego.spaceRequirement());
    }

}

1 个答案:

答案 0 :(得分:2)

你不需要那些循环。

public String spaceRequirement(){
     int s = 0;
     int t = 0;
     int k = 0;
     for (int i = 0; i < animal_list.size(); i++){
         if (animal_list.get(i) instanceof Giraffe){
                s += animal_list.get(i).getSpaceRequired();
         }
         System.out.println(s);
         if (animal_list.get(i) instanceof Penguin){
                t += animal_list.get(i).getSpaceRequired();
         }
         if (animal_list.get(i) instanceof Tiger){
                k += animal_list.get(i).getSpaceRequired();
         }

     }
     return "Total tree height needed: " + t + "\nTotal water volume needed: " + s + "\nTotal grass area needed: " + k;
 }