-2
嗨,我試圖添加動物(老虎,長頸鹿,企鵝)的每個子類的所有空間要求,所以我可以在動物園班級打印出來。用我目前的程序(下面)我得到每個大小的要求是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());
}
}
對不起,當你說「尺寸」,你是指數字或實際重量/動物的大小?你困惑這兩個嗎? – awm
[示例代碼](http://sscce.org/)應該最小化(閱讀鏈接)。這是太多無關的代碼。 – outis
只是因爲你添加了問號,並不是一個問題 –