Classes:
- chair object
package Array; public class Chair { String color; public Chair(String color) { this.color = color; } }
- table object
package Array; public class Table { String color; public Table(String color) { this.color = color; } }
- chairCollection object
package Array; public class ChairCollection { Chair[] collection; int i; public ChairCollection() { collection = new Chair[3]; i = 0; } void add (Chair newChair) { collection[i] = newChair; i++; } String getColor(int index) { return collection[index].color; } }
- tableCollection object
package Array; public class TableCollection { Table[] collection; int i; public TableCollection() { collection = new Table[2]; i = 0; } void add (Table newTable) { collection[i] = newTable; i++; } String getColor(int index) { return collection[index].color; } }
- main class: array
package Array; public class Array { void start() { Chair blueChair = new Chair("blue"); Chair redChair = new Chair("red"); Chair yellowChair = new Chair("yellow"); Table blueTable = new Table("blue"); Table blackTable = new Table("black"); ChairCollection chairCollection = new ChairCollection(); TableCollection tableCollection = new TableCollection(); chairCollection.add(blueChair); chairCollection.add(redChair); chairCollection.add(yellowChair); tableCollection.add(blueTable); tableCollection.add(blackTable); System.out.println("The color of this chair is: " + chairCollection.getColor(2)); } public static void main(String[] args) { new Array().start(); } }