title: How do I compare 2 objects with 1 parameter?
body: <p>I made a java program that compares the distance between 2 objects</p>
  <p>I managed to figure out how to solve it when I make a class with 2 parameters and compare these with the formula seen below;</p>
  <pre><code>public class RasterServices {
  
  public static double distance (SimpleRasterElement a, SimpleRasterElement b) {
      
      double d;
      
      d = Math.sqrt(((b.x-a.x)*(b.x-a.x)) + ((b.y-a.y)*(b.y-a.y)));
  
      return d;
  }
  </code></pre>
  <ul>
  <li></li>
  </ul>
  <pre><code>public class SimpleRasterElement {
        public int id;
        public double x;
        public double y;
        public double height;
  }
  </code></pre>
  <ul>
  <li></li>
  </ul>
  <pre><code>public class SimpleRasterElementTest {
  
      public static void main (String[] args)
      {
          SimpleRasterElement a, b ;  // Deklarera variabeln
          
          a = new SimpleRasterElement (); // Skapa en instans (med ’new’),
          b = new SimpleRasterElement (); 
          
          // Tilldela variablerna i ’a’ värden:
          a.id = 1;
          a.x = 6.0;
          a.y = 8.0;
          a.height = 10.5;
          
          // Tilldela variablerna i ’b’ värden:
          b.id = 1;
          b.x = 9.0;
          b.y = 12.0;
          b.height = 15.5;
  
          System.out.println (RasterServices.distance(a,b));
          }
  }
  </code></pre>
  <p>I can then test this via using my test program <code>RasterServices.distance(a,b)</code></p>
  <p>But now I want to make my variables private, use getters and create the distance() method within the RasterElement-class, and now I'm hardstuck.</p>
  <pre><code>public class RasterElementTest {
  
  public static void main(String[] args) {
      
      RasterElement re_a = new RasterElement(1, 6.0, 8.0, 10.5);
      RasterElement re_b = new RasterElement(1, 9.0, 12.0, 15.5);
      
      double d = re_a.distance(re_b);
  
      System.out.println(d);
  
      }
  }
  </code></pre>
  <ul>
  <li></li>
  </ul>
  <p>asd</p>
  <pre><code>public class RasterElement {
  private final int id;
  private final double x;
  private final double y;
  private final double height;
  
  public RasterElement (int id_nr, double x_val, double y_val, double height_val) {
      id = id_nr;
      x = x_val;
      y = y_val;
      height = height_val;
  }
  
  public int getId () {
      return id;
  }
  
  public double getX () { 
      return x;
  }
  
  public double getY () { 
      return y;
  }
  
  public double getHeight () { 
      return height;
  }
  
  public double distance (RasterElement a) {
      
      double d;
      
      d = Math.sqrt(((b.getX()-a.getX())*(b.getX()-a.getX())) + ((b.getY()-a.getY())*b.getY()-a.getY()));
  
      return d;
      }
  }
  </code></pre>
  <p>But here in distance() i'm only allowed ONE parameter, can someone please explain to me how I can compare two elements/objects when I'm only allowed one parameter in the function?</p>
  <p>(By using re_a.distance(re_b); in my test-code)</p>
  <p>Thanks, sorry for the long post B-)</p>
  <p>(how do I get the b-value into the equation in the method distance in the class RasterElement..?)</p>