Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 21st, 2007, 8:16 AM   #1
dirtycash
Newbie
 
Join Date: Nov 2007
Posts: 2
Rep Power: 0 dirtycash is on a distinguished road
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?
dirtycash is offline   Reply With Quote
Old Nov 21st, 2007, 8:23 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Nov 21st, 2007, 8:43 PM   #3
null_ptr0
12 years old
 
Join Date: Nov 2007
Posts: 80
Rep Power: 1 null_ptr0 is on a distinguished road
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;
}
null_ptr0 is offline   Reply With Quote
Old Dec 3rd, 2007, 4:02 PM   #4
JavaBoi
Newbie
 
Join Date: Dec 2007
Posts: 1
Rep Power: 0 JavaBoi is on a distinguished road
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
JavaBoi is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:02 PM.

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