Blebhebhe
Blebhebhe

Reputation: 93

Manipulating objects , constructors?

Hello english is not my main language i'm not sure how to explain my problem or should the title be titlted

so...please jump to House class !
Thank you in advance

Main class

public class Main {

    public static void main(String[] args) {

        Furniture f1 = new Furniture("chair", 2);
        Furniture f2 = new Furniture("bed", 20);
        Furniture f3 = new Furniture("workbench", 7);
        Furniture f4 = new Furniture("drawer", 15);

        Furniture [] foo = {f1,f2,f3}; 

        // public House(Furniture[] fur)
        House h1 = new House(foo);

        //  public House(Furniture f, House p) 
        House h2 = new House(f4, h1);

    }

}

Furniture class

public class Furniture {

    String name;
    int weight;

    public Furniture(String name, int weight) {
        this.name = name;
        this.weight = weight;
    }

    public static void display(Furniture f) {
    System.out.println("This furniture is a " + f.name + " and weights " + f.weight);
    }

}

House class

// My House class contains one attribut which is an array of Furniture
public class House {

    Furniture[] fur;

    // Should build an empty house
    public House() {

    }

    // Should define the house with the array content
    public House(Furniture[] fur) {
        this.fur = fur;
    }

    // Should build an house containing the Furniture f and the furniture of the House p
    public House(Furniture f, House p) {

    // I'm so confused here, I'm not sure how to start 



    }

}

I've thought about making a new array which length is the array contained in the house and add +1 ( for the furniture f ) then do a loop to get all the furnitures of the house p and add them to the new array with the new furniture f

I tried to do p.length ( p for the house ) but it doesn't work. I kind of understand why but on the other I don't, how can I access to the array of the house ? Is this the wrong approach, I can't find another way

Upvotes: 0

Views: 62

Answers (2)

FilipRistic
FilipRistic

Reputation: 2821

You can't access array like that because your House isn't array, you must say houseInstance.fur in other to get access to array, where houseInstance is an instance of House class.

Considering on how to make new house with furniture from old and another new piece of furniture:

This is how you can do it using your approach:

    public House(Furniture f, House p) {

        // I'm so confused here, I'm not sure how to start 
        this.fur = new Furniture[p.fur.length+1];

        this.fur[0] = f; // Add new furniture

        // Make new instances of furniture from another house(They don't share reference but have same values)
        // Suggested way of doing it
        for(int i =0; i < p.fur.length;i++){
            this.fur[i+1] = new Furniture(p.fur[i].name, p.fur[i].weight);
        }
        /*
           This will share furniture reference between those 2 houses
            so changing one will change another
            This should be avoided
        for(int i =0; i < p.fur.length;i++){
            this.fur[i+1] = p.fur[i];
        }*/
    }

Upvotes: 1

Alex Cuadr&#243;n
Alex Cuadr&#243;n

Reputation: 688

Variable p is not an array (it's a class (House class)), so you can't call for the length of fur directly with p.length. Instead use p.fur.length; where you want to obtain the length of fur array. Because this way you're referring first to the class: p.fur and then accessing the array inside that class (fur).

Upvotes: 1

Related Questions