Results 1 to 11 of 11
- 09-06-2009, 09:52 AM #1
Senior Member
- Join Date
- Nov 2008
- Posts
- 105
- Rep Power
- 0
Help with interfaces, compareTo etc
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
So I just started out simple, not trying to worry about doing this with arrays and this is what i've gottenJava Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author jigglywiggly */ import java.util.Scanner; public class ComparingDriver { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter a name "); String name = scan.next(); System.out.println("Enter an age"); int age= scan.nextInt(); Comparing c = new Comparing(name, age); } }
Now I really don't get how to do this with arrays properly...Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author jigglywiggly */ public class Comparing implements Comparable { String Name; int Age; int highPos=0; Comparing theRay[] = new Comparing[50]; public Comparing(String name, int age) { Name=name; Age=age; } public int getAge() { return Age; } public String getName() { return Name; } public int compareTo(Object name){ String b = ((Comparing)name).getName(); String a = this.Name; return(a.compareTo(b)); } }
So I just tried random stuff
This really doesn't make any sense...
Yes everything is wrong here, multiple return statements...Java Code:public int compareTo(Object[] theArray) { for(int j=1; j<theArray.length; j++) { return(theArray[j].compareTo(theArray[highPos])); } return highPos; }
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 :DLast edited by jigglywiggly; 09-06-2009 at 09:55 AM.
-
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).Java Code:public class Comparing implements Comparable<Comparing> { String Name; int Age; int highPos = 0; Comparing theRay[] = new Comparing[50]; public Comparing(String name, int age) { Name = name; Age = age; } public int getAge() { return Age; } public String getName() { return Name; } public int compareTo(Comparing name) { String b = name.getName(); String a = this.Name; return (a.compareTo(b)); } }
- 09-06-2009, 10:41 PM #3
Senior Member
- Join Date
- Nov 2008
- Posts
- 105
- Rep Power
- 0
I'm allowed to use Arrays.sort(myArray)
So here is what I tried
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?Java Code:public int compareTo(Object name, Object age){ String b = ((Comparing)name).getName(); String a = this.Name; int b1 = ((Comparing)age).getAge(); int a1= this.Age; [B] theRay[50]=(b,b1);[/B] return(a.compareTo(b)); }
-
Again, your first compareTo was correct.
Meaning this is a well-formed compareTo method, one that Arrays.sort can use:
Your latest method, on the other hand, isn't, as for one the method signature (the top line of the method) doesn't even match the proper signature for this method:Java Code:public int compareTo(Object name){ String b = ((Comparing)name).getName(); String a = this.Name; return(a.compareTo(b)); }
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.Java Code:public int compareTo(Object name, Object age){ String b = ((Comparing)name).getName(); String a = this.Name; int b1 = ((Comparing)age).getAge(); int a1= this.Age; theRay[50]=(b,b1); return(a.compareTo(b)); }
- 09-06-2009, 11:29 PM #5
Senior Member
- Join Date
- Nov 2008
- Posts
- 105
- Rep Power
- 0
Ok I just did a Gutmann 35 pass to delete it, happy now? :rolleyes:
Thnaks for the advice, I should have this figured out :)
-
Finally, thanks to you for the first time today, I can say that I am truly happy. :D
- 09-07-2009, 12:05 AM #7
Senior Member
- Join Date
- Nov 2008
- Posts
- 105
- Rep Power
- 0
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:Last edited by jigglywiggly; 09-07-2009 at 12:19 AM.
-
and since Comparing implements Comparable and has your wonderful compareTo method in it, all is still good.
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.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
Only after the array has been completely filled with Comparing objects, yes. Why not try it out and see for yourself?Do Arrays.sort(theRay)
Is that right?
Much luck!
- 09-07-2009, 12:19 AM #9
Senior Member
- Join Date
- Nov 2008
- Posts
- 105
- Rep Power
- 0
Was thinking of changing my main to something like this, just to simplify it by creating a few example people.
Java Code:Comparing[] apts = new Comparing[50]; Scanner scan = new Scanner(System.in); System.out.println("Enter a name "); String name = scan.next(); System.out.println("Enter an age"); int age= scan.nextInt(); apts[0] = new Comparing("Jamey", 24); apts[1] = new Comparing("Erica", 21); apts[2] = new Comparing("Jolee", 18); apts[3] = new Comparing(name, age); //Then maybe Arrays.sort(apts); //but if I do that, I get a java.lang.NullPointerException
-
Most of your array is filled with null items. What if you instead use an array size of 4 (to match the number of items it holds)?
- 09-07-2009, 01:52 AM #11
Senior Member
- Join Date
- Nov 2008
- Posts
- 105
- Rep Power
- 0
Similar Threads
-
[SOLVED] [newbie] compareTo that returns an Enum
By jon80 in forum New To JavaReplies: 4Last Post: 05-17-2009, 11:03 PM -
[SOLVED] Sorting / compareTo method confusion. Please help
By kbullard516 in forum New To JavaReplies: 8Last Post: 03-19-2009, 09:38 PM -
compareTo()
By Tsiliadoros in forum Advanced JavaReplies: 5Last Post: 10-03-2008, 01:18 PM -
how compareTo Method works
By nanaji in forum Advanced JavaReplies: 1Last Post: 06-22-2008, 07:40 PM -
Interfaces
By jon80 in forum New To JavaReplies: 2Last Post: 05-03-2008, 09:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks