1)- Create a specialized version of Insects called FlyingInsects which have the
ID: 3794252 • Letter: 1
Question
1)- Create a specialized version of Insects called FlyingInsects which have the ability to fly.
2)- Create a specialized version of Insects called Butterfly which has a state called cocoon and the
ability of metamorphosis
cocoon is an object of class Cacoon which contains only a toString method which
returns the string “Cacoon” (same as class Stinger in Listing C)
metamorphosis contains a println which utilizes cacoon (same as protectHive in
Listing C)
3)- Refactor the Honeybee and Grasshopper classes into the new hierarchy:
honeybees and butterflies can fly while grasshoppers cannot
4)- Modify the BugCollection main program to instantiate a Butterfly object and to call all methods
of all Insect subclasses
insect.java
class bodysection
{
private String name;
public bodysection(String s)
{
name = s;
}
public String toString()
{
return name;
}
public class insect
{
private bodysection head;
private bodysection thorax;
private bodysection abdomen;
public insect()
{
head = new bodysection("head");
thorax = new bodysection("thorax");
abdomen = new bodysection("abdomen");
}
public void respireLungs()
{
System.out.println("An insect respires through lungs" + " in its " + thorax);
}
public void reproduceEggs()
{
System.out.println("An insect reproduces by laying eggs" + " from its " + abdomen);
}
}
Explanation / Answer
import java.util.Scanner;
insect.java
class bodysection
/**
* This class encapsulates the logic and state for a simplified
{
private String name;
public bodysection(String s)
{
name = s;
}
public String toString()
{
return name;
}
public class insect
{
private bodysection head;
private bodysection thorax;
private bodysection abdomen;
public insect()
{
head = new bodysection("head");
thorax = new bodysection("thorax");
abdomen = new bodysection("abdomen");
}
public void respireLungs()
{
System.out.println("An insect respires through lungs" + " in its " + thorax);
}
public void reproduceEggs()
{
System.out.println("An insect reproduces by laying eggs" + " from its " + abdomen);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.