aspose file tools*
The moose likes Cattle Drive and the fly likes Need Help with OOP-3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Need Help with OOP-3" Watch "Need Help with OOP-3" New topic
Author

Need Help with OOP-3

Matt Fry
Ranch Hand

Joined: Mar 06, 2006
Posts: 60
I have a class that does my I/O and populates an ArrayList. I can sort the list by first name fine. Where I am running into trouble is trying to sort by the last name. I was trying to use a new class that will implement an interface and override a method in the interface. I will then instantiate an object of this class and pass it to my sort method. So far, so good???

But where I am really getting frustrated is in the method I'm trying to override. This method has to return an int. But the signature says it has to be passed two objects (I assume some casting could be involved, but I've only got one ArrayList). The only way I can think of to get the ArrayList of strings from my first class is to create a constructor in the second class that takes an ArrayList. Then, when I instantiate an object, I can pass the ArrayList into the second class.

But, how do use this in the overridden method?

I hope someone can help me with this. Am I even going about it the right way. From what I've read and learned from previous posters, I believe I'm on the right track.

I am really frustrated with this assignment. I've read every thread in the Cattle Drive forum concerning this assignment multiple times (I've worked on this assignment at least 10-12 hours so far) and this is as far as I can get.
Mark Beattie
Greenhorn

Joined: Mar 30, 2006
Posts: 26
Hi Matt,

I was fortunate to be in the middle of reading 'Head First Java' when I joined Cattle Drive. Thus I was prepared for this confusing topic on Collections. I find reading the Java API very difficult unless I have examples I can use.

In the first paragraph it sounds like you've got everything right. It's the second paragraph that indicates the problem you are having. You are right in that you need two objects. But the objects are not ArrayLists. The interface Comparator is not that complex once you get it working. I did not understand exactly what it did until I got my code to work.

Spoiler, or not really, ahead:
Use the same objects you used to populate the ArrayList for the comparator.
Matt Fry
Ranch Hand

Joined: Mar 06, 2006
Posts: 60
Thanks for the reply, Mark.

I still am completely in the dark on this one. I believe I remember reading earlier that you don't call the overridden method. I can't figure out how to get the objects needed into the method and tell it how to "use" them. I've tried putting code into the second class to read the file in and do some substring stuff, but I don't think it's working write at all.

I'm just missing something very fundamental in all this. Everything I try is little more than a wild guess with little hope of actually working. I really have no idea what I'm doing with this. I think I understand the principles behind implementing an interface and overriding methods, but something is getting lost with me somewhere. I've read several times where it's not that complicated, but to me it really is.

I've read Just Java once cover to cover and I went back and read the relevant chapters again. Before that, I read the Head First Java book twice. So, I understand the concepts (I think), I just don't get how to apply them in this scenario. I think my main point of confusion is overriding the method and "telling" it how to compare. I know how to substring and all that, but I don't understand beyond that.

I've about done all I can on this today. I'm all in. Maybe a good nights sleep will help to tackle this fresh in the morning. This assignment is TONS more difficult that Say ever was.

Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
I have a class that does my I/O and populates an ArrayList. I can sort the list by first name fine. Where I am running into trouble is trying to sort by the last name. I was trying to use a new class that will implement an interface and override a method in the interface. I will then instantiate an object of this class and pass it to my sort method. So far, so good???

Yep.

But where I am really getting frustrated is in the method I'm trying to override. This method has to return an int. But the signature says it has to be passed two objects (I assume some casting could be involved, but I've only got one ArrayList).

Yep.

The only way I can think of to get the ArrayList of strings from my first class is to create a constructor in the second class that takes an ArrayList.

Nope. Look again at the method that uses the class you are creating (the one that implements the interface). Not the methods in that class, but the one that creates/uses it to sort the last names. What's the first parameter? That's how the list gets to your new class. Automagically.

But, how do use this in the overridden method?

Now that your new class has the list, it can use the objects in it.
[ July 10, 2006: Message edited by: Marilyn de Queiroz ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Mark Beattie
Greenhorn

Joined: Mar 30, 2006
Posts: 26
Hi Matt,

Looking over my message I said your second paragraph was where you were having problems. I just want to point out that you are right in that casting is required in the Comparator class.

Mark
Matt Fry
Ranch Hand

Joined: Mar 06, 2006
Posts: 60
Thanks, Mark. I finally got a solution that works

What was really giving me fits was understanding how Strings that were stored in an array in one class got "passed" to the other class and in the manner that they got passed. I still don't fully understand how they got there, but now I see that I don't have to.

This assignment has been the most difficult one I've tackled yet. It had me questioning my own abilities to become a programmer at times. Thanks goes out you and Marilyn for your help and assistance, as well as all the other posters over the last 5-6 years whose advise I read and reread numerous times over the last 5 days.
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Nice feeling, eh Matt? Have yerself a cold sarsparilla on the house.
Mark Beattie
Greenhorn

Joined: Mar 30, 2006
Posts: 26
Great to hear Matt!
Matt Fry
Ranch Hand

Joined: Mar 06, 2006
Posts: 60
Originally posted by Pauline McNamara:
Nice feeling, eh Matt? Have yerself a cold sarsparilla on the house.


Yup, it feels mighty good! This one really gave me fits.

Hey, looky there, says "ranch hand" now instead of "greenhorn". Reckon I'm movin' on up the trail for sure now.
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Watch out! Ranch hand on the move!

Congrats++

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Need Help with OOP-3