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
Exercise 8 (The interface class-like) Assume you have the Edible interface with its abstract method Design...
(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...
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...
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...
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 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, 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 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 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 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...
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....