Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   public method (http://www.programmingforums.org/showthread.php?t=14545)

dirtycash Nov 21st, 2007 8:16 AM

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.


is it asking for a conditional statement?

DaWei Nov 21st, 2007 8:23 AM

Re: public method
 
Why don't you consider this as a problem-solving exercise, and not as a programming exercise. That way you can design your solution, then implement it.

It's pretty clear. You have a list of rooms. These rooms are given from the beginning (lowest numbered room first). They may be vacated in any order.

Each time you need a vacant room, you begin with the first room and go up the list until you find a vacant room.

Obviously, there are conditionals involved. IF a room is not vacant, you can't give it out. If the hotel is full, you can't give out a room. Isn't that easy, if you just think about it and don't get dranoed because it's a "programming" assignment?

Hint: when it comes to the code, think array or list.

null_ptr0 Nov 21st, 2007 8:43 PM

Re: public method
 
Here's something I scratched up:
:

import java.util.List;

class Occupant {
        public Occupant(String name, int age) {
                setName(name);
                setAge(age);
        }

        public void setName(String name) {
                this.name = name;
        }

        public String getName() {
                return name;
        }

        public void setAge(int age) {
                this.age = age;
        }

        public int getAge() {
                return age;
        }

        private int age;
        private String name;
}

class Room {
        public Room(int roomNo) {
                this(null, roomNo;
        }

        public Room(Occupant o, int roomNo) {
                setOccupant(o);
                setRoomNo(roomNo);
        }

        public void setOccupant(Occupant o) {
                this.o = o;
        }

        public Occupant getOccupant() {
                return o;
        }

        private void setRoomNo(int roomNo) {
                ROOM_NO = roomNo;
        }

        public int getRoomNo() {
                return ROOM_NO;
        }

        private Occupant o;
        private final int ROOM_NO;
}

/* overhead waste :( oh well... */
class RoomOperations {
        public static void setRooms(List<Room> rooms) {
                this.rooms = rooms;
        }

        public static List getRooms() {
                return rooms;
        }

        public static int findVacantRoom() {
                for(Room r : getRooms().toArray(new Room[0]))
                        if(r.getOccupant() == null)
                                return r.getRoomNo();
                return -1;
        }

        private static List rooms;
}


JavaBoi Dec 3rd, 2007 4:02 PM

Re: public method
 
Don't know if you're still working on this or not...but you might want to look into ArrayList, which would be another alternative.

Logic would just be having a class with an ArrayList containing all classes that are Rooms, and have a boolean in the room classes which would determine if it had been used or not. In your findVacantRoom method you would simply check the rooms in the ArrayList to see if their boolean is true or false, and go from there.

Very simple, ArrayList is amazing :)


All times are GMT -5. The time now is 11:13 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC