![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Nov 2007
Posts: 2
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
12 years old
Join Date: Nov 2007
Posts: 94
Rep Power: 1
![]() |
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;
} |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Dec 2007
Posts: 1
Rep Power: 0
![]() |
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 ![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Cannot assign 'Add' because it is a 'method group' | Jarod Silverstar | C# | 1 | Oct 13th, 2007 11:14 PM |
| Method not returning string | csrocker101 | C# | 1 | Apr 6th, 2007 6:45 PM |
| HTTP Status 405 - HTTP method POST is not supported by this URL | hemanth.balaji | Java | 5 | Mar 21st, 2006 2:48 AM |
| method doesn't recognize variable | Krista | Java | 1 | Dec 5th, 2005 5:40 PM |
| Debug recursion method() | pr0gm3r | Java | 3 | Oct 11th, 2005 12:33 PM |