Ok, so it's been like 3 months iwth no programmign and now I'm back and rusty. Last year my first year of programming in any language we touched interfaces a bit at the end, but not a lot and I forgot most of it.

So anyway my lovely assignment, is not hard if I didn't have to use interfaces and casting.

I have to using an interfacing and casting, is to sort an array of people by their names alphabetically, and the people also must have their age contained with them.

Main pretty simple is what I've got

Code:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

Yes everything is wrong here, multiple return statements...
I don't fully understand how to do this. I want to cast theArray as a String array, not as object array, and then I want it to go through this and sort all the names alphabetically in the array.

I am going to think about this more in the morning I kind of started sleepy at 1 am D:
In the mean time, suggestions are welcome :D

09-06-2009, 01:01 PM

Fubarable

Your first compareTo was the correct one as this method is only used to compare one object to another. The only way I can see to make your method better is to use the generic Comparable interface (if you're allowed to do that) as it will let you avoid the cast:

So either use this, or the first method that you have (with the cast), and then you either sort your array by using Arrays.sort(myArray) or by writing your own sorting routine. If you must write your own, then you'll need to iterate through the array comparing instances with other array instances using this compareTo method (the order of iterations and how you do this will depend on what sorting algorithm you'll use).

There is a problem with that bolded line though, because it wants a comparing not a String. But how would I get around this? Because I thought that might work because Comparing is a String and an int, and that's what I gave it. Any ideas?

09-06-2009, 10:52 PM

Fubarable

Again, your first compareTo was correct.

Meaning this is a well-formed compareTo method, one that Arrays.sort can use:

Please understand this: compareTo deals only with comparing the current object ("this") with the object passed via its parameter. It does not deal with any array of objects or collection of objects, and in fact does not care about this. It only deals with comparing two objects and returning a number based on the rank of the current object vs the parameter object. Please re-read these statements until they sink in as it is at the crux of your current misunderstanding. The Arrays.sort method will worry about the array, it will iterate through the array (probably several times) and will use your compareTo method to compare each object it iterates through against another. This part you don't have to worry about as you'll let the Arrays.sort worry about the details here. You just have to worry about getting your compareTo method correct. Please delete, burn, vaporize, and destroy this current method as it will only mess you up.

09-06-2009, 11:29 PM

jigglywiggly

Ok I just did a Gutmann 35 pass to delete it, happy now? :rolleyes:
Thnaks for the advice, I should have this figured out :)

09-06-2009, 11:40 PM

Fubarable

Finally, thanks to you for the first time today, I can say that I am truly happy. :D

09-07-2009, 12:05 AM

jigglywiggly

Ok time to break your happiness :(
How would I end up storing the actual peoples names into theRay array? It is of Comparing, because it holds a name and age.

So I'm still a bit confused on interfaces, but I just want to make sure this is how it will go:
Take the name and age
Put it in the array
Do Arrays.sort(theRay)
Is that right?
Don't face palm D:

09-07-2009, 12:10 AM

Fubarable

Quote:

Originally Posted by jigglywiggly

Ok time to break your happiness :(
How would I end up storing the actual peoples names into theRay array? It is of Comparing, because it holds a name and age.

and since Comparing implements Comparable and has your wonderful compareTo method in it, all is still good.

Quote:

So I'm still a bit confused on interfaces, but I just want to make sure this is how it will go:
Take the name and age
Put it in the array

Yes, but to be complete, I'd make this step: Take the name and age, create a Comparing object with them, and place this object into the array.

Quote:

Do Arrays.sort(theRay)
Is that right?

Only after the array has been completely filled with Comparing objects, yes. Why not try it out and see for yourself?

Much luck!

09-07-2009, 12:19 AM

jigglywiggly

Was thinking of changing my main to something like this, just to simplify it by creating a few example people.