Question

In: Computer Science

Java Write a program that will define a hierarchy of classes for Insect, Butterfly, Bee, and...

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.

Solutions

Expert Solution

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


Related Solutions

You will write several classes for this program in Java. The name of all classes for...
You will write several classes for this program in Java. The name of all classes for this program will start with y2y3. Concepts to be applied : Classes and objects, inheritance, polymorphism Assignment: An electronic store, named carrefoure, services many customers. The customers’ orders for electronics are delivered. Carrefoure bills their customers once each month. At the end of each month, the regional store manager requests a report of all customers. In this program, you will develop the inheritance hierarchy...
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
Write a program in java which is in main and uses no classes, methods, loops or...
Write a program in java which is in main and uses no classes, methods, loops or if statements Ask the user to enter their first name Ask the user to enter their last name Print their initials followed by periods (ie. Clark Kent = C. K.) Print the number of letters in their last name
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel....
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel. A dog consists of the following information: • An integer age. • A string name. If the given name contains non-alphabetic characters, initialize to Wolfy. • A string bark representing the vocalization the dog makes when they ‘speak’. • A boolean representing hair length; true indicates short hair. • A float weight representing the dog’s weight (in pounds). • An enumeration representing the type...
Define what an array index value is in JAVA. Then, write a JAVA program that passes...
Define what an array index value is in JAVA. Then, write a JAVA program that passes an array to a method and finds the average value or mean value (add up the numbers in the array and divide by the number of values) of the array and prints it out.
Write a java program with the following classes: Class Player Method Explanation: play : will use...
Write a java program with the following classes: Class Player Method Explanation: play : will use a loop to generate a series of random numbers and add them to a total, which will be assigned to the variable score. decideRank: will set the instance variable rank to “Level 1”, “Level 2”, “Level 3”, “Level 4” based on the value of score, and return that string. getScore : will return score. toString: will return a string of name, score and rank....
Define the classes to complete dynamic array hierarchy with a concrete, abstract and interface class. public...
Define the classes to complete dynamic array hierarchy with a concrete, abstract and interface class. public class DArray { private int array[]; public DArray() { } private void expandArray() { } private void shrinkArray() { } } --------------------------------------------------------------- public abstract class ArrayBP { protected int numElements; protected int numAllocations; public abstract void storeAt(int item, int index) { } public abstract getFrom(int index) { } public abstract int len() { } public abstract void remove();{ } public abstract void removeAt(int index)...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape,...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape, Two Dimensional Shape, Three Dimensional Shape, Square, Circle, Cube, Rectangular Prism, and Sphere. Cube should inherit from Rectangular Prism. The two dimensional shapes should include methods to calculate Area. The three dimensional shapes should include methods to calculate surface area and volume. Use as little methods as possible (total, across all classes) to accomplish this, think about what logic should be written at which...
Write a Java Program using the concept of objects and classes. Make sure you have two...
Write a Java Program using the concept of objects and classes. Make sure you have two files Employee.java and EmployeeRunner.java. Use the template below for ease. (Please provide detailed explanation) Class Employee Class Variables: Name Work hour per week Hourly payment Class Methods: - Get/Sets - generateAnnualSalary(int:Duration of employment)               annual salary = Hourly payment * Work hours per week * 50 If the employee is working for more than 5 years, 10% added bonus             -void displayAnnualSalary ( )...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT