Class composition help needed

hi

i have this task to do but im struggling with this second question for the last 6 hours.

these are the instructions:
let’s add composition. Make a new class called Home . Lots of people have dogs and cats in their homes. Home should have a function called adoptPet that takes any Animal as an input. The new pet should be stored in the Home object in an array/list. The Home object should also have a function called makeAllSounds .

It should work like this:

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();


home.makeAllSounds();// this doesn't give/return any result/data
home.adoptPet(dog1);
home.makeAllSounds();
// this prints :
// Dog barks

home.adoptPet(cat);
home.makeAllSounds();
// this prints :
// Dog barks
// Cat meows

home.adoptPet(dog2);
home.makeAllSounds();
// this prints :
// Dog barks
// Cat meows
//Dog barks

this is my code so far:

class Animal {

    constructor(name) {

        this.name = name;

    }

    eat() {

        return `${this.name} eats`

    }

    sounds() {

        return

    }

}

class Dog extends Animal{

    constructor(name) {

        super(name);

        this.name = name

    }

    sounds() {

        return "Dog barks"

    }

}

class Cat extends Animal{

    constructor(name) {

        super(name);

        this.name = name

    }

    sounds() {

        return "Cat meows"

    }

}

class Home extends Animal{

    constructor(name) {

        super(name);

        this.newPet = [];

    }

    adoptPet(animal) {

        this.newPet.push(animal)

    }

    makeAllSounds() {

        sounds();

        sounds();

    }

}

I tried using separate functions but i cannot use it as stated in the instructions.
please help me compose both animal sounds and help with adding pets to the adoptPet array so it accumulates as shown above how it should output.

Ok, start by thinking about this logically, what you have described in your code.

Lets say you have a pet, a cat. A cat is a type of animal. Is your house an animal?

i should make the class “home extends cat or dog” but how do i do that without repeating myself? like inheriting both animals sounds?

Is your house a cat? Is your house a dog?

Again, think about this logically. I don’t think you’re a flea, so I’m going to assume your house isn’t an animal. Is it at all logical to say that “Home” is a type of animal?

well its how i tried to output the sounds without repeating myself. I need to know what methods to use where, i now know house shouldnt extends animal, animal should extends house. House sould be main class, its second part of task so i added house clas afterwards

No, an animal is not a type of house. Again, think about this logically. A house doesn’t make animal sounds. A house may contain some animals, and the animals make sounds.

You have some objects of the type Animal. An object of the type House may store those objects, but it isn’t one of those objects

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.