Java Abstract Class and Interface

Java Abstract Classe and Interface

In this exercise you are about to complete several missing methods in classes Animal, Dog, and Bird in the following Java program. The program is organized in two files: TestAnimal.java and Flyable.java. You are not allowed to modify class TestAnimal and its main method. Based on the main method and the output given at the end of the code, complete those missing methods in those three classes.

//Flyable.java
public interface Flyable
{
	public abstract void fly();
}
			
//TestAnimal.java 
public class TestAnimal
{
	public static void main(String []args)
	{
	   Animal d1 = new Dog(50.2, 101, "German Shepherd");
	   Dog d2 = new Dog(40.5, 102, "Lab");	 
	   Animal b1 = new Bird(0.35, 103, "Blue");
	   Flyable b2 = new Bird(0.55, 104, "Red");
	   
	   if (d1 instanceof Dog)
			((Dog)d1).speak();
	   
	   b1.speak();
	   b2.fly();
	   
	   try{		   
			Dog d3 = (Dog)d2.clone();
			Bird b3 = (Bird)((Bird)b1).clone();
			d3.speak();
			b3.speak();
			b3.fly();
	   } catch (Exception e){
		   e.printStackTrace();
	   }

	   System.out.println(d2.compareTo(d1));
	   System.out.println(((Bird)b2).compareTo(b1));
	}
}

abstract class Animal
{
	protected double weight;
	protected int tag;
	
    public abstract void speak();
}

class Dog extends Animal implements Comparable<Animal>, Cloneable
{
	private String breed;
	
	//Implement a compareTo method that returns 1 if the caller object weighs more than the object as parameter;
	//returns 0 if they weigh equally; returns -1 if the caller object weighs less than the object as parameter
	public int compareTo(Animal a)
	{
	}
	
	public Object clone() throws CloneNotSupportedException
	{
	}
}

class Bird extends Animal implements Flyable, Comparable<Animal>, Cloneable
{
	private String color;
}

// Output:

// German Shepherd dog with a tag of 101 woof!
// Blue bird with a tag of 103 tweet!
// Red bird with a tag of 104 is flying!
// Lab dog with a tag of 102 woof!
// Blue bird with a tag of 103 tweet!
// Blue bird with a tag of 103 is flying!
// -1
// 1