Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Move </p> <pre><code>public static Rooms[] rooms = new Rooms[4]; </code></pre> <p>out of your <code>main()</code> method, to the declarations section like this</p> <pre><code>public class MasterControlPanel { public static Rooms[] rooms = new Rooms[4]; public static void main(String[] args) { ... } } </code></pre> <p>This will define a <code>rooms</code> <em>static member</em> in your <code>MasterControlPanel</code> class. </p> <p>Here are some other tips:</p> <ol> <li>Static members can be accessed as long as that <code>Class</code> exists. However, in order to access instance members (variables which belong to an <em>instance</em> of that class) you need an instance of the class. </li> <li>Use <code>Rooms[] rooms = new Rooms[4];</code>. Notice that array brackets are moved to <code>Type</code> part, this will prevent confusion in future. "<em>Let's define An Array of Rooms with the name rooms</em>". This is not required however a good thing to do.</li> <li>You are defining <code>rooms</code> array with the size of <strong>3</strong> however you are adding <strong>4</strong> elements to it. This will cause <code>ArrayIndexOutOfBounds</code> exception. </li> <li>You should make your members <code>private</code> to achieve <a href="http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29" rel="nofollow">Encapsulation</a>. You can provide <code>public</code> or <code>protected</code> methods to retrieve those <code>private</code> members from out of your class.</li> <li>When an Array contains <code>n</code> elements, maximum number of consecutive iterations that you can make on that array is also <code>n</code> that's for sure. That means in a <code>for</code> loop make sure that your loop condition is <code>n-1</code> for the last iteration. Remember, there is no <code>4</code>th element in a size <code>4</code> array.</li> </ol> <p>Here follows the code</p> <pre><code>public class MasterControlPanel { public static Room[] rooms = new Room[4]; public static void main(String[] args) { rooms[0] = new Room("Room U", 1, 4, 4); rooms[1] = new Room("Room U", 2, 4, 4); rooms[2] = new Room("Connector X", 3, 2, 4); rooms[3] = new Room("Connector U", 4, 2, 4); for (int x = 0; x &lt;= rooms.length-1; x++) { rooms[x].createLights(); rooms[x].createWalls(); } } public static Room retrieveRoom(int connectedRoom) { connectedRoom -= 1; return rooms[connectedRoom]; } } class Room { // These are instance members for Room class private String roomString; private int roomInteger1, roomInteger2, roomInteger3; public Room(String string, int integer1, int integer2, int integer3) { // This is the constructor for Room object this.roomString = string; this.roomInteger1 = integer1; this.roomInteger2 = integer2; this.roomInteger3 = integer3; } public void createLights() { // This method creates lights } public void createWalls() { // This method creates walls } // These are Getters and Setters for Room members public String getRoomString() { return roomString; } public void setRoomString(String roomString) { this.roomString = roomString; } public int getRoomInteger1() { return roomInteger1; } public void setRoomInteger1(int roomInteger1) { this.roomInteger1 = roomInteger1; } public int getRoomInteger2() { return roomInteger2; } public void setRoomInteger2(int roomInteger2) { this.roomInteger2 = roomInteger2; } public int getRoomInteger3() { return roomInteger3; } public void setRoomInteger3(int roomInteger3) { this.roomInteger3 = roomInteger3; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload