Java Create four classes: 1. An Animal class that acts as a superclass for Dog a
ID: 3574167 • 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
Here are the classes:
public class Animal {
private String name;
public Animal(String name) {
this.name = name;
System.out.println("The Animal " + this.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 "+ this.name+" is asleep.");
}
public void makeNoise() {
System.out.println("The Animal "+ this.name+" is making a noise.");
}
@Override
public String toString() {
return "The Animal " + name;
}
public boolean equals( Object obj )
{
boolean flag = false;
Animal animal = ( Animal )obj;
if( animal.name == name )
flag = true;
return flag;
}
}
public class Bird extends Animal {
public Bird(String name) {
super(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 " + getName();
}
public boolean equals( Object obj )
{
boolean flag = false;
Bird bird = ( Bird )obj;
if( bird.getName() == getName() )
flag = true;
return flag;
}
}
public class Dog extends Animal {
private String breed;
public Dog(String name, String breed) {
super(name);
this.breed = breed;
System.out.println("A dog "+ name+" of breed "+this.breed+" has been created.");
}
public void makeNoise() {
System.out.println("The Dog "+ this.getName() +" is Barking.");
}
@Override
public String toString() {
return "The Dog "+ this.getName() +" of breed " + breed ;
}
public boolean equals( Object obj )
{
boolean flag = false;
Dog dog = ( Dog )obj;
if( dog.getName() == getName() && dog.breed == breed )
flag = true;
return flag;
}
}
Here is the driver class:
public class driver {
public static void main(String[] args) {
Animal animal = new Animal("Oscar");
Dog newDog = new Dog("Inka", "Mutt");
Bird newBird = new Bird("Tweety");
String animalString = animal.toString();
String dogString = newDog.toString();
String birdString = newBird.toString();
System.out.println(animalString);
System.out.println(dogString);
System.out.println(birdString);
newDog.makeNoise();
newBird.makeNoise();
newDog.sleep();
newBird.sleep();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.