The Artima Developer Community
Sponsored Link

Java Answers Forum
Comparable Class (overiding)

1 reply on 1 page. Most recent reply: Mar 5, 2002 1:41 PM by Alain Ravet

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Chirs

Posts: 1
Nickname: chris
Registered: Mar, 2002

Comparable Class (overiding) Posted: Mar 5, 2002 11:57 AM
Reply to this message Reply
Advertisement
I need help on how to override the Comparable class. I need to have a compareTo method in this class and it should compare two objects.


Alain Ravet

Posts: 19
Nickname: aravet
Registered: Feb, 2002

Re: Comparable Class (overiding) Posted: Mar 5, 2002 1:41 PM
Reply to this message Reply
You must first have a rule to compare 2 objects of the same class.

Example :
Pixel (1,2) < Pixel (2,3) < Pixel (1,5)
because
1+2 < 2+3 < 1+5

=>
new Pixel (2,3).compareTo (new Pixel(1,5)) < 0 ;
and
0 < new Pixel (2,3).compareTo (new Pixel(1,2)) ;



public class Pixel implements Comparable
{
    public int compareTo( Object o )
    {
        final Pixel pixel = ( (Pixel ) o );
        final int rad1 = pixel.x + pixel.y;
        final int radThis = this.x + this.y;
        return radThis - rad1 ;
    }
 
    public Pixel( int i_x , int i_y )
    {
        x = i_x;
        y = i_y;
    }
 
    public int x ;
    public int y ;
    ...
}    

Flat View: This topic has 1 reply on 1 page
Topic: How to save properly ? Previous Topic   Next Topic Topic: java applet and html

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use