Please find the required solution:
/*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:
(The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal a...
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...
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...
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...
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...
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...
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....
What is wrong with the following Java Code. public class TestEdible { abstract class Animal { /** Return animal sound */ public abstract String sound(); } class Chicken extends Animal implements Edible { @Override public String howToEat() { return "Chicken: Fry it"; } @Override public String sound() { return "Chicken: cock-a-doodle-doo"; } } class Tiger extends Animal { @Override public String sound() { return "Tiger: RROOAARR"; } } abstract class Fruit implements Edible { // Data fields, constructors, and methods...
Create a data class named Automobile that implements the Comparable interface. Give the class data fields for make, model, year, and price. Then add a constructor, all getters, a toString method that shows all attribute values, and implement Comparable by using the year as the criterion for comparing instances. Write a program named TestAutos that creates an ArrayList of five or six Automobiles. Use a for loop to display the elements in the ArrayList. Sort the Arraylist of autos by...
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...