Question

(The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal and its

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Please find the required solution:

<<Java Class>> GAnimal (default package) Animal) d sound():void <<Java Class»> <<Java Class»> c Diary (default package) GMamm

/*Please find the required solution*/

import java.util.ArrayList;

//test method to save animal objects to array list
public class AnimalTest {
    public static void main(String[] args) {
        // define ArrayList
        ArrayList<Animal> animals = new ArrayList<>();

        // add animals to ArrayList
        animals.add(new Chicken());
        animals.add(new Bear());
        animals.add(new Cow());
        animals.add(new Sheep());

        // iterate through animals list
        for(Animal animal:animals){
            // invoke sound method
            animal.sound();

            // invoke how to eat if edible
            if(animal instanceof Edible) {
                Edible edible = (Edible)animal;
                edible.howToEat();
            }
        }

    }
}

// interface edible
interface Edible {
    void howToEat();

    void sound();
}

// abstract class animal
abstract class Animal {
    public abstract void sound();
}

abstract class Mammal extends Animal {
}

abstract class Diary extends Animal implements Edible {
}

class Sheep extends Mammal implements Edible {

    @Override
    public void howToEat() {
        System.out.println("Sheep Eat");
    }

    @Override
    public void sound() {
        System.out.println("Sheep sound");
    }
}

class Bear extends Mammal {
    @Override
    public void sound() {
        System.out.println("Bear Sound");
    }
}

class Chicken extends Diary {

    @Override
    public void howToEat() {
        System.out.println("Chicken Eat");
    }
    @Override
    public void sound() {
        System.out.println("Chicken Sound");
    }
}

class Cow extends Diary {

    @Override
    public void howToEat() {
        System.out.println("Cow Eat");
    }
    @Override
    public void sound() {
        System.out.println("Cow Sound");
    }
}

sample output:

Chicken Sound Chicken Eat Bear Sound Cow Sound Cow Eat Sheep sound Sheep Eat

Add a comment
Know the answer?
Add Answer to:
(The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal a...
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
  • Exercise 8 (The interface class-like) Assume you have the Edible interface with its abstract method Design...

    Exercise 8 (The interface class-like) Assume you have the Edible interface with its abstract method Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make Chicken and Cow as subclasses of Dairy. The Sheep and Dairy classes implement the Edible interface. howToEat) and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML...

  • Design an interface named Colorable with a void method named howToColor(). Every class of a colorable...

    Design an interface named Colorable with a void method named howToColor(). Every class of a colorable object must implement the Colorable interface. Design a class named Square that extends GeometricObjectand implements Colorable. Design another class named Trianglethat extends GeometricObjectand implements Colorable. Implement howToColor inSquareto display the message Color all four sides. ImplementhowToColor inTriangleto display the message Color all three sides. Draw a UML diagram that involves Colorable, Square, Triangle, and GeometricObject. Write a test program that creates an array of...

  • This assignment involves writing a program that uses an abstract class and concrete subclasses. An abstract...

    This assignment involves writing a program that uses an abstract class and concrete subclasses. An abstract Vehicle class declares an abstract method named display() that is used to display vehicles. Vehicle has two subclasses: Boat and Car. A Boat has a length attribute, and a draft attribute (a draft is how far below the surface the bottom of the boat is). A Car has a make attribute and a year attribute. Appropriate getter, setter, and constructor methods should be defined...

  • Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman...

    Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman, sophomore, junior, or senior). Define the possible status values using an enum. Staff has an office, salaray, and date-hired. Implement the above classes in Java. Provide Constructors for classes to initialize private variables. Getters and setters should only be provided if needed. Override the toString() method...

  • Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named...

    Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type(does not mention the data type of the variable coffeeType), price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public...

  • a c++ program (The Person, Student, Employee, Faculty, and Staff classes) Design a class named Person...

    a c++ program (The Person, Student, Employee, Faculty, and Staff classes) Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name, address, phone number, and email address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, and date hired. Use the MyDate classto create an object for date hired. A faculty...

  • Java Programming Design a class named Person and its two subclasses named Student and Employee. A...

    Java Programming Design a class named Person and its two subclasses named Student and Employee. A person has a name, address, phone number, and email address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, date hired. Define a class named MyDate that contains the fields year, month, and day. Override the toString method in each class to display the class name and the person's name....

  • 1. Design and implement classes. Design a class named Account and its two subclasses named Checking and Saving. Make Flexible and NotFlexbile subclasses of Saving. An account has a name, address, phon...

    1. Design and implement classes. Design a class named Account and its two subclasses named Checking and Saving. Make Flexible and NotFlexbile subclasses of Saving. An account has a name, address, phone number, and balance. A checking account has a overdraft limit. A saving account has an interest rate. A flexible saving account has the accumulated interest rate. A none flexible account has the expected accumulated interest rate at the maturity of the account. Override the toString method in each...

  • JAVA Problem:  Coffee    Design and implement a program that manages coffees. First, implement an abstract class...

    JAVA Problem:  Coffee    Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type, price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public void prepare (); } The prepare method will...

  • In this next example, we'll build two classes that implement the Cloneable Interface. The QuizTracker class...

    In this next example, we'll build two classes that implement the Cloneable Interface. The QuizTracker class contains an ArrayList of QuizScore objects, and it's up to us to build these two classes so that we can make deep copies of QuizTrackers (and share no internal aliases). The key idea here is that to make deep copies of compound objects (or lists of objects), we need to copy (using clone) every object in every list, and even make copies of the...

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