PDA

View Full Version : Java help


PureMisery
01-15-2007, 08:26 AM
I know the title isn't much, but it's all I could come up with to relate to my problem.

I have a bunch of room objects. I need a method to return a certain room object based on a String input.


public Room getRoom(String roomName){
//code to return the room object with the name roomName
return room;
}


I also need a way to randomly return a room object.

A room object:

public Room(String name, String description) {
this.name = name;
this.description = description;
visits = 0;
}


Does anyone have a suggestion?

Aradon
01-15-2007, 12:51 PM
This sound suspiciously like a homework to which I point to our rules


1.5) No homework assignments - Do not post your entire homework assignment and request that other members do it for you. This is considered cheating, and your thread may even be used by your school to prove your guilt. Now, you may ask for advice or help on a specific aspect of your assignment that you're having trouble with. Use common sense as far as what's acceptable in terms of soliciting help with homework assignments.


I'm just posting this so that we know where the boundries are.

So, my suggestion is that you need to keep track of a List of some type of room objects. An Array, we could say. That way you can search the array for a room object by it's name.

As far as Random goes you could look into the Random Class that is provided by the Java API (I think it's in Math.Random) That way you can pick a random number that is an array number access.

Hope that helps you get started on one way to do this problem :)

pludlow
01-18-2007, 01:45 PM
I think in your case, you'd be better off using a HashMap, for problem A anyway. That way you wont have to do an interative search, and you can mention why you used a hashmap and its advantages over other collections for this particualr problem. *This will be sure to earn you marks.

Also think about applying generics if you are using Java 5, again for more marks.

Anyway, I'll leave you to research those yourself. Dont really want to get booted off the forums after my 4th or 5th post :)