public class Animal { double weight; int height; Animal(int height, double weigh
ID: 3820753 • Letter: P
Question
public class Animal {
double weight;
int height;
Animal(int height, double weight){
this.height = height;
this.weight = weight;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public String toString(){
return "Weight is "+this.getWeight()+" Height is "+this.getHeight();
}
}
public class Bird extends Animal{
boolean canFly;
Bird(int height, double weight, boolean canFly){
super(height,weight);
this.canFly = canFly;
}
public boolean isCanFly() {
return canFly;
}
public void setCanFly(boolean canFly) {
this.canFly = canFly;
}
public String toString(){
return super.toString()+" canFly: "+this.isCanFly();
}
}
public class Fish extends Animal{
boolean needAir;
public boolean isNeedAir() {
return needAir;
}
public void setNeedAir(boolean needAir) {
this.needAir = needAir;
}
Fish(int height, double weight, boolean needsAir){
super(height,weight);
this.needAir = needsAir;
}
public String toString(){
return super.toString()+" Need Air: "+this.isNeedAir();
}
}
Thanks for the help!
Explanation / Answer
The code is correct. There is no issue in the code. The code tells that there are two childs Fish and Bird which extends Animal class . All three are classes only. Now the question comes for penguins and whales . Actually this will not be specified here.
Because penguins and whales are more of a specialized kind of bird and fish respectively . So these must not be a part of the class but will be OBJECTS of these classes. They will be instantiated in the main function as follows
Bird penguins = new Bird(4,11.3,false);
just see here and compare this with constructor in Bird class. 4 is height , 11.3 is weight and false is the value of canFly varobale. This is as per the specifications.
Fish whales = new Fish(4,11.3,true);
Now compare this with Fish constructor in Fish class. 4 is height here,11.3 is weight and we are setting needAir as true here for whales as per the specification.
So it is clear that whales and penguins are objects not the part of specification classes. For your more understanding , here is the definition of objects and class
Class is the blueprint of data i.e. we specify the properties and behaviour of a group of similar kind of entities. It does not define the data but just specify what its objects will have can perform.
Objects are instance of the classes and have all the properties and behavious that are specified in classes. We can say that they are the definition of classes.
So here Bird and Fish are classes and penguins and whales are objects of these classes.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.