Question: In Java, I need to implement the following: My problem stipulates the following, dealing with birds: 1. Birds can fly. 2. Ducks fly but also
My problem stipulates the following, dealing with birds:
1. Birds can fly.
2. Ducks fly but also swim.
3. Penguins do not fly.
4. Penguins deep dive, unlike ducks.
5. Only parrots can talk.
Here is my code:
import java.util.*; public class HomeworkSixB { public static void main(String[] args) { Bird birds[] = new Bird[3]; birds[0] = new Duck(true, false, false, false); birds[1] = new Penguin(false, true, false, true); birds[2] = new Parrot(true, false, true, false); } }
abstract class Bird { abstract boolean flight(); abstract boolean deepdive(); abstract boolean talk(); abstract boolean swim(); Bird (boolean flight, boolean deepdive, boolean talk, boolean swim) { this.flight = flight; this.deepdive = deepdive; this.talk = talk; this.swim = swim; }
}
abstract class Duck extends Bird { public Duck(boolean flight, boolean deepdive, boolean talk, boolean swim) { super(flight, deepdive, talk, swim); } abstract public boolean flight(); abstract public boolean deepdive(); abstract public boolean talk(); abstract public boolean swim(); }
abstract class Penguin extends Bird { public Penguin(boolean flight, boolean deepdive, boolean talk, boolean swim) { super(flight, deepdive, talk, swim); } abstract public boolean flight(); abstract public boolean deepdive(); abstract public boolean talk(); abstract public boolean swim(); }
abstract class Parrot extends Bird { public Parrot(boolean flight, boolean deepdive, boolean talk, boolean swim) { super(flight, deepdive, talk, swim); } abstract public boolean flight(); abstract public boolean deepdive(); abstract public boolean talk(); abstract public boolean swim(); }
However, this doesn't work; I get \"Cannot instantiate the type Duck/Penguin/Parrot\" and \"flight/deepdive/talk/swim cannot be resolved or is not a field.\" I'm trying to get there to be boolean values for each activity by each bird.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
