Results 1 to 5 of 5

Thread: public method

  1. #1
    dirtycash is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default public method

    how would i go about answering the following question -


    1. findVacantRoom has no parameters and returns an integer value representing the room
    number of a vacant room. Remember that the getOccupier method of a Room will return
    null when a room is vacant. Room zero will be occupied first, then room 1, then room
    2 and so on. This method will return the first vacant room, so if rooms 0, 1, 2 and 4 are
    occupied, findVacantRoom will return 3 which is the number of the first vacant room.
    The method must return -1 if the hotel is full and there are no vacant rooms.

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    I'm making a few assumptions before doing this. They are:
    1. The rooms are stored in an array.
    2. The rooms are stored as Room objects.
    3. getOccupier returns a String


    Java Code:
    public int findVacantRoom(){
    [INDENT]
    for(int i = 0; i < rooms.length; i++){
    [INDENT]
    if (rooms[i].getOccupier() == null){
        return i;
    }[/INDENT]
    }[/INDENT]
    
    return -1;
    }
    Essentially what this does is checks all of the rooms in the room array starting at zero. When a vacant room is encountered, the array index of that room is returned. (You might have to return something like rooms[i].roomNumber if the room numbers do not correspond to the array index.) If the for loop completes without finding a vacant room, -1 is returned.

    Again, if my assumptions aren't true than this might not work. Regardless of how the data is stored, the idea is the same though.

    Good luck.

  3. #3
    dirtycash is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    it says it cannot find variable length?


    :)
    thanks for the reply
    Last edited by dirtycash; 11-21-2007 at 06:18 PM.

  4. #4
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    That was written assuming that the rooms were going to be stored in an array. If they are stored another way, (vector, ArrayList...) you will have to modify the code.

  5. #5
    dirtycash is offline Member
    Join Date
    Nov 2007
    Posts
    38
    Rep Power
    0

    Default

    ive fixed it now thanks for your help :)
    Last edited by dirtycash; 11-21-2007 at 08:18 PM.

Similar Threads

  1. Class is public, should be declared in a file
    By goldhouse in forum New To Java
    Replies: 7
    Last Post: 05-10-2012, 09:17 AM
  2. why we use public access modifier?
    By vichet in forum New To Java
    Replies: 1
    Last Post: 04-04-2008, 07:04 AM
  3. public declaration ('undeclaring')
    By Jadellll in forum New To Java
    Replies: 2
    Last Post: 03-17-2008, 08:50 PM
  4. Public Holidays (Australia)
    By oranjeBoven in forum Advanced Java
    Replies: 0
    Last Post: 02-13-2008, 08:14 AM
  5. Public class variable
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •