Ron Paul
Ron Paul

Reputation: 183

compareTo() method java is acting weird

hi im having trouble getting this to work im getting an error here with my object comparison...how could I cast the inches to a string ( i never used compare to with anything other than strings) , or use comparison operators to compare the intigers,

Object comparison = this.inches.compareTo(obj.inches);

here is my code so far

import java.io.*;
import java.util.*;
import java.lang.Integer;
import java.lang.reflect.Array;

 public class Distance implements Comparable<Distance> {

private static final String HashCodeUtil = null;
private int feet;
private int inches; 
private final int DEFAULT_FT = 1;
private final int DEFAULT_IN = 1;

public Distance(){
    feet = DEFAULT_FT;
    inches = DEFAULT_IN;
}
public Distance(int ft, int in){
    feet = ft;
    inches = in;
}

public void setFeet(int ft){
    try {
        if(ft<0){
            throw new CustomException("Distance is not negative");

        }

    }
    catch(CustomException c){
        System.err.println(c);
        feet =ft;
    }
}

public int getFeet(){
    return feet;
}

public void setInches(int in){

    try
    {
        if (in<0)
            throw new CustomException("Distance is not negative");
        //inches = in;
    }
    catch(CustomException c)
    {
        System.err.println(c);
        inches = in;
    }

}

public int getInches(){
    return inches;
}


public String toString (){
    return "<" + feet + ":" + inches + ">";

}

public Distance add(Distance m){
    Distance n = new Distance();
    n.inches = this.inches + m.inches;
    n.feet = this.feet + m.feet;

    while(n.inches>12){
        n.inches = n.inches - 12;
        n.feet++;
    }

    return n;
}

public Distance subtract(Distance f){
    Distance m = new Distance();
    m.inches = this.inches - f.inches;
    m.feet = this.feet - f.feet;
    while(m.inches<0){
        m.inches = m.inches - 12;
        feet--;

    }

    return m;
}


@Override
public int compareTo(Distance obj) {
    // TODO Auto-generated method stub

    final int BEFORE = -1;
    final int EQUAL = 0;
    final int AFTER = 1;

    if (this == obj) return EQUAL;

    if(this.DEFAULT_IN < obj.DEFAULT_FT) return BEFORE;
    if(this.DEFAULT_IN > obj.DEFAULT_FT) return AFTER;

    Object comparison = this.inches.compareTo(obj.inches);
    if (this.inches == obj.inches) return compareTo(null);

    assert this.equals(obj) : "compareTo inconsistent with equals";

    return EQUAL;

}

@Override public boolean equals( Object obj){
    if (obj != null) return false;
    if (!(obj intanceof Distance)) return false;

    Distance that = (Distance)obj;

            ( this.feet == that.feet &&
            this.inches == that.inches);
            return true;
            else
            return false;
}


 @Override public int hashCode(int, int) {
    int result = HashCodeUtil.inches;
    result = HashCodeUtil.hash(result, inches );
    result = HashCodeUtil.hash(result, feet);
    ruturn result;
   }

Upvotes: 2

Views: 1714

Answers (2)

pb2q
pb2q

Reputation: 59607

With this line:

Object comparison = this.inches.compareTo(obj.inches);

you are trying to dereference an int, a primitive type. The compiler should be giving you an error: you can only dereference Objects using the dot .

I'm not sure what you want this compareTo code to do, but it is at this point, to compare primitive types, that you should be using ==:

 if (this.inches == obj.inches) return compareTo(null);

Be aware that in this line: if (this == obj) return EQUAL; you are comparing object references, which might or might not be what you want. Since your class doesn't override the equals method, this comparison is equivalent to this.equals(obj).

Upvotes: 1

KV Prajapati
KV Prajapati

Reputation: 94645

You're comparing object references. Try to compare object value; either override hashCode() or compare field values.

@Override
public int compareTo(Distance obj) {
    ....
    if (this == obj) return EQUAL; <--- This
    ...
}

Upvotes: 1

Related Questions