#Objects

9 messages · Page 1 of 1 (latest)

shrewd tulip
#

i need help getting the 'x' value out of an object:

class SortablePointd implements Comparable {
    double x, y;
  // Constructor:
  public SortablePointd (Pointd p, boolean sortByX){
    this.x = p.x;
    this.y = p.y;
  }

  public int compareTo (Object obj)
  {
    // ... Do the comparison based on whether it's by X or Y ...
    System.out.println(obj.getClass());
    System.out.println(obj.x);

  } 
}

on the last line, it gives an error because I can't do obj.x? so how would i get the x value in obj?
obj is a SortablePointd btw

tall daggerBOT
#

Hey, @shrewd tulip!
Please remember to /close this post once your question has been answered!

somber needle
shrewd tulip
#

i tried using obj.getx() but that also doesn't work

somber needle
#

you need to create the method first

#

additionally it is not guaranteed that the object passed to compare to will be an object of type SortablePointd

wispy anchor
#

first you have to cast your object

#

either cast, or put inside compareTo paremeter