PDA

View Full Version : new to java


Bluej
11-21-2007, 07:48 PM
im finding it hard to answer the following question -

I have to create a public method.


findVacantSeaView has no parameters and returns an integer value representing the
room number of a vacant room that has a sea view. Only even numbered rooms have a sea
view. Room zero is occupied first, then room 2, then room 4 and so on. This method will
return the next vacant room, so if rooms 0, 2, 4 and 8 are occupied, findVacantSeaView
will return 6 which is the first vacant room with a sea view. The method must return -1
if there are no vacant rooms with a sea view.


any help would be appreciated :)

Gox
11-22-2007, 04:28 AM
What exactly are you having issues with? A blanket "I'm having trouble" doesn't make it real easy for us to help you. So, I'll just assume you want an algorithm in pseudocode.

However, since I don't know anything about how your rooms and such are implemented (since you didn't provide that info) it's hard for me to give you something that won't need tweaking on your part.

Loop:
1. Loop through all rooms checking only the even numbered rooms
Maybe a for-loop from "0 To Number of Total rooms", incrementing your counter by two for each iteration of the loop? (0,2,4,6....)

In The Loop:
2. Check the current room (which will be an even room) to see if it's vacant.

3. If the room is vacant return the vacant room number (otherwise the loop continues checking the next even room number) -- Method terminates on it's own here when you "return" the room number.

Loop Exits On It's Own:
4. If our loop terminates because we've checked all even number rooms and none are vacant, return -1. -- Method terminates on it's own here when you "return" -1.

Give it a shot and if you're still having problems post your code and specify what exactly is giving you troubles.

Gox

WilliamHolmes
11-29-2007, 01:25 AM
public int findVacantSeaView()
{
int RoomNumber = -1;
int TotalNumberofRooms = 100;
boolean[] RoomNumberVacant= getRoomVacancyArray();

for(int i=0;i <TotalNumberofRooms;i++)
{
if RoomNumberVacant[i] == true
{
RoomNumber = i;
break;
}
i++;
}
return RoomNumber ;
}