0

I am very new to Java and I was wondering if it is possible to assign an object to another object. For example, supposing I have an object called Classroom. In this classroom, I have got chairs, tables, board etc. Chairs, tables and the board are objects themselves with their own separate attributes such as number of chair legs, table legs, width of the board etc. So basically, I have 4 classes. Now I want to assign the chair, table, board to be attributes of the ClassRoom. I am not sure how to go about this.

Any help would be appreciated, Thanks in advance Ester

5 Answers 5

1

You need to make Classroom a "composite" class, one that holds fields that refer to the other classes that you've mentioned. For instance, Classroom could hold an ArrayList<Furnature> (or known as an ArrayList of Furnature), and your Chair, Table, and similar classes could extend from an abstract Furnature class. Then you could give Classroom methods that allow you to add and remove Furnature items from the ArrayList.

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

1 Comment

Oh, and give my best to uncle Mordecai when you see him.
1

You have a has-many relation between classroom and table (for example). The basic design would look like this:

public class Classroom {
  List<Table> tables = new ArrayList<Table>();

  // you may want to add a table to the classroom
  public void addTable(Table table) {
    tables.add(table);
  }

  // you may want to remove one
  public void removeTable(Table table) {
    tables.remove(table);
  }

  // here we can replace (a broken) one
  public void replaceTable(Table oldTable, Table newTable) {
    tables.set(tables.indexOf(oldTable), newTable);
  }

  // and: the inventory
  public List<Table> getTables() {
    return tables;
  }
}

Comments

1

Edit: for adding objects

public final class Classroom {
  public Classroom() {
     this.tables = new ArrayList<Table>;
     this.chairs = new ArrayList<Chair>;
     this.boards = new ArrayList<Board>;
  }

  public void addTable(final Table table) {
    this.tables.add(table);
  }

  public void addChair(final Chair chair) {
    this.chairs.add(chair);
  }

  public void addBoard(final Board board) {
    this.boards.add(board);
  }


  private final List<Table> tables;
  private final List<Chair> chairs;
  private final List<Board> boards;
}

and outside, from main for example

Table table = new Table(param1, param2);
Table anotherTable = new Table(param1, param2);
Chair chair = new Chair(param1, param2);
Board board = new Board(param1, param2);

now make your classroom:

Classroom classroom = new Classroom();

// adding object
classrooom.addTable(table);
classroom.addChair(chair);
classroom.addTable(anotherTable);

// and so on...

1 Comment

Realworld classrooms usually have more then one table or chair.
0

I'm not a big java guy but I suspect it is similar to doing this in C++... As long as the classes are defined you should be able to declare them as properties of another object just like you would with something such as a String.
Just make sure each object is defined and it's supporting methods are defined within that class.
This may not be exactly syntactically correct, but it should look something like this:

class classRoom {
    Chair chair;
    Table table;
    Board board;
    String name;

    add_chair();
    add_table();
    remove_table();

}

class Chair {
    Leg leg1;
    Leg leg2;
}

class Leg {
    int height;
    set_height();
    get_height();
}

class Board {
    int width;
    int height;
}

Now if you want to access the classRoom chair you just do something like this:

classRoom room = new classRoom();
height = room.chair.leg.get_height();

Although note that this is bad practice, you should set up functions to get such values, instead of directly accessing them.

2 Comments

This won't compile because the methods don't have bodies and return types - and a classroom usually has more then one table or chair
Yes I realize that and I quote "This may not be exactly syntactically correct, but it should look something like this:" I figured pseudo code is more useful in this instance
0

I guess an easy way is to go with lists

class ClassRoom {
    private List<Chair> chairs = new ArrayList<Chair>();
    private List<Table> tables = new ArrayList<Chair>();
    ...

    void addChair(Chair chair) {
        chairs.add(chair);
    }

    List<Chair> getChairs() {
    ....

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.