3

I am stuck at a homework problem.

It asks me to design interfaces Drawable, Rotatable, Resizable and Sounds. Develop a progrma that does the below.

An Animal class that will have a name attribute and setter/getter methods for name. Animal can make sounds and is drawable, rotatable, and resizable. A Vehicle class that have name and age attributes and setter/getter methods for name and age. Vehicle can also make sounds and is drawable, rotatable, and resizable as well. Create a collection of 2 vehicles and 2 animals stored in the same array.

I have created the interfaces:

interface Drawable
{
    void drawObject();
}
interface Rotatable
{
    void rotateObject();
}
interface Resizable
{
    void resizeObject();
}
interface Sounds
{
    void playSound();
}

Parents classes (Animal and Vehicle) and the child classes (Panda and Tiger for Animal, Honda and Toyota for Vehicle).

abstract class Animal implements Drawable, Rotatable, Resizable, Sounds
{
    String name;
    public abstract void setName();
    public abstract String getName();
    public abstract void drawObject();
    public abstract void rotateObject();
    public abstract void resizeObject();
    public abstract void playSound();
}

class Panda extends Animal
{
    public void setName()
    {
        System.out.println("Panda's name is Bao Bao");
    }
    public String getName()
    {
        return name;
    }
    public void drawObject()
    {
        System.out.println("Drawing a Panda");
    }
    public void rotateObject()
    {
        System.out.println("Rotating a Panda");
    }
    public void resizeObject()
    {
        System.out.println("Resizing a Panda");
    }
    public void playSound()
    {
        System.out.println("Panda sound");
    }
}

class Tiger extends Animal
{
    public void setName()
    {
        System.out.println("Tiger's name is Si Si");
    }
    public String getName()
    {
        return name;
    }
    public void drawObject()
    {
        System.out.println("Drawing a Tiger");
    }
    public void rotateObject()
    {
        System.out.println("Rotating a Tiger");
    }
    public void resizeObject()
    {
        System.out.println("Resizing a Tiger");
    }
    public void playSound()
    {
        System.out.println("Tiger sound");
    }
}

abstract class Vehicle implements Drawable, Rotatable, Resizable, Sounds
{
    String name;
    int age;
    public abstract void setName();
    public abstract String getName();
    public abstract void setAge();
    public abstract int getAge();
    public abstract void drawObject();
    public abstract void rotateObject();
    public abstract void resizeObject();
    public abstract void playSound();
}

class Honda extends Vehicle
{
    public void setName()
    {
        System.out.println("Honda's name is Civic");
    }
    public String getName()
    {
        return name;
    }
    public void setAge()
    {
        System.out.println("Honda is 10 years old");
    }
    public int getAge()
    {
        return age;
    }
    public void drawObject()
    {
        System.out.println("Drawing a Honda");
    }
    public void rotateObject()
    {
        System.out.println("Rotating a Honda");
    }
    public void resizeObject()
    {
        System.out.println("Resizing a Honda");
    }
    public void playSound()
    {
        System.out.println("Honda sound");
    }
}

class Toyota extends Vehicle
{
    public void setName()
    {
        System.out.println("Toyota's name is Camry");
    }
    public String getName()
    {
        return name;
    }
    public void setAge()
    {
        System.out.println("Honda is 1 year old");
    }
    public int getAge()
    {
        return age;
    }
    public void drawObject()
    {
        System.out.println("Drawing a Toyota");
    }
    public void rotateObject()
    {
        System.out.println("Rotating a Toyota");
    }
    public void resizeObject()
    {
        System.out.println("Resizing a Toyota");
    }
    public void playSound()
    {
        System.out.println("Toyota sound");
    }
}

In my main method I was able to create 2 arrays and loop through each of them to display the output. My question is how I can go about combining the 2 different classes in one array and run the loop.

import java.util.Arrays;

public class ManipulateAnimals
{
    public static void main(String[] args)
    {
        Animal[] animal = {new Panda(), new Tiger()};
        Vehicle[] vehicle = {new Honda(), new Toyota()};

        for(int i = 0; i < animal.length; i++)
        {
            animal[i].setName();
            animal[i].getName();
            animal[i].drawObject();
            animal[i].rotateObject();
            animal[i].resizeObject();
            animal[i].playSound();
            System.out.println(" ");
        }


        for(int i = 0; i < vehicle.length; i++)
        {
            vehicle[i].setName();
            vehicle[i].getName();
            vehicle[i].setAge();
            vehicle[i].getAge();
            vehicle[i].drawObject();
            vehicle[i].rotateObject();
            vehicle[i].resizeObject();
            vehicle[i].playSound();
            System.out.println(" ");
        }
    }
}
3
  • You can use object[] and use instance of in loop and typecast the object to original class to call their methods. Commented Oct 17, 2019 at 6:56
  • Your setters don't make much sense, and your assignment doesn't require you to make Animal and Vehicle abstract, nor to create subclasses of these classes. But anyway, The array can ba an array of Object, or of Drwable, or of Rotatable, or of Resizable, since all are common types of the two classes. Commented Oct 17, 2019 at 6:57
  • Thanks JB Nizet for the feedback. I am taking baby steps to learn interfaces and poloymorphisim. I am constantly looking for ways to improve code efficiency and comprehensiveness. Could you please suggest a cleaner approach for this problem? Commented Oct 17, 2019 at 17:58

2 Answers 2

2

Usually such kind of problem is solved by introducing a new upper-level interface.

So, you will have Drawable extends Editable, Rotatable extends Editable and so on.

After that you'll be able to create an array of Editable.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Eduard!
1

Your classes Panda and Tiger extend the class Animal. Therefore your can store both of them in the Animal[] array.

It's the same for interfaces: All your classes Panda, Tiger, Honda and Toyota implement e.g. the interface Drawable (or Rotatable or ...) so you could store all of them in an array of type Drawable like this:

Drawable[] drawables = new Drawable[] {new Panda(), new Tiger(), new Honda(), new Toyota()};

But now you're facing the problem that a Drawable object has no method rotateObject() (and the other methods you want to call).

So a solution would be to create a new interface that extends all other interfaces and make the classes Animal and Vehicle extend this interface:

public interface FindAGoodNameForThisInterface extends Drawable, Rotatable, Resizable, Sounds {
    //this interface can be empty because it extends the other interfaces and inherits it's methods
    //NOTE: extending multiple classes or interfaces only works for interfaces that extend other interfaces; not for classes
}

public abstract class Animal implements FindAGoodNameForThisInterface {
    //... your Animal class code goes here
    //(you don't need to declare the methods from the interfaces here)
}

public abstract class Vehicle implements FindAGoodNameForThisInterface {
    //... your Vehicle class code goes here
}

Now your main method could look like this:

public static void main(String[] args)
{
    FindAGoodNameForThisInterface[] all = {new Panda(), new Tiger(), new Honda(), new Toyota()};

    for(int i = 0; i < all.length; i++)
    {
        all[i].setName();
        all[i].getName();
        all[i].drawObject();
        all[i].rotateObject();
        all[i].resizeObject();
        all[i].playSound();
        System.out.println(" ");
    }
}

1 Comment

Thanks Tobias! This approach is working like a cham!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.