-2

I want to write a method that compares the attributes of two objects from the same class. Say I'v got point1 & point2 of the Point class, which have the attributes of:

public class Point{
    private double x;
    private double y;
    private double z;

[...] */constructor and methods*/[...]
}

public static void main (String[] args){
    Point point1 = new Point(5, 10, 20);
    Point point2 = new Point(0, 5, 10); 
}

I want to compare the x, y, and z values of point1 & point2 against each other but I have no idea how to do so. How would I differentiate between the different values in the code block of a method? This is the theoretical method I would write if I could:

public double comparePoints(Point a, Point b){
    if (x1 < x2){
        System.out.println("Point b has the bigger x-value");
        return x2;
    }
    etc.
}

Any ideas how to do this?

Play4Fun
  • 3
  • 3

2 Answers2

0

if you want to get one of the attributes of on object, you have two choices :

1/ using getter methods : a.getAttributeX(Point) { ...}

2/ once creating an object Point you would be able to use this: if(a.x < b.x) then....

PS: It's always safe to use getter methods !

Oox Hamza
  • 9
  • 1
  • 7
0

You have already got good answers to your questions, i.e comparing using the comparator interface, using getter\setter to safely access the properties when they are declared private or access the property from each of the objects using obj1.x and obj2.x. It depends on the exact requirement what approach you want to take. The following piece of code that i have updated does a basic comparison of the attributes of two objects.

public class Point {    

        private double x;
        private double y;
        private double z;    

        //Constructor
        Point(double l,double m,double n){

            this.x = l;
            this.y = m;
            this.z = n;         

        }

    //main method
    public static void main (String[] args){

        Point point1 = new Point(5, 10, 20);

        Point point2 = new Point(0, 5, 10); 

       //Object that is created with the greater value
        Point point3 = comparePoints(point1,point2);

        System.out.println("-----The greater value of property, "
                + "between the two compared objects is as below----");

        System.out.println("x ="+point3.x);
        System.out.println("y ="+point3.y);
        System.out.println("z ="+point3.z);

    }


    //Compare method declared static, for the satic main method to access
    public static Point comparePoints(Point a, Point b){    

        System.out.println("Values in Object a = "+a.x+""+a.y+""+a.z);

        System.out.println("Values in Object a = "+b.x+""+b.y+""+b.z);

        System.out.println("Point a has the bigger x-value");

        Point h = new Point(0,0,0);     

        if (a.x < b.x){
            h.x = b.x;
        }
        else{
            System.out.println("Point a has the bigger x-value");
            h.x = a.x;
        }

        if (a.y < b.y){
            System.out.println("Point b has the bigger y-value");
            h.y = b.y;
        }
        else{
            System.out.println("Point a has the bigger y-value");
            h.y = a.y;
        }

        if (a.z < b.z){
            System.out.println("Point b has the bigger z-value"+b.z);
            h.z = b.z;
        }
        else{
            System.out.println("Point a has the bigger z-value"+a.z);
            h.z = a.z;
           }

        return h;

    }

}
Fabich
  • 2,768
  • 3
  • 30
  • 44
shalinir
  • 26
  • 5