In: Computer Science
Java
Write a program that will define a hierarchy of classes for Insect, Butterfly, Bee, and Mosquito, and define the attributes that are common to all in the superclass, while defining customized attributes in each subclass. Common attributes may include numberOfWings, numberOfLegs, numberOfEyes. Come up with at least 1 additional attribute for each subclass. Define a polymorphic method, fly(), at each level of the hierarchy, which will print the an imaginary sound that the insect will make when it flies. Define a constructor for each class that will receive the number of wings, the number of legs, and the number of eyes.
Next, write a driver class that will create an arrayList of Insect objects. In the arrayList, add an instance of each of the subclasses, Butterfly, Bee, and Mosquito. Once the arrayList is populated, iterate through the arrayList, and call the polymorphic method fly() for each member of the arrayList. Then, end the program.
Insect.java
public class Insect {
int numberOfWings;
int numberOfLegs;
int numberOfEyes;
public int getNumberOfWings() {
return numberOfWings;
}
public void setNumberOfWings(int numberOfWings)
{
this.numberOfWings =
numberOfWings;
}
public int getNumberOfLegs() {
return numberOfLegs;
}
public void setNumberOfLegs(int numberOfLegs) {
this.numberOfLegs =
numberOfLegs;
}
public int getNumberOfEyes() {
return numberOfEyes;
}
public void setNumberOfEyes(int numberOfEyes) {
this.numberOfEyes =
numberOfEyes;
}
public Insect(int numberOfWings, int numberOfLegs, int
numberOfEyes) {
super();
this.numberOfWings =
numberOfWings;
this.numberOfLegs =
numberOfLegs;
this.numberOfEyes =
numberOfEyes;
}
public void fly() {
System.out.println("Insect is
flying");
}
}
Butterfly.java
public class Butterfly extends Insect{
double lifeTime = 12;
public Butterfly(int numberOfWings, int
numberOfLegs, int numberOfEyes) {
super(numberOfWings, numberOfLegs,
numberOfEyes);
// TODO Auto-generated constructor
stub
}
public void fly() {
System.out.println("Bufferfly is
flying");
}
@Override
public String toString() {
return "Butterfly [lifeTime=" +
lifeTime + " Months, numberOfWings=" + numberOfWings + ",
numberOfLegs=" + numberOfLegs
+ ", numberOfEyes=" + numberOfEyes + "]";
}
}
Bee.java
public class Bee extends Insect{
double lifeTime = 4;
public Bee(int numberOfWings, int numberOfLegs, int
numberOfEyes) {
super(numberOfWings, numberOfLegs,
numberOfEyes);
// TODO Auto-generated constructor
stub
}
public void fly() {
System.out.println("Bee is
flying");
}
@Override
public String toString() {
return "Bee [lifeTime=" + lifeTime
+ " Months, numberOfWings=" + numberOfWings + ", numberOfLegs=" +
numberOfLegs
+ ", numberOfEyes=" + numberOfEyes + "]";
}
}
Mosquito.java
public class Mosquito extends Insect{
double lifeTime = 1.5;
public Mosquito(int numberOfWings, int
numberOfLegs, int numberOfEyes) {
super(numberOfWings, numberOfLegs,
numberOfEyes);
// TODO Auto-generated constructor
stub
}
public void fly() {
System.out.println("Mosquito is
flying");
}
@Override
public String toString() {
return "Mosquito [lifeTime=" +
lifeTime + " Months, numberOfWings=" + numberOfWings + ",
numberOfLegs=" + numberOfLegs
+ ", numberOfEyes=" + numberOfEyes + "]";
}
}
TestDriver.java
import java.util.ArrayList;
public class TestDriver {
public static void main(String[] args) {
// TODO Auto-generated method
stub
ArrayList<Insect> insects
= new ArrayList<>();
insects.add(new Butterfly(2, 2,
2));
insects.add(new Bee(2, 4,
2));
insects.add(new Mosquito(2, 2,
1));
for(int
i=0;i<insects.size();i++) {
System.out.println(insects.get(i));
insects.get(i).fly();
}
}
}
Output
Butterfly [lifeTime=12.0 Months, numberOfWings=2,
numberOfLegs=2, numberOfEyes=2]
Bufferfly is flying
Bee [lifeTime=4.0 Months, numberOfWings=2, numberOfLegs=4,
numberOfEyes=2]
Bee is flying
Mosquito [lifeTime=1.5 Months, numberOfWings=2, numberOfLegs=2,
numberOfEyes=1]
Mosquito is flying
Feel free to ask any doubts, if you face any difficulty in understanding.
Please upvote the answer if you find it
helpful