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:

  • The setName and setLegs methods are private. This is intentional. The logic is that once a Dogs name and number of legs are set, they should never be changed.
  • Because of the above, you can only use the set methods from the constructor.
  • The toString method is an abstract method. It is included in the Animal class so that Dog is required to define it
  • Dog is implementing the Walkable interface, so it should define walk method in its class as well.

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).
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.