Help! Problem with this class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Help! Problem with this class

  1. #1
    Join Date
    Jun 2005
    Posts
    3

    Help! Problem with this class

    I need to make a program using at first an OrderedList (already defined with succes) and then a list of names with a priority number...
    The problem is that I must define a type of object (Paziente) with two values (a String as a name and an int as a pririty value), but i must make this type working as a Comparable ( i MUST use Comparable objects)...
    this is the class Paziente I tried to make:


    public class Paziente implements Comparable{

    private String nome; //name
    private int prio; //priority


    public Paziente(String k, int i) {
    nome=k;
    prio=i;
    }

    public int compareTo(Paziente x){
    if (this.prio>x.prio) return 1;
    if (this.prio<x.prio) return -1;
    else {int comp=this.nome.compareTo(x.nome);
    if (comp>0) return 1;
    if (comp<0) return -1;
    else return 0;
    }
    }
    }

    can u tell me where i'm doing wrong? Thx in advantage

    Elio

  2. #2
    Join Date
    Aug 2003
    Posts
    313
    So, this problem is a sneeky one. The definition of the Comparable interface is:
    Code:
    public interface Comparable {
      public int compareTo(Object o);
    }
    That means that the parameter that you have to accept is an Object, not a Paziente; normally at the beginning of the method you would say something like:
    Code:
    if( !(x instanceof Paziente) ) {
      return -1;
    }
    Or something like that.

    That being said, if you are using Java 1.5 (or 1.4 with jsr14) then you willl be able to use the generic signiture. In that case, Comparable is defined as:
    Code:
    public interface Comparable<T> {
      public int compareTo(T o);
    }
    And you will simply be able to change your class definition to:
    Code:
    public class Paziente implements Comparable<Paziente> {
    ....
    }
    Generics are great, they garantee more type safety (though they aren't perfect). Anyways, I hope this helps.
    ~evlich

  3. #3
    Join Date
    Jun 2005
    Posts
    3
    Thx evlich very much for the help! I got the error! But now the problem is another: I have to make a comparation with 2 Paziente objects, at first comparing the int value and then, if they're ==, comparing the String values. Ex: I can't make a comparation between a this.nome and a o.object because o is an Object but not a Paziente...
    In other classes of the same project (it's for school) i must use Paziente as a Comparable...
    Maybe there is another way, just give me a hint...
    If you need the other class OrderedList (that contains the methods i must use, all of them using Comparable) just tell me and I'll post here (I don't post it now because it's a little long).
    Thx in advantage and thx to evlich for the hint
    Ps: I also tried to use generics but they don't work on my system and at school (it's an informatic university) we've never used them...maybe they'll be useful...

  4. #4
    Join Date
    Aug 2003
    Posts
    313
    If you are using java 1.5 then generics are definately the way to go, but if you arent' then you have to deal with a little bit of non-type safety. Here's an example for comparing money amounts, your code should look very similar:
    Code:
    public class Currency implements Comparable {
      private int _dollars;
      private int _cents;
    
      public Currency(int d, int c) {
        _dollars = d; _cents = c;
      }
    
      public int compareTo(Object o) {
         if( !(o instanceof Currency) ) {
            return -1;
         }
         Currency c= (Currency) o;
         return (_dollars * 100 + _cents) - (c._dollars * 100 + c._cents);
      }
    
      public static void main(String[] args) {
        System.out.println(new Currency(1,5).compareTo(new Currency(2,5)));
      }
    }
    Now if you want to use generics :
    Code:
    public class Currency implements Comparable<Currency> {
      private int _dollars;
      private int _cents;
    
      public Currency(int d, int c) {
        _dollars = d; _cents = c;
      }
    
      public int compareTo(Currency c) {
         return (_dollars * 100 + _cents) - (c._dollars * 100 + c._cents);
      }
    
      public static void main(String[] args) {
        System.out.println(new Currency(1,5).compareTo(new Currency(2,5)));
      }
    }
    A note about generics, you will have to declare your sorted set as parameterized, so if you didn't write it, and it doesn't support generics then you will have to use the first (non-generic) method. The generic form of the declaration would be:
    Code:
    OrderedList<Currency> li = new OrderedList<Currency>();
    I hope this helps.
    ~evlich

  5. #5
    Join Date
    Jun 2005
    Posts
    3
    Allright! Now the class is ok! Thx a lot! The tester is going mad, but I think I can solve it!
    Thx again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center