• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Please Help Me

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Everyone!!!



I know this may be a bit too much to ask, but what do these exactly do? If there is a brief answer on this.

Also, if anyone has the time, can someone please tell me where and what this person is doing while using the util package in the following code.
Thanks in Advance!!






I just do not exactly understand how the util. package is being implemented here. Could the coder have used something else?? I know I can look in the java tutorial for such information, but sometimes the tutorial acan be a bit confusing to me at times. Thanks!

-Sean Magee

[ December 06, 2004: Message edited by: Sean Magee ]
[ December 06, 2004: Message edited by: Sean Magee ]
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sean:

Java Collections are a bit much to cover in a post on this bulletin board. I'm afraid you'll have to read that section in the tutorial. If you have any questions after reading it, be sure to get back to us.
 
Sean Magee
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand. Thank you. Is it too much to even briefly describe the relation of the package to this code posted above?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you just after the import and package syntax?

The package statement declares where this class resides in a name space. In most development setups the package structure equals the directory structure for the java files. Let's imagine the developers at Sun. Maybe they put all their source in a directory called "src", actually a fairly common practice. The util source code is then in a directory called "src/java/util". They also put the line "package java.util;" at the top of their source code.

Your class "Room" doesn't have a package statement. So if you're running your compiler in the "src" directory your "Room.java" is right there with you. That's perfectly legal, but not a great practice for anything but the smallest programs. If you organize your code in directories just make the package match the directory tree.

"import" tells the compiler where to find other classes, so when you use HashMap you have to add an import statement to help the compiler out. As an alternative, you could say "java.util.HashMap" every time instead of just "HashMap", but that's ugly extra typing.

The final part of the formula is classpath. If you go to "src" to compile and everything is in subdirectories from there then you don't have to worry about it. But if some of the source is in c:\my\src and some in c:\sun\src we need classpath. It's just a list of the root directories or jar or zip files to start searching in. You might set classpath c:\my\src;c:\sun\src to get files from either directory.

Was that the answer to the right question?
[ December 06, 2004: Message edited by: Stan James ]
 
Sean Magee
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, im sorry, it wasnt, but thank you.

For example what is


doing?

Also what is :

and at last

I just do not understand what EXACTLY is going on here, been looking at it for awhile trying to figure it out. If anyone can help, please, do.

FYI, I do understand the return statemnts and if statements etc.

What I do not understand is why .put nad .get are being used/
What is keys.iterator();iter.hasNext


[ December 06, 2004: Message edited by: Sean Magee ]
[ December 06, 2004: Message edited by: Sean Magee ]
 
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well a HashMap is a Map, which is to say that a 'key' has a corrosponding 'value', like a function. For instance, you could map football players' positions to names, so that Wide Reciever would map to Terrel Owens, and Quarterback would map to Donnovan McNabb. (E.A.G.L.E.S. EAGLES).

So:
When I say
HashMap players=new HashMap();, I create an empty hash map.

I can then say
players.put("Wide Reciever","Terrel Owens"); I have mapped "Wide Reciever" to "Terrel Owens"
players.put("Quarterback","Donnovan McNabb"); I have mapped "Quarterback" to "Donnovan McNabb".

I can now say:

String quarterback=map.get("Quarterback"); and quarterback will be set to "Donnovan McNabb".

Dig?

Note that any Object can be a key, and any Object can be a value, so you can map "D-line" to an ArrayList with the whole Defensive line.
(Picky people will point out that come java version 5, you don't even need these to be Objects. To them, I stick my tounge out and say, "5 ain't released yet!")

Maybe someone else will take up the topic of iterators now.
[ December 06, 2004: Message edited by: Nick George ]
 
Sean Magee
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Nick. Ok got it.
So using a hash map is perfect for making a text based game like the one above. Every place is referencing the "exits" of other places. cool.

By doing is what enables something that is in another class like after a room has been declared of course. Again, cool. Now if only i knew what the iterator does. I know it correlates with the hash map somehow.

[ December 06, 2004: Message edited by: Sean Magee ]
[ December 06, 2004: Message edited by: Sean Magee ]
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to do a little more riffing on the idea that a Map is like a function. More than one input can have the same output. For instance, say that instead of mapping position to player, we did player to position. Both Lito Sheppard and Sheldon Brown are Cornerbacks. So, we can map both of them to Cornerback.

//remember, key, then value.
players.put("Lito Sheppard","Cornerback");
players.put("Sheldon Brown","Cornerback");

This will never cause a problem, becuase the keys are all unique, so there's never a question as to what will come from

players.get("Lito Sheppard");

however, if we mapped "Terrel Owens" to both "Wide Reciever" and "Best in the league", there's a problem, because what comes of
players.get("Terrel Owens");

So, the second mapping will override the first.

Dig?
 
Sean Magee
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I Dig.


[ December 07, 2004: Message edited by: Sean Magee ]
 
Look ma! I'm selling my stuff!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic