Question

Using java, Represent the following using classes and interfaces: Dogs, Fish, and BoxTurtles are all Animals....

Using java, Represent the following using classes and interfaces: Dogs, Fish, and BoxTurtles are all Animals. All swim except BoxTurtles. Next, make a list containing all kinds of animals, fill it and print it; then make a list of only animals that can swim

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please rate the answer.

Thank You !


===========================================================================

public class Animal {

    private boolean canSwim;
    private String animalType;

    public Animal(String animalType) {
        canSwim = false;
        this.animalType = animalType;

    }

    public void setAnimalType(String animalType) {
        this.animalType = animalType;
    }

    public String getAnimalType() {
        return animalType;
    }

    public Animal(boolean canSwim) {
        this.canSwim = canSwim;
    }

    public boolean isCanSwim() {
        return canSwim;
    }

    public void setCanSwim(boolean canSwim) {
        this.canSwim = canSwim;
    }

    @Override
    public String toString() {
        if (canSwim) {
            return animalType + " can swim";
        } else {
            return animalType + " can swim";
        }
    }
}

=================================================================

public class Dog extends Animal {

    private static String animalType = "Dog";

    public Dog() {
        super(animalType);
        setCanSwim(true);
    }


}

=================================================================

public class Fish extends Animal {

    private static String animalType = "Fish";

    public Fish() {
        super(animalType);
        setCanSwim(true);
    }

}

=================================================================

public class BoxTurtles extends Animal {

    private static String animalType = "Box Turtles";

    public BoxTurtles() {
        super(animalType);
        setCanSwim(false);
    }


}

=================================================================

import java.util.ArrayList;

public class MainAnimal {

    public static void main(String[] args) {


        // Next, make a list containing all kinds of animals,
        ArrayList<Animal> animals = new ArrayList<>();

        animals.add(new Dog());
        animals.add(new Fish());
        animals.add(new BoxTurtles());

        // fill it and print it;
        for (Animal animal : animals) {
            System.out.println(animal);
        }


        //then make a list of only animals that can swim
        ArrayList<Animal> swimmingAnimals = new ArrayList<>();

        swimmingAnimals.add(new Dog());
        swimmingAnimals.add(new Fish());
        System.out.println("Animals that can swim only");
        System.out.println("==========================");
        // fill it and print it;
        for (Animal animal : swimmingAnimals) {
            System.out.println(animal);
        }
    }
}

=================================================================

Add a comment
Know the answer?
Add Answer to:
Using java, Represent the following using classes and interfaces: Dogs, Fish, and BoxTurtles are all Animals....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT