By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,637 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,637 IT Pros & Developers. It's quick & easy.

Having some trouble with arrays for my hotel class

P: 19
First of all I'm new to this site but it certainly looks like a place that i will be visiting more often!

Onto my problem.
I am creating a Hotel Bussiness project in java using BlueJ

The classes are as follows:
Hotel - Deals with the major functions of the hotel (i.e guests booking in, finding rooms etc.)
Room - Used to decide whether a room has a sea view and stores the guest that is in the room.
Occupier - For information about the group occupying a room with methods for amount of nights stayed and nights eaten dinner.


I have completed the Room and Occupier classes with no trouble at all.
My downfall is creating the hotel class.
The class has 3 fields so far they are
Expand|Select|Wrap|Line Numbers
  1.     private Room[] Rooms;
  2.     private int Profit;
  3.     private int FreeRooms;
  4.  
The constructor is as follows:
Expand|Select|Wrap|Line Numbers
  1.  public Hotel(int totalRooms)
  2.     {
  3.         Profit = 0;
  4.         FreeRooms = totalRooms;
  5.         setRoom();
  6.     }
  7.  
And here is the problem, the setRoom is a private method used to declare each entry in the array as a new room object. Even numbers having a sea view. The part labelled as SOMETHING is where i wish to step into the array and set each room object as sea view or no seaview. The problem is i just don't know how to put this into code. Here is my attempt althogh after much brain ache i am starting to think using a while or for loop would work?

Expand|Select|Wrap|Line Numbers
  1.     private void setRoom()
  2.     {
  3.         Rooms = new Room[FreeRooms];
  4.         if (Rooms.SOMETHING % 2 = 0){
  5.             Room.hasSeaView = true;
  6.         }
  7.         else Room.hasSeaView = false;
  8.     }
  9.  
Also note that the hasSeaView method has already been set in the Room class.
I have reffered to all of my work books and scoured the interweb but just can't find a decent example to see what to put here.

Thanks in advance.
Nov 19 '07 #1
Share this Question
Share on Google+
24 Replies


Expert 10K+
P: 11,448
Also note that the hasSeaView method has already been set in the Room class.
I have reffered to all of my work books and scoured the interweb but just can't find a decent example to see what to put here.

Thanks in advance.
Note that a Room doesn't know where it is stored in your hotel array; you have
to help it a bit w.r.t. having a sea view or not. Suppose a Room class has the
following two methods (a getter and a setter for a boolean variable)

Expand|Select|Wrap|Line Numbers
  1. =java]
  2. boolean seaView;
  3. ...
  4. public void setSeaView(boolean seaView) { this.seaView= seaView; }
  5. public boolean isSeaView() { return seaView; }
  6.  
We could argue whether or not 'hasSeaView' would be a better name for the
second method but lets conform to Sun's naming convention for now.

In the hotel you want to set the even rooms to have a sea view; the odd numbered
rooms are located at the other side of the hotel. When you 'build' your hotel you
have to do something like this in your Hotel class:

1) walk over the 'rooms' array
2) set the even numbered rooms to 'true' using the method above

Alternatively you could remove the setter method in the Room class and pass
a boolean 'seaView' to its constructor. When you build the rooms in the array
pass the appropriate boolean value to the constructor when you 'new' a Room.

kind regards,

Jos
Nov 19 '07 #2

P: 19
Thanks for the help so far but the part I'm really stuck on is telling the Rooms in the array where abouts in the array they are so they can decide whether they have a seaview or not. But since each entry in the array is declared as a room object i can't directly number them.
Nov 19 '07 #3

Expert 10K+
P: 11,448
Thanks for the help so far but the part I'm really stuck on is telling the Rooms in the array where abouts in the array they are so they can decide whether they have a seaview or not. But since each entry in the array is declared as a room object i can't directly number them.
Ok, second try; lets explain the alternative way (see my previous reply).
Here's the relevant part of a Room class:

Expand|Select|Wrap|Line Numbers
  1. public class Room {
  2.    private boolean seaView; // set at construction time
  3.  
  4.    public Room(boolean seaView) {
  5.       this.seaView= seaView;
  6.       ...
  7.    }
  8.    public boolean isSeaView() { return seaView; }
  9. }
  10.  
A Room constructor needs to know whether or not the Room has a sea view.
You can ask a room whether or not it has a sea view; you can't change it anymore.

In your Hotel class, when you build it, do this:

Expand|Select|Wrap|Line Numbers
  1. public class Hotel {
  2.    private Room[] rooms; // the rooms in the hotel
  3.    public Hotel() {
  4.       // first construct the rooms array and construct the rooms as follows:
  5.       for (int i= 0; i < rooms.length; i++)
  6.          rooms[i]= new Room(i%2 == 0); // sea view or not
  7.    }
  8.    ...
  9. }
kind regards,

Jos
Nov 19 '07 #4

P: 19
Cheers for clearing that up. I have it now and it works.
Thanks for your help Jos!
Nov 19 '07 #5

Expert 10K+
P: 11,448
Cheers for clearing that up. I have it now and it works.
Thanks for your help Jos!
You're welcome of course.

kind regards,

Jos
Nov 19 '07 #6

P: 19
Sorry to constantly ask for help but I've hit another wall.

I'm trying to create a method that will find the first vacant room in the hotel (i.e the first entry in the array that has the value null)

Here is my code
Expand|Select|Wrap|Line Numbers
  1.     public int findVacantRoom()
  2.     {
  3.     int index = 0;
  4.     while(index < Rooms.length) {
  5.         if (Room.getOccupier() = null) {
  6.             return index; }
  7.         if (Room.getOccupier() = !null) { 
  8.             index ++; }
  9.         }
  10.         return -1;
  11.     }
  12.  
The return -1 is for when the hotel is full.

And here is the (simple) code for the getOccupier method in the Room class
Expand|Select|Wrap|Line Numbers
  1.     public Occupier getOccupier()
  2.     {
  3.         return guest;
  4.     }
  5.  
When i try to compile this i get the error message:
non-static method getOccupier() cannot be referenced to a static context.

I have no idea what I'm doing wrong as I have done this before in past projects and had no difficulty what so ever.

Thanks again.
Nov 19 '07 #7

10K+
P: 13,264
Sorry to constantly ask for help but I've hit another wall.

I'm trying to create a method that will find the first vacant room in the hotel (i.e the first entry in the array that has the value null)

Here is my code
Expand|Select|Wrap|Line Numbers
  1.     public int findVacantRoom()
  2.     {
  3.     int index = 0;
  4.     while(index < Rooms.length) {
  5.         if (Room.getOccupier() = null) {
  6.             return index; }
  7.         if (Room.getOccupier() = !null) { 
  8.             index ++; }
  9.         }
  10.         return -1;
  11.     }
  12.  
The return -1 is for when the hotel is full.

And here is the (simple) code for the getOccupier method in the Room class
Expand|Select|Wrap|Line Numbers
  1.     public Occupier getOccupier()
  2.     {
  3.         return guest;
  4.     }
  5.  
When i try to compile this i get the error message:
non-static method getOccupier() cannot be referenced to a static context.

I have no idea what I'm doing wrong as I have done this before in past projects and had no difficulty what so ever.

Thanks again.
You have to be refering to a particular room don't you?
You have to go through the array and get the Room objects first, then you can call the getOccupier method on a room.
Nov 19 '07 #8

P: 19
Oh i see - so the problem is that I'm not referencing index to a room object in the array then?
Nov 19 '07 #9

10K+
P: 13,264
Oh i see - so the problem is that I'm not referencing index to a room object in the array then?
No you are not.
Nov 19 '07 #10

P: 19
No you are not.
Alright thanks for pointing that out - I've got 7 or 8 more methods lined up for this bad boy and I'm bound to get stuck again!
All good practice for next weeks assessment though!

Thank you guys
Nov 19 '07 #11

P: 19
Sorry to pester you guys again but I'm still stuck on this findVacantRoom method. I just can't figure out how to reference the index variable to the room number.

Here is my attempted code
Expand|Select|Wrap|Line Numbers
  1.     public int findVacantRoom()
  2.     {
  3.         for (int index = 0; index < Rooms.length; index++)
  4.         if (Room.getOccupier() = null){
  5.             return index;
  6.         }
  7.     }
  8.  
Nov 20 '07 #12

10K+
P: 13,264
Sorry to pester you guys again but I'm still stuck on this findVacantRoom method. I just can't figure out how to reference the index variable to the room number.

Here is my attempted code
Expand|Select|Wrap|Line Numbers
  1.     public int findVacantRoom()
  2.     {
  3.         for (int index = 0; index < Rooms.length; index++)
  4.         if (Room.getOccupier() = null){
  5.             return index;
  6.         }
  7.     }
  8.  
If Rooms is an array of room objects, then
Rooms[0] refers to the first room object in the array (if it's not null)
and Rooms[1] refers to the second room object .... and Room[i] refers to the (i+1)th room in the array.
Nov 20 '07 #13

P: 19
okay - thankyou very much!
Nov 20 '07 #14

P: 19
Back again!

I've come to another problem when trying to compute the bill.

First of all here is my code:
Expand|Select|Wrap|Line Numbers
  1.     public int getBill(int roomNumber)
  2.    {
  3.         int total = 0;
  4.         int nightsStayed = Occupier.getLengthOfStay();
  5.         int numberPpl = Occupier.getNumInGroup();
  6.         if (Rooms[roomNumber].hasSeaView()= true){
  7.             total = nightsStayed * SEAVIEW_SUPPLEMENT;
  8.         }
  9.  
  10.         if(numberPpl = 1){
  11.             total += nightsStayed * SINGLE_OCCUPIER_SUPPLEMENT;
  12.         }
  13.         total += nightsStayed * numberPpl;
  14.         return total;
  15.     }
  16.  
The problem i have is not with the algorithm but an error i am getting on line 4.
It's the static/ non-static error again! I know why i am getting this - it's because I'm am not reffering to a specific occupier.

I just don't know how to refer to a specific occupier within the array. If you need any more background info or more parts of code to help me with this just say. After this method every method i have lined up is just rounding up the project.

Thanks for all of your help guys it's greatly appreciated. I already understand alot more about array objects after being on this site for 24 hours than i have from weeks of lectures.
Nov 20 '07 #15

P: 19
No worries now guys - i had a brain wave!
I amde a new variable of type Occupier and used that to access the Occupier class!
Thanks anyway
Nov 20 '07 #16

P: 19
Right guys i have compiled all of my classes - my next problem comes with testing one of the methods! If i wasn't such a perfectionist i could let this go but i want this to work!

I have a method called checkIn. After creating a hotel object and some occupier objects you use the method to check occupiers into the hotel.

The method has two parameters. group is to declare which occupier is checking in. Preference tells the hotel if the guest wants a seaview or not.

Upon running the method here is what should happen:
If the occupier wants a seaview they will be assigned to the first room available with a seaview.
If all of the seaview rooms are taken they will be put in the first room available without a seaview.
The method then returns the roomnumber the occupier was set to.
If no vacant rooms were found the method returns -1.

Now onto my code. here is the code for finding a vacant room.
Expand|Select|Wrap|Line Numbers
  1.     public int findVacantRoom()
  2.     {
  3.         for (int index = 0; index < Rooms.length; index++)
  4.         if (Rooms[index].getOccupier() == null){
  5.             return index;
  6.         }
  7.         int indx = -1;
  8.         return indx;
  9.     }
  10.  
Here is the code for finding a room with a seaview (only even rooms have a seaview hence the index +=2
Expand|Select|Wrap|Line Numbers
  1.     public int findVacantSeaView()
  2.     {
  3.         for (int index = 0; index < Rooms.length; index+=2)
  4.         if (Rooms[index].getOccupier() == null){
  5.             return index;
  6.         }
  7.         int indx = -1;
  8.         return indx;
  9.     }
  10.  
And here is the checkin method
Expand|Select|Wrap|Line Numbers
  1.     public int checkIn(Occupier group, boolean preference)
  2.     {
  3.         int rm = findVacantSeaView();
  4.         int rm2 = findVacantRoom();
  5.         int rm3 = findVacantRoom();
  6.         int rm4 = -1;
  7.  
  8.         if (preference == true){
  9.             Rooms[rm].setOccupier(group);
  10.             FreeRooms -=1
  11.             return rm;            
  12.         }
  13.  
  14.         else if (findVacantSeaView() == -1){
  15.            Rooms[rm2].setOccupier(group);
  16.            FreeRooms -=1
  17.            return rm2;
  18.         }
  19.  
  20.         else if (preference == false){
  21.            Rooms[rm3].setOccupier(group);
  22.            FreeRooms -=1
  23.            return rm3;
  24.         }
  25.         else {return rm4;}               
  26.     }
  27.  
The problem occurs when the hotel is full or the guest wants a seaview but has to be assigned a vacant room without a seaview instead. The program is supposed to return -1 but instead crashes and brings up the error:
java.lang.ArrayIndexOutOfBoundsException: -1

Any help with this will be greatly appreciated!
Nov 20 '07 #17

10K+
P: 13,264
It's your findVacantSeaView method that's stealing the cookies.

That index can go over the size of the array thus throwing the outofbounds thing at you.
Shouldn't a room have a isSeaView property which you can check in your findVacantSeaView method so that your loop simply has something like
Expand|Select|Wrap|Line Numbers
  1. if((Rooms[i].isSeaView()) && (Rooms[i].getOccupier() == null)) {
  2. //found it
  3. }
Nov 20 '07 #18

P: 19
I inserted your code into my method and I still get the same error.
I should have mentioned that the findVacantRoom and findVacantSeaview methods both work when i run them and they even return -1 if there are no rooms available.
I think it's something to do with the checkIn method
When the error occurs line:
Expand|Select|Wrap|Line Numbers
  1. Rooms[rm].setOccupier(group);
  2.  
is highlighted in the editor.

EDIT - is it possibly becasue the program is trying to find room -1?
If so how would I edit it so that it isnt?

Thanks
Nov 20 '07 #19

10K+
P: 13,264
I inserted your code into my method and I still get the same error.
I should have mentioned that the findVacantRoom and findVacantSeaview methods both work when i run them and they even return -1 if there are no rooms available.
I think it's something to do with the checkIn method
When the error occurs line:
Expand|Select|Wrap|Line Numbers
  1. Rooms[rm].setOccupier(group);
  2.  
is highlighted in the editor.

EDIT - is it possibly becasue the program is trying to find room -1?
If so how would I edit it so that it isnt?

Thanks
Where did you plug-in my code? It was not meant as a plug-in.
You have to have the boolean isSeaVies property in your Room class and the getter for it for that code to work with your code.
Nov 20 '07 #20

P: 19
Yes i should have made that clear.

The room class has the method hasSeaView. The value of seaView is determined on construction of the hotel in a private method in the constructor which makes all even rooms have a seaview.
I put your code into my findVacantSeaview method put used my method "hasSeaView" instead of your "isSeaView" but still got the same error.
Nov 20 '07 #21

10K+
P: 13,264
Yes i should have made that clear.

The room class has the method hasSeaView. The value of seaView is determined on construction of the hotel in a private method in the constructor which makes all even rooms have a seaview.
I put your code into my findVacantSeaview method put used my method "hasSeaView" instead of your "isSeaView" but still got the same error.
Erm, which same error? Can you post the code you used and the exact error that you got?
Nov 20 '07 #22

P: 19
Erm, which same error? Can you post the code you used and the exact error that you got?
My code for the hotel class is:
Expand|Select|Wrap|Line Numbers
  1. public class Hotel {
  2.     public static final int ROOM_PRICE = 20;
  3.     public static final int SEAVIEW_SUPPLEMENT = 5;
  4.     public static final int SINGLE_OCCUPIER_SUPPLEMENT = 5;
  5.     public static final int DINNER_PRICE = 9;
  6.     private Room[] Rooms;
  7.     private int Profit;
  8.     private int FreeRooms;
  9.  
  10.     public Hotel(int totalRooms)
  11.     {
  12.         Profit = 0;
  13.         FreeRooms = totalRooms;
  14.         setRoom();
  15.  
  16.     }
  17.  
  18.     private void setRoom()
  19.     {
  20.         Rooms = new Room[FreeRooms];
  21.         for (int i = 0; i < Rooms.length; i++)
  22.         Rooms[i] = new Room(i%2 == 0);
  23.     }
  24.  
  25.     public int findVacantRoom()
  26.     {
  27.         for (int index = 0; index < Rooms.length; index++)
  28.         if (Rooms[index].getOccupier() == null){
  29.             return index;
  30.         }
  31.         return -1;
  32.     }
  33.  
  34.     public int findVacantSeaView()
  35.     {
  36.         for (int index = 0; index < Rooms.length; index++)
  37.         if((Rooms[index].hasSeaView()) && (Rooms[index].getOccupier() == null)){
  38.             return index;
  39.         }
  40.         return -1;
  41.     }
  42.  
  43.     public int checkIn(Occupier group, boolean preference)
  44.     {
  45.         int rm = findVacantSeaView();
  46.         int rm2 = findVacantRoom();
  47.         int rm3 = findVacantRoom();
  48.  
  49.         if (preference == true){
  50.         Rooms[rm].setOccupier(group);
  51.         FreeRooms -=1;
  52.         return rm;
  53.     }
  54.  
  55.     else if (findVacantSeaView() == -1){
  56.         Rooms[rm2].setOccupier(group);
  57.         FreeRooms -=1;
  58.         return rm2;
  59.     }
  60.  
  61.     else if (preference == false){
  62.         Rooms[rm3].setOccupier(group);
  63.         FreeRooms -=1;
  64.         return rm3;
  65.     }
  66.     return -1;
  67.     }
  68.  
  69.     public int getBill(int roomNumber)
  70.     {
  71.         int total = 0;
  72.         Occupier occupant = Rooms[roomNumber].getOccupier();
  73.         int nightsStayed = occupant.getLengthOfStay();
  74.         int numberPpl = occupant.getNumInGroup();
  75.         if (Rooms[roomNumber].hasSeaView()== true){
  76.             total += nightsStayed * SEAVIEW_SUPPLEMENT;
  77.         }
  78.  
  79.         if(numberPpl == 1){
  80.             total += nightsStayed * SINGLE_OCCUPIER_SUPPLEMENT;
  81.         }
  82.  
  83.         total += nightsStayed * numberPpl;
  84.         total += occupant.getNumDinners() * DINNER_PRICE;
  85.         return total;
  86.     }
  87.  
  88.     public void checkOut(int roomNumber)
  89.     {
  90.         Profit += getBill(roomNumber);
  91.         Rooms[roomNumber].setOccupier(null);
  92.     }
  93.  
  94.     public void updateOvernight()
  95.     {
  96.         int index = 0;
  97.         Occupier occupant = Rooms[index].getOccupier();
  98.         while (index < Rooms.length){
  99.             occupant.stayNight();
  100.         }
  101.     }
  102.  
  103.     public void eatDinner(int roomNumber)
  104.     {
  105.         Occupier occupant = Rooms[roomNumber].getOccupier();
  106.         int dinners = occupant.getNumDinners();
  107.         int numberInGroup = occupant.getNumInGroup();
  108.         dinners += numberInGroup;
  109.         dinners = occupant.getNumDinners();
  110.     }
  111.  
  112.     public int getTotalIncome()
  113.     {
  114.         return Profit;
  115.     }
  116.  
  117.     public int getNumFreeRooms()
  118.     {
  119.         return FreeRooms;
  120.     }
  121.  
  122. }
  123.  
  124.  
And the room class:
Expand|Select|Wrap|Line Numbers
  1. public class Room
  2. {
  3.  
  4.     private boolean seaview;
  5.     private Occupier guest;
  6.  
  7.     /**
  8.      * Constructor for objects of class Room
  9.      */
  10.  
  11.     public Room(boolean seaView)
  12.     {
  13.         seaview = seaView;
  14.         guest = null;
  15.     }
  16.  
  17.     /**
  18.      * Set whether the room has a sea view.
  19.      */
  20.  
  21.     public boolean hasSeaView()
  22.     {
  23.         return seaview;
  24.     }
  25.  
  26.     /**
  27.      * Set the Occupier of the room
  28.      */
  29.  
  30.     public void setOccupier(Occupier newOccupier)
  31.     {
  32.         guest = newOccupier;
  33.     }
  34.  
  35.     /**
  36.      * Return the Occupier
  37.      */
  38.  
  39.     public Occupier getOccupier()
  40.     {
  41.         return guest;
  42.     }
  43. }
  44.  
And the occupier class:
Expand|Select|Wrap|Line Numbers
  1. public class Occupier
  2. {
  3.  
  4.     private int numberInGroup;
  5.     private int nightsStayed;
  6.     private int nightsEaten;
  7.  
  8.     /**
  9.      * Constructor for objects of class Occupier
  10.      */
  11.  
  12.     public Occupier(int groupNumber)
  13.     {
  14.         numberInGroup = groupNumber;
  15.     }
  16.  
  17.     /**
  18.      * Returns the number of nights the group has stayed at the hotel 
  19.      */
  20.  
  21.     public int getLengthOfStay()
  22.     {
  23.         return nightsStayed;
  24.     }
  25.  
  26.     /**
  27.      * Returns the number of people in the group
  28.      */
  29.  
  30.     public int getNumInGroup()
  31.     {
  32.         return numberInGroup;
  33.     }
  34.  
  35.     /**
  36.      * Return the number of dinners the group has eaten
  37.      */
  38.  
  39.     public int getNumDinners()
  40.     {
  41.         return nightsEaten;
  42.     }
  43.  
  44.     /**
  45.      * Add one to the number of nights the group has stayed
  46.      */
  47.  
  48.     public void stayNight()
  49.     {
  50.         nightsStayed += 1;
  51.     }
  52.  
  53.     /**
  54.      * Add one to the number of times the group has eaten dinner
  55.      */
  56.  
  57.     public void eatDinner()
  58.     {
  59.         nightsEaten += 1;
  60.     }
  61. }
  62.  
The code does compile. When every room in the hotel has an occupier the program should return -1. But instead i get the following error:
java.lang.ArrayIndexOutOfBoundsException: -1

Line 50 of the hotel class is highlighted when this error occurs. As i said before the findVacantRoom and findVacantSeaView methods work individually so the error is in the checkIn method.

Thanks and sorry about being unclear with anything before.
Nov 20 '07 #23

P: 19
Sorry if you started working on this already but i figured it out my self.
I modified the checkIn method to take into account when the hotel is full as follows
Expand|Select|Wrap|Line Numbers
  1.     public int checkIn(Occupier group, boolean preference)
  2.     {
  3.         int rm = findVacantSeaView();
  4.         int rm2 = findVacantRoom();
  5.         int rm3 = findVacantRoom();
  6.  
  7.         if ((preference == true)&&(FreeRooms > 0)){   // The guest wants a sea view.
  8.         Rooms[rm].setOccupier(group); // Find a room with a sea view.
  9.         FreeRooms -=1; // Subtract one from the number of free rooms in the hotel
  10.         return rm; // return the room number
  11.     }
  12.  
  13.     else if ((preference == true) && (findVacantSeaView() == -1) && (FreeRooms > 0)){
  14.         Rooms[rm2].setOccupier(group);
  15.         FreeRooms -=1;
  16.         return rm2;
  17.     }
  18.  
  19.     else if ((preference == false) && (FreeRooms > 0)){
  20.         Rooms[rm3].setOccupier(group);
  21.         FreeRooms -=1;
  22.         return rm3;
  23.     }
  24.  
  25.     else if ((preference == true) && (FreeRooms == 0)){
  26.         return -1;
  27.     }
  28.  
  29.     else if (FreeRooms == 0){
  30.         return -1;
  31.     }
  32.  
  33.     return -1;
  34.     }
  35.  
Thanks for your guidance.
Nov 20 '07 #24

10K+
P: 13,264
In future the exception trace is usually a give-away. It usually has the exact line number where the exception was thrown at.
Nov 21 '07 #25

Post your reply

Sign in to post your reply or Sign up for a free account.