aspose file tools*
The moose likes Cattle Drive and the fly likes OOP-3 Sorting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "OOP-3 Sorting" Watch "OOP-3 Sorting" New topic
Author

OOP-3 Sorting

jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Ok, I know this one has probably been talked to death, but I have hit the proverbial wall now I think.
I have had no problems with getting the I/O to work, nor with printing out the names in order based on first names, that's the easy part. What's got me is what interface I'm supposed to be implementing, and how that is going to help in ordering, more specifically with ordering by last name. What I have is two classes, one independant class that does the sorting algorithims and printing, and the other main class that creates the ArrayList from the I/O and sends it to an object of the other class as an argument.
If you implement an interface, you must implement ALL methods in that interface, though you can leave the ones you don't need as empty methods, this I understand (unless you want to declare the implementing class abstract, don't think that is what is wanted here). I've been through quite a bit of the API, following the trail as was suggested from previous topics on this assignment, but haven't seen a good way to do what is requested by the assignment. It seems to me that you could create your own method that uses logic to ignore the first part of the string and order just by the last, then print out the full string.
I've read through just about every post in the Cattle Drive relating to this assignment, and unfortunately (unlike other assignments I got frustrated with) have gotten no further than I already was. Suggestions?
Thanks!
Jason
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
The assignment states that the sorting should be done by the Collections.sort method. My recommendation would be to look at the Collections class for a clue. I hope this helps.
Matthew Phillips


Matthew Phillips
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
I do see in Collections where there is a sort that takes not only a List but also a certain interface as its argument. Are you to implement that interface with your other class and pass an object of that class to be sorted? I don't think this is correct because in everything I have read, this interface is used to sort in different orders.
This specific interface has a method for comparing two objects, but if you implement the interface, you have to override the method.
Perhaps I'm not seeing what the benefit of implementing the interface in this situation over just putting your own method in a class and calling that method, because it seems you aren't really getting any extra functionality (unlike when you implement EventListeners and such).
[This message has been edited by jason adam (edited July 02, 2001).]
Richard Boren
Ranch Hand

Joined: Mar 01, 2001
Posts: 233
Originally posted by jason adam:
This specific interface has a method for comparing two objects, but if you implement the interface, you have to override the method.

Bingo. You create you own comparer class which implements the interface which requires you to overrided its method to do what you want.

Perhaps I'm not seeing what the benefit of implementing the interface in this situation over just putting
your own method in a class and calling that method, because it seems you aren't really getting any extra
functionality (unlike when you implement EventListeners and such).

Creating your own method would be duplicating code that already exist.
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
Originally posted by jason adam:
I don't think this is correct because in everything I have read, this interface is used to sort in different orders.


Isn't this the behavior that you are expecting when sorting by last name?
Matthew Phillips
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1751
    
    2
Originally posted by jason adam:
Perhaps I'm not seeing what the benefit of implementing the interface in this situation over just putting your own method in a class and calling that method...

Without getting too weird about this...
I think the interface approach represents a beautiful, elegant object-oriented solution to the problem.
When you implement that interface by overiding that method you've encapsulated an algorithm into a class, made it reusable elsewhere throughout the system, in effect (like any class you build) you've extended the language to include your own little sorting rule.
But think about how you've done it. You haven't had to worry about the mechanics of sorting. That is, you don't have to worry about the details of what underlying data structure you have, or the particular sort technique (do you search the whole list for the "first" element and put it in the first position, then search the list again for the "second" element then put it in the second position, etc., or do you compare elements in positions 1 and 2 and either accept the order or switch them then compare elements 2 and 3 and either accept the order and switch them, etc., or do you work the list from top to bottom or bottom to top, or do you split the list into 2 lists and work from the outside in to the middle or from the middle to the out, etc.)
All you have to do is write the logic that decides, given two elements, which comes in front of the other. (My version has 12 lines -- 5 of which are "actual code" and the rest is method signatures and lines consisting only of curly braces.)
And the infrastructure provided by Java takes care of all the gory details.
[This message has been edited by Michael Matola (edited July 02, 2001).]
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1751
    
    2
I know I've made some long-winded, weirdly enthusiastic posts on this assignment, but I think this stuff is lots of fun and it's really hard (try it!) to give people useful hints without revealing too much.
(Heck, I haven't even been nitpicked yet on this one, so I don't even know what the instructor's solution is.)
If folks find my comments particularly unhelpful, send me an email -- my address is in my profile -- and I'll try a different approach.
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
I have been thinking about this one all day, and I think I understand how you are supposed to implement and pass the class to the sort method, the problem is now figuring out HOW to write the class so that it is meaningful to the sort method. I have been thinking that the implemented class passes just an integer value, because you have to override a method that returns an integer (well, a method that returns a boolean also, but that method doesn't really work here). I still am learning how to write a class so that when you pass an object of that class, it can be used somewhere else.
So I am assuming then that I need to have the actual source code for the sort method to figure out what kind of parameters it can take. Am I off base here? The only thing I have read as far as the API goes is the functionality of the methods of each class, not the actual code.
Thanks y'all for the help, taking my baby steps towards understand objects!
Jason
[This message has been edited by jason adam (edited July 02, 2001).]
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
As a side note, I whole-heartedly agree with Michael, I love the object-oriented approach that Java focuses, and relies, on. Here is what I am comfortable with, and what I am still learning:
A) I love to create LOGICAL classes, and have no trouble either extending these classes or creating objects from these classes, and using the methods and such I have laid out within them.
B) I have created some pretty neat applets and GUI programs by extending classes and implementing EventListener classes, but this is all based off of examples I have read, and what limited understanding I have of polymorphism.
C) My biggest hurdle right now is figuring out how to implement all of the code that has been laid down already, and use that in the classes and interfaces I create. This is the true elegance of Java.
I'm sure with some trial, error, practice, Cattle Drive, and LOTS of Marilyn input, I will become more confident with these skills.
Anyways, that's my blurb on where I am, and might give you some insight on why I'm having a bit of trouble with OOP-3!
Thanks again,
Jason
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
I think my biggest problem with OOP-3 was that I was trying to reinvent the wheel. It sounds like you might be having the same problem. The breakthrough for me was the realization that I could use Collections.sort for both sorts. The real key is not to worry about what is going on behind the scenes with the sort method. What you need to worry about is giving it a way to compare the last names. One of the methods of the required interface gives you the means to do this. The String class has methods that you can use inside that method to allow the comparision to operate on the last names.
I hope I didn't give too much away.
Matthew Phillips
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1751
    
    2
Originally posted by jason adam:
I have been thinking about this one all day, and I think I understand how you are supposed to implement and pass the class to the sort method, the problem is now figuring out HOW to write the class so that it is meaningful to the sort method.

Exactly.
I have been thinking that the implemented class passes just an integer value, because you have to override a method that returns an integer (well, a method that returns a boolean also, but that method doesn't really work here).

Well, officially the class passes itself, and the guts of the method from Collections (actually it's the class Arrays doing the work, but that's a different story -- no need to worry about that detail) call the method you override and use the integer value -- but you've got the idea.
Note that you do not have to override the other method, the one that returns a boolean. Although this method is part of the interface you will implement, this method already has an implementation. Whatever class you create ultimately extends Object, which already implements this method.
Note also that this method that returns a boolean isn't used to specify the sort rule -- it's used to compare whether the current instance of an object implementing the given interface is equal to another object implementing that same interface. (See the API for details if you're curious.)
So I am assuming then that I need to have the actual source code for the sort method to figure out what kind of parameters it can take. Am I off base here? The only thing I have read as far as the API goes is the functionality of the methods of each class, not the actual code.

I think you can get what you need from the API. The second parameter is a class implementing the mystery interface.
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Wow, I actually got it to work. Only thing is, I probably am doing it totally wrong, because I am still not sure how my comparing method is actually pulling in the information from the ArrayList. Does the Collections sort method handle all of this? It's the only way I that I can see this is working, if there is stuff going on behind the scenes that I am not aware of (imagine that).
Thanks!
Jason
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1751
    
    2
Originally posted by jason adam:
Wow, I actually got it to work. Only thing is, I probably am doing it totally wrong, because I am still not sure how my comparing method is actually pulling in the information from the ArrayList.

Sounds to me like you've got it right. (Or least your solution sounds like mine and the other contributors'.) You don't have to know the gory details to make it work.
Does the Collections sort method handle all of this? It's the only way I that I can see this is working, if there is stuff going on behind the scenes that I am not aware of (imagine that).

For source code fans --
There's lots going on behind the scenes here. If you follow the scource code, you'll see that the 2-parameter Collections.sort() method calls Arrays.sort() with the same parameters, which does most of the dirty work. The Arrays code is hard for me to follow, but you'll find that in lines 1174-1205 the method you overrode in the mystery interface gets called several in several places.
(As far as I can make out this method uses recursion to split the list in half, then splits that in half again, etc. until it has chunks of 6 elements or less, which it sorts, then reassembles itself.)
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
That totally makes sense about the recursion part. I put in a line in seperate sections of my independant class that simply does a System.out.println("Test1"); (or Test2, Test3, etc) so I can see when things are being done, and before I even see a line print out from the list, it does several repeated method calls.
Well, good to know I got the gist right, now to figure out optimization!
Thanks for all of the help y'all,
Jason
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: OOP-3 Sorting