For this assignment you will create TWO java classes and ONE java interface. All three will belong to package MyAnimals.
Here is a diagram of the relationships of the java classes and packages. see image.
Some things to note:
Abstract class Animal:
1. Note that the class does not have a default constructor. This is intentional
2. Also note that there is only one constructor. Use the String and int variables it receives to set the name and legs values, respectively.
3. As mentioned before, the toString method is abstract. Please recall that this method has a very specific definition signature. You must define it according to this signature so that the default toString method is overridden.
Interface Walkable:
1. This is a very simple interface. It only includes one method.
2. Make sure this method is public. And note that it returns nothing
3. walk method simply prints to System.out a message similar to this:
I am walking with 0 leg(s).
Please note that the actual number of legs will be set by the Dog/Animal class.
Class Dog:
1. It has four constructors. Please use UNNAMED and 0 as the values of name and legs if they are not specifically used in constructor (i.e. use those values as the default initialization values)
a. Default constructor should use UNNAMED and 0
b. String constructor should use 0 for the legs
c. int constructor should use UNNAMED for the name
d. String, int will be passed to the Animal constructor.
2. Because all the setter methods are private to class Animal, you must call the Animal(String, int) constructor directly from class Dog.
3. Because toString method is declared abstract in Animal, therefore Dog must define this method.
4. Also because Dog implements Walkable, it needs to also define walk method.
Please insert the following main class into your Dog class so that your assignment can be compiled and run.
public static void main(String[] args) {
Dog[] dogs = new Dog[4];
dogs[0] = new Dog();
dogs[1] = new Dog(2);
dogs[2] = new Dog("Rex");
dogs[3] = new Dog("Fido", 4);
for (int i = 0; i < 4; i++) {
System.out.println(dogs[i]);
dogs[i].walk();
}
}
If everything is coded correctly your output should look like the following.
My name is UNNAMED and I have 0 leg(s).
I am walking with 0 leg(s).
My name is UNNAMED and I have 2 leg(s).
I am walking with 2 leg(s).
My name is Rex and I have 0 leg(s).
I am walking with 0 leg(s).
My name is Fido and I have 4 leg(s).
I am walking with 4 leg(s).