Results 1 to 9 of 9
Thread: Array of object initialization
- 05-03-2012, 03:39 AM #1
Member
- Join Date
- May 2012
- Posts
- 5
- Rep Power
- 0
Array of object initialization
Getting errors with the following code:
/** Creates Hotel Class */
public class Hotel
{
/** Creates instance variables */
private String name;
private String location;
private int occupiedCnt;
private boolean Full;
/** Default constructor for Hotel */
public Hotel()
{
Room[] theRooms = new Room[5];
}
/** Paramterized Constructor */
public Hotel(String name, String location)
{
this.name = name;
this.location = location;
}
public String addRoom(int roomNum, String bedType, char smoking, double rate)
{
Room[] theRooms = new Room [5];
Room[0]= new Room (101, "Queen", 'n', 100);
Room[1]= new Room (102, "King", 'n', 110);
Room[2]= new Room (103, "King", 's', 110);
Room[3]= new Room (104, "twin", 'n', 90);
Room[4]= new Room (105, "Queen", 'n', 100);
}
The error is :
Hotel.java:24: error: cannot find symbol
Room[] theRooms = new Room[5];
^
PLease help
- 05-03-2012, 03:48 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
Re: Array of object initialization
Where is the class Room defined? Please post the code.
- 05-03-2012, 04:15 AM #3
Member
- Join Date
- May 2012
- Posts
- 5
- Rep Power
- 0
Re: Array of object initialization
The Room class is a separate class that is called by the Hotel Class. The project is to create a Hotel that has an array of Rooms created by the Room class. THe addRoom method assigns the parameters for each Room. The code I have looks like this:
/** Creates room class */
public class Room
{
private int roomNum;
private String bedType;
private double rate;
private String occupantName;
private char smoking;
private boolean occupied;
public Room()
{
roomNum = 0;
bedType = " ";
rate = 0;
occupantName = "";
smoking = 0;
occupied = false;
}
public Room(int roomNum, String bedType, char smoking, double rate)
{
this.roomNum = roomNum;
this.bedType = bedType;
this.smoking = smoking;
this.rate = rate;
}
public String getBedType()
{
return bedType;
}
public char getSmoking()
{
return smoking;
}
public int getRoomNum()
{
return roomNum;
}
public double getRoomRate()
{
return rate;
}
public String setOccupant()
{
return occupantName;
}
public boolean setOccupied()
{
int len = occupantName.length();
if (len > 1){
occupied = true;
}
else if(len <1){
occupied = false;
}
return occupied;
}
public boolean isOcuppied()
{
boolean getRoomState = occupied;
return getRoomState; }
public String toString(){
return "Room Number: " + roomNum +"Name of Guest: " + occupantName +"Bed Type: " + bedType + "Smoking Room: " + smoking + "Rate: " + rate;
}
}
- 05-03-2012, 04:33 AM #4
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Array of object initialization
The code should be:
Java Code:Room[] theRooms = new Room [5]; theRooms[0] = new Room (101, "Queen", 'n', 100); ... ... ...
Website: Learn Java by Examples
- 05-03-2012, 05:04 AM #5
Member
- Join Date
- May 2012
- Posts
- 5
- Rep Power
- 0
Re: Array of object initialization
Tried the suggestion but, it didn't solve the problem. Still getting error message:
Hotel.java:24: error: cannot find symbol
Room[] theRooms = new Room[5];
^
symbol: class Room
location: class Hotel
Any other suggestions?
- 05-03-2012, 05:15 AM #6
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Array of object initialization
Do you have the Hotel.java and the Room.java in the same path? You are compiling using a javac command line or using an IDE?
Website: Learn Java by Examples
- 05-03-2012, 05:22 AM #7
Member
- Join Date
- May 2012
- Posts
- 5
- Rep Power
- 0
Re: Array of object initialization
Using an IDE called jGRASP. Any suggestions? It seems that Hotel doesn't know that Room exists.
- 05-03-2012, 05:36 AM #8
Member
- Join Date
- May 2012
- Posts
- 5
- Rep Power
- 0
Re: Array of object initialization
Thanks for all the help. I relooked at how I used your suggestion and I got it to work.
- 05-03-2012, 05:49 AM #9
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Array of object initialization
So, what was that causing the problem? And how did you solve it?
Website: Learn Java by Examples
Similar Threads
-
Int Array Initialization Problem
By dragstang86 in forum New To JavaReplies: 5Last Post: 03-28-2012, 09:54 PM -
An Array of Objects (Class Initialization)
By Chris_C in forum New To JavaReplies: 9Last Post: 02-09-2011, 05:49 AM -
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Array initialization difference
By sln69 in forum Advanced JavaReplies: 2Last Post: 10-10-2008, 02:38 AM -
2D Array Initialization
By M77 in forum Advanced JavaReplies: 3Last Post: 06-04-2008, 02:12 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks