That brings me to today’s questions. The code below for the Points class implements the compareTo() method. Now my manager kept insisting that there is something not right about it – but the code looks alright to me.
What do you folks think – are there any problems with the code below?
package com.twister;
import java.util.ArrayList;
import java.util.Collections;
public class Points implements Comparable<Points>{
int xCoordinate;
/*
* 1. Returns 0 if both points have same xCoordinate (say 3 & 3) - returns 3-3=0
* 2. Returns +ve if first point is on the
* right hand side of the second point (say 5, -3) - returns 5 - (-3) = 8
* 3. Returns -ve if first point is on the
* left hand side of the second point (say -5, 3) - returns (-5) - (3) = -8
*/
public int compareTo(Points p) {
return xCoordinate - p.xCoordinate;
};
public static void main(String[] args) {
ArrayList<Points> arrPoints = new ArrayList<Points>();
/* Add lots of points to the array list */
Collections.sort(arrPoints);
/*Print the sorted collection */
}
}
Got a view? Leave one here.
Just try this out:
ReplyDelete{code}
arrPoints.add(new Points(Integer.MAX_VALUE));
arrPoints.add(new Points(-3));
arrPoints.add(new Points(Integer.MIN_VALUE));
arrPoints.add(new Points(2));
Collections.sort(arrPoints);
for (Points p : arrPoints) {
System.out.println(p);
}
{code}
The output would be:
{code}
com.twister.Points@42e816 (2147483647)
com.twister.Points@9304b1 (-2147483648)
com.twister.Points@190d11 (-3)
com.twister.Points@a90653 (2)
{code}
See my previous post for problem.
ReplyDeleteSolution:
1. Quick and dirty:
{code}
public int compareTo(Points p) {
return (int) (1.0 * xCoordinate + -1.0 * p.xCoordinate);
}
{code}
The problem with the code is that the integer substraction can lead to overflow.
ReplyDeleteIf we add to the array the values:
arrPoints.add( new Points( Integer.MAX_VALUE ) );
arrPoints.add( new Points( -1 ) );
arrPoints.add( new Points( 1 ) );
arrPoints.add( new Points( Integer.MIN_VALUE ) );
the order will be [2147483647, -1, 1, -2147483648]
One way to compare two integers:
public int compareTo(Points p) {
return xCoordinate > p.xCoordinate ? 1 : xCoordinate == p.xCoordinate ? 0 : -1;
};
This code will produce incorrect result if list will contain positive number and Integer.MIN_VALUE
ReplyDelete