Results 1 to 11 of 11
  1. #1
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Unhappy 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
    Java 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);
    
        }
    
    }
    So I just started out simple, not trying to worry about doing this with arrays and this is what i've gotten

    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));
    
      }
    
    
    
    }
    Now I really don't get how to do this with arrays properly...
    So I just tried random stuff

    This really doesn't make any sense...

    Java Code:
    public int compareTo(Object[] theArray) {
     
        for(int j=1; j<theArray.length; j++) {
           return(theArray[j].compareTo(theArray[highPos]));
          
            }
            return highPos;
        }
    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
    Last edited by jigglywiggly; 09-06-2009 at 10:55 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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:

    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));
      }
    }
    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).

  3. #3
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    I'm allowed to use Arrays.sort(myArray)

    So here is what I tried
    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));
    
      }
    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?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Again, your first compareTo was correct.

    Meaning this is a well-formed compareTo method, one that Arrays.sort can use:
    Java Code:
     public int compareTo(Object name){
        String b = ((Comparing)name).getName();
        String a = this.Name;
        return(a.compareTo(b));
    
      }
    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, 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));
    
      }
    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.

  5. #5
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

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

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  7. #7
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    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 01:19 AM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by jigglywiggly View Post
    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.

    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.
    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!

  9. #9
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    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

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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)?

  11. #11
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    You are absolutely right :) just made the array of 4(for now) instead of 50.
    EDIT:
    Yay, thanks to you making me understand an interface much clearer, I got it working :D
    Last edited by jigglywiggly; 09-07-2009 at 03:09 AM.

Similar Threads

  1. [SOLVED] [newbie] compareTo that returns an Enum
    By jon80 in forum New To Java
    Replies: 4
    Last Post: 05-18-2009, 12:03 AM
  2. Replies: 8
    Last Post: 03-19-2009, 10:38 PM
  3. compareTo()
    By Tsiliadoros in forum Advanced Java
    Replies: 5
    Last Post: 10-03-2008, 02:18 PM
  4. how compareTo Method works
    By nanaji in forum Advanced Java
    Replies: 1
    Last Post: 06-22-2008, 08:40 PM
  5. Interfaces
    By jon80 in forum New To Java
    Replies: 2
    Last Post: 05-03-2008, 10:57 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •