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

Java Create four classes: 1. An Animal class that acts as a superclass for Dog a

ID: 3574161 • Letter: J

Question

Java

Create four classes:

1.      An Animal class that acts as a superclass for Dog and Bird

  2.      A Bird class that is a descendant of Animal

  3.      A Dog class that is a descendant of Animal

  4.      A “driver” class named driver that instantiates an Animal, a Dog, and a Bird object.

The Animal class has:

  ·        one instance variable, a private String variable named   name

  ·        a single constructor that takes one argument, a String, used to set the instance variable, then prints the message

  “The Animal (its name) has been created.”

  ·        “getter” and “setter” methods for the instance variable name

  ·        a no-argument method named sleep that prints the message

  “The Animal (its name) is asleep.

  ·        a no-argument method named makeNoise that prints the message

  “The Animal (its name) is making a noise.

  ·        a toString method that returns

  “The Animal (its name)”.

  ·        an equals method that compares another Object to this Animal object by comparing their names. Use our “improved” version of the equals method.

  Notice that there are NO references to either the Dog or the Bird class in the Animal class.

The Bird class:

  ·        extends the Animal class

  ·        has no instance variables

  ·        has a single constructor that takes one argument, a String used to set the instance variable of its parent, then prints the message

  “A Bird (its name) has been created.”

  ·        has a no-argument method named makeNoise that prints the message

  “The Bird (its name) is chirping.

  ·        has a toString method that returns

  “The Bird (its name)”.

  ·        has an equals method that compares another Object to this Bird object by comparing their names. Use our “improved” version of the equals method.

The Dog class:

  ·        extends the Animal class

  ·        has one instance variable, a private String named breed.

  ·        has a single constructor that takes two arguments, a String used to set the instance variable of its parent, and another    String used to set the instance variable of this object (breed). It also prints the message

  “A Dog (its name) of breed (its breed) has been created.”

  ·        has “getter” and “setter” methods for the instance variable   breed

  ·        has a no-argument method named makeNoise that prints the message

  “The Dog (its name) is barking.

  ·        has a toString method that returns

  “The Dog (its name) of breed (its breed)”.

  ·        has an equals method that compares another Object to this Dog object by comparing their names and breeds. Use our “improved” equals method.

The driver class contains a main method which:

  ·        creates an Animal object with a name “Oscar”.

  ·        creates a Dog object with a name “Inka” and a breed “Mutt

  ·        creates a Bird object with a name “Tweety

  ·        uses the toString method to print Oscar, Inka, and Tweety

  ·        causes Oscar, Inka, and Tweety each to make a noise

  ·        causes Oscar, Inka, and Tweety each to go to sleep

Explanation / Answer

Please find attached code and its output.

===Animal===


public class Animal {
   private String name;

   public Animal() {
       super();
   }

   public Animal(String name) {
       this.name = name;
       System.out.println("The Animal " + name + " has been created.");
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public void sleep() {
       System.out.println("The Animal " + name + " is asleep.");
   }

   public void makeNoise() {
       System.out.println("The Animal " + name + " is making a noise.");
   }

   @Override
   public String toString() {
       return "The Animal " + name + ".";
   }

   @Override
   public boolean equals(Object O) {
       if (O instanceof Animal && ((Animal) O).name == name)
           return true;

       return false;
   }

}

====Bird===


public class Bird extends Animal {
   public Bird(String name) {
       setName(name);
       System.out.println("A Bird " + name + " has been created.");

   }

   public void makeNoise() {
       System.out.println("The Bird " + this.getName() + " is chirping.");
   }

   @Override
   public String toString() {
       return "The Bird " + this.getName() + ".";
   }
   @Override
   public boolean equals(Object O) {
       if (O instanceof Bird && ((Bird) O).getName() == getName())
           return true;

       return false;
   }

}

=======Dog======


public class Dog extends Animal {
   private String breed;

   public Dog(String breed, String name) {
       setName(name);
       this.breed = breed;
       System.out.println("A Dog " + getName() + " of breed " + breed + " has been created.");

   }

   public String getBreed() {
       return breed;
   }

   public void setBreed(String breed) {
       this.breed = breed;
   }
   public void makeNoise() {
       System.out.println("The Dog " + this.getName() + " is barking.");
   }

   @Override
   public String toString() {
       return "The Dog "+getName()+" of breed " + breed + ".";
   }
   @Override
   public boolean equals(Object O) {
       if (O instanceof Dog && ((Dog) O).getName() == getName())
           return true;

       return false;
   }

}

=====Driver====


public class Driver {
public static void main(String[] args) {
   Animal animal=new Animal("Oscar");
   Dog dog=new Dog("Mutt", "Inka");
   Bird bird=new Bird("Tweety");
   animal.toString();
   dog.toString();
   bird.toString();
   System.out.println(" ");
   animal.makeNoise();
   dog.makeNoise();
   bird.makeNoise();
   System.out.println(" ");
   animal.sleep();
   dog.sleep();
   bird.sleep();
}
}

=====O/P===

The Animal Oscar has been created.
A Dog Inka of breed Mutt has been created.
A Bird Tweety has been created.


The Animal Oscar is making a noise.
The Dog Inka is barking.
The Bird Tweety is chirping.


The Animal Oscar is asleep.
The Animal Inka is asleep.
The Animal Tweety is asleep.

====

Let me know if you have any doubts. Thanks.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote