Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

java help: i need to write a code. This was the problem given to me, ignore the

ID: 3819725 • Letter: J

Question

java help:
i need to write a code. This was the problem given to me, ignore the first sentence.

"Next, I'd like to illustrate how inheritance works. Suppose we have an Animal class that has instance variables for height and weight, an argument constructor, getters and setters, and a toString method. If we want to create Fish and Bird subclasses that contain the following information: Bird instance variable: canFly false for penguins) Fish instance variable needsAir true for whales) both argument constructor, getter, setter, toString

Explanation / Answer


public class Animal {
   private int height;
   private int weight;
  
   public Animal(int h ,int w){
       this.setHeight(h);
       this.setWeight(w);
   }

   public int getHeight() {
       return height;
   }

   public void setHeight(int height) {
       this.height = height;
   }

   public int getWeight() {
       return weight;
   }

   public void setWeight(int weight) {
       this.weight = weight;
   }
  
}

class Bird extends Animal{
   private String type;
   public Bird(int h ,int w, String t){
       super(h,w);
       this.type =t;
   }
   boolean canFly(){
       if(type.equalsIgnoreCase("Penguin"))
           return false;
       //We can add more if
       return true;
   }
}
class Fish extends Animal {
  
   private String type;
   public Fish(int h ,int w, String t){
       super(h,w);
       this.type =t;
   }
   boolean needAir(){
       if(type.equalsIgnoreCase("Whale"))
           return true;
       //We can add more if
       return false;
   }
}