Question

Exercise 8 (The interface class-like) Assume you have the Edible interface with its abstract method Design a class named Anim

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// Edible.java

public interface Edible {
   public void howToEat();

   public void sound();
}
______________________

// Animal.java

public abstract class Animal {
public abstract void sound();
}
_____________________

// Mammal.java

public abstract class Mammal extends Animal {


}

_____________________

// Dairy.java

public abstract class Dairy extends Animal implements Edible{

}

_____________________

// Bear.java

public class Bear extends Mammal {

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

   }

}
______________________

// Chicken.java

public class Chicken extends Dairy {

   @Override
   public void howToEat() {
       System.out.println("It eats grains.");

   }

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

   }

}

________________________

// Cow.java

public class Cow extends Dairy {

   @Override
   public void howToEat() {
       System.out.println("It eats grass.");

   }

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

   }

}

__________________________

// Sheep.java

public class Sheep extends Mammal implements Edible {

   @Override
   public void howToEat() {
       System.out.println("It eats grass.");

   }

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

   }

}

__________________________

// Test.java

import java.util.ArrayList;

public class Test {

   public static void main(String[] args) {

       ArrayList<Animal> arl=new ArrayList<Animal>();
       arl.add(new Sheep());
       arl.add(new Bear());
       arl.add(new Chicken());
       arl.add(new Cow());
       for(int i=0;i<arl.size();i++)
       {
           if(arl.get(i) instanceof Edible)
           {
               System.out.println(arl.get(i).getClass().getSimpleName()+":");
               ((Edible)arl.get(i)).howToEat();
               arl.get(i).sound();
           }
           else
           {
               System.out.println(arl.get(i).getClass().getSimpleName()+":");
              
               arl.get(i).sound();
           }
       }
_______________________

Output:

Sheep:
It eats grass.
Baa Baa.
Bear:
growl growl
Chicken:
It eats grains.
cluck cluck
Cow:
It eats grass.
Moo Moo.

_______________Could you plz rate me well.Thank You

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

    (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 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 diagram for the classes and the interface 2. Use Arraylist class to create an...

  • 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...

  • 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...

  • 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...

  • 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...

  • 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...

  • Define an interface FarmAnimal that contains two methods: getName() and talk(), each returns a string. Declare...

    Define an interface FarmAnimal that contains two methods: getName() and talk(), each returns a string. Declare an abstract class FarmAnimalBase that implements the interface FarmAnimal. In the class FarmAnimalBase, define a private final instance variable name (type: String), a public constructor that initializes the value of the instance variable name, and a public method getName() that returns the value of the instance variable name. Note that we do not implement the method talk() declared in the interface FarmAnimal, that’s the...

  • Class MediaMerger This class is meant to host the main method used to test all the...

    Class MediaMerger This class is meant to host the main method used to test all the other classes in our project. We reccomend that you work on implementing the main method in this class progressively, as you add features to the other classes of this project. Do not attempt to implement all requirements for the main method before the other classes are able to be used in that manner. Work progressively, as demonstrated during the live coding videos in the...

  • Java programming 1. (Triangle class) Design a new Triangle class that extends the abstract GeometricObject class....

    Java programming 1. (Triangle class) Design a new Triangle class that extends the abstract GeometricObject class. Draw the UML diagram for the classes Triangle and GeometricObject and then implement the Triangle class. Write a test program that prompts the user to enter three sides of the triangle, a color, and a Boolean value to indicate whether the triangle is filled. The program should create a Triangle object with these sides and set the color and filled properties using the input....

  • 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....

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
ADVERTISEMENT