Reputation: 299
How can I make the object array named rooms accessible to the static method at the end called retrieveRoom(); I tryed public static Rooms rooms[] = new Rooms[3]. But I am just getting errors from that. Any help is appreciated.
public class MasterControlPanel{
public static void main(String[] args){
Rooms rooms[] = new Rooms[3];
rooms[0] = new Rooms("Room U", 1, 4, 4);
rooms[1] = new Rooms("Room U", 2, 4, 4);
rooms[2] = new Rooms("Connector X", 3, 2, 4);
rooms[3] = new Rooms("Connector U", 4, 2, 4);
for(int x = 0; x <= rooms.length; x++){
rooms[x].createLights();
rooms[x].createWalls();
}
}
public static Object retrieveRoom(int connectedRoom){
connectedRoom -= 1;
return rooms[connectedRoom];
}
}
Upvotes: 1
Views: 974
Reputation: 1302
Move
public static Rooms[] rooms = new Rooms[4];
out of your main()
method, to the declarations section like this
public class MasterControlPanel {
public static Rooms[] rooms = new Rooms[4];
public static void main(String[] args) {
...
}
}
This will define a rooms
static member in your MasterControlPanel
class.
Here are some other tips:
Class
exists. However, in order to access instance members (variables which belong to an instance of that class) you need an instance of the class. Rooms[] rooms = new Rooms[4];
. Notice that array brackets are moved to Type
part, this will prevent confusion in future. "Let's define An Array of Rooms with the name rooms". This is not required however a good thing to do.rooms
array with the size of 3 however you are adding 4 elements to it. This will cause ArrayIndexOutOfBounds
exception. private
to achieve Encapsulation. You can provide public
or protected
methods to retrieve those private
members from out of your class.n
elements, maximum number of consecutive iterations that you can make on that array is also n
that's for sure. That means in a for
loop make sure that your loop condition is n-1
for the last iteration. Remember, there is no 4
th element in a size 4
array.Here follows the 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 <= 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;
}
}
Upvotes: 3