aspose file tools*
The moose likes Cattle Drive and the fly likes please hint me on Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "please hint me on "SortNames" !" Watch "please hint me on "SortNames" !" New topic
Author

please hint me on "SortNames" !

juliane gross
Ranch Hand

Joined: May 30, 2002
Posts: 161
The assignment says "..Do not modify the ArrayList or its contents except by using the Collections class".
For order of first names, no problem.
For order of last names BIG problem!
Question 1) Is is correct that I can NOT write a method, which separates the names, sorts them via Collection class, and puts them afterwards together via kind of backward cut-and-paste?
If yes:
Question 2) Do I maybe have to use the compareTo-method of Comparable?
If yes: Question 3) How to compare STRINGS whether one is "greater" than the other? This makes no sense to me
I do not understand the example in van Linden's "Just Java 2" on page 204-205, for example, why is he introducing java.lang.Comparable, couldn't I use my ArrayList instead? (My test programs cannot).
Unfortunately I even don't know exactly what I could ask!!
Maybe someone may give me a few hints in which direction I should navigate my poor confused brain, thank you.
Juliane
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Question 1) Is is correct that I can NOT write a method, which separates the names, sorts them via Collection class, and puts them afterwards together via kind of backward cut-and-paste?

Yes, as long as you don't store the reversed names into another ArrayList or array, etc.

Question 2) Do I maybe have to use the compareTo-method of Comparable?

That's one way to do it.

If yes: Question 3) How to compare STRINGS whether one is "greater" than the other? This makes no sense to me

The String class already knows how to compare one String to another. All you have to do it tell it to compare stringOne to stringTwo. As I recall, it sorts the Strings in alphabetical order.

I do not understand the example in van Linden's "Just Java 2" on page 204-205, for example, why is he introducing java.lang.Comparable, couldn't I use my ArrayList instead? (My test programs cannot).

I don't have an example on pages 204-205. Must be a different edition. Can you tell me which section (by heading/subheading) this example is in?


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
juliane gross
Ranch Hand

Joined: May 30, 2002
Posts: 161
thanks a lot, Marilyn.
The example is the fourth and fifth page in chapter 8 "interfaces".
The code example begins on the fourth page with "public class Double implements Comparable" and is in snippets, interrupted a few times by explanations.
On the next page van Linden says:
// a constructor to fill up the d array.
sortingClass(java.lang.Comparable data[]
{ d = data }
...
this part and what follows is not clear to me:
why "java.lang.Comparable"?
I don't think (I hope I didn't) I missed an explanation somewhere before if there was one, but this way of syntax
(java.lang. ..) at this kind of place in a code is new to me ..
thank you!
Juliane
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Juliane, Comparable is not the only interface you can use for sorting. Take a look at the excellent articles about implementations of the Collection Interface by Thomas Paul in the JavaRanch Newsletters.
The part on TreeSet could help...
I'm just peeking ahead to the time when I get the Java-*'s finished, so I could be wrong.
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I have just tried that other interface, it works a dream for sorting on both the first AND the last names.
But, is it THE solution? I will have to wait a few weeks to find out.
-Barry
BTW are inner/nested/anonymous classes tabu for these assignments?
juliane gross
Ranch Hand

Joined: May 30, 2002
Posts: 161
Barry, do you know that the last name is part of the (fixed) string,for example "Barry Gaunt".
I thought I could search the beginning of the last name via finding the blank, and what comes after it, is the valid thing. Then sort the whole name string via comparing it to the sorted last name result.
I don't know if inner/nested/anonymous classes are not allowed (Marilyn??), well, some time ago I read theories about them, but so far I have no experience at all using them.
Out of pure curiosity (since you mentioned those in context with "a dream" , I will go to my books immediately and check this area again.
Juliane
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Hold on, Juliane when I said "works a dream", that means it "works very well". And I cannot see into the future - I just wanted to know if nested classes can be used - it was not a hint. Whoops! Sorry, beg pardon.
Must we put only String objects in that ArrayList or can we put some wrapper objects in there? If we can only put String objects in I have to rethink things.
Are the lines in the input file unique? That is, no duplicates?
The contents of the ArrayList can only be modified using methods of the Collections class? Ho Humm Diddley...
If we can only use String objects in the ArrayList and there are no duplicated lines, then there IS one method of java.util.Collections, apart from sort(), that can help. Take a close look at the Collections API one more time.
-Barry
[ January 13, 2003: Message edited by: Barry Gaunt ]
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
I can't speak for the nitpickers or what will pass and what will not, but my personal advice on this one is that if you're not already familiar with inner/nested/anonymous classes, don't bother using them on this assignment. You can do this assignment quite nicely with top-level classes. There are plenty of other interesting things to learn on this one without the added noise of inner/nested/anonymous classes.
Another note: there's been some good discussion of this assignment in this forum. If you don't need any hints, of course go ahead and do things on your own. But if you find yourself getting stuck, there are some previous hints out there and pointers to good resources.
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
Originally posted by Barry Gaunt:
Must we put only String objects in the ArrayList or can we put some wrapper objects in there? If we can only put String objects in I have to rethink things.
From the assignment:
Load an ArrayList with Strings from a text file of names.
Sure it's an OO assignment, but it's not that OO.
Are the lines in the input file unique? That is, no duplicates?
There aren't any duplicates in the file the assignment supplies. But why should duplicates matter?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
are inner/nested/anonymous classes tabu for these assignments?
They are not taboo, but not necessarily recommended either.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I've got an idea that I might be confusing Juliane, so I'm going to volunteer to shut me up. Altogether now: "GOOD IDEA!!!"
But, I have improved my first version of OOP-3: it now uses only Strings in the ArrayList, and uses an alternative Interface to Comparable and sorts on first AND last names.
And no inner/nested/anonymous classes.
Keep at it Juliane I have got to get back to take a try at Java-7.
-Barry
[ January 13, 2003: Message edited by: Barry Gaunt ]
juliane gross
Ranch Hand

Joined: May 30, 2002
Posts: 161
Another note: there's been some good discussion of this assignment in this forum. If you don't need any hints, of course go ahead and do things on your own. But if you find yourself getting stuck, there are some previous hints out there and pointers to good resources.
thank you, Michael, for this useful tip,
and many thanx to you, Barry, to lay out these baits
No, I am very difficult to discourage and much used to do lots of trying before frustration (at least for something my degree in music should be useful )
By the way, I consider LOOPS a very musical subject..

I am confident about finding a solution to string sorting as well, and I hope not to have to write again about this subject!
Juliane
p.s. Barry: keep your last "dream solution" for a later comparison, i.e. if we both have passed
this assignment, will you? would be fun..
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
// a constructor to fill up the d array.
sortingClass(java.lang.Comparable data[]
{ d = data }
...

this part and what follows is not clear to me:
why "java.lang.Comparable"?


I think he is specifying java.lang.Comparable so nobody will confuse it with an interface in any other package. Some books will include a package of classes (usually on a CD). For instance, you could have an interface or class named com.queiroz.util.Comparable

Normally you don't need to specify any class/interface in the java.lang package (as you've probably noticed). We use java.lang.String, java.lang.Integer, etc frequently without either importing the package or using the fully qualified name (including the package).
juliane gross
Ranch Hand

Joined: May 30, 2002
Posts: 161
thank you, Marilyn, that helped.

Juliane
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
I'm in the middle of SortNames right now, but I've been thinking about it for a long time. I reccommend searching for old javaranch posts on the subject& reading them. Once you know what classes you need to learn more about, do a google search for articles and teaching materials from lectures on the subject. If you�re not careful you can really learn a lot about all kinds of nifty classes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please hint me on "SortNames" !