public class Point { public float x; public float y; public float z; public Point(int inX, int inY, int inZ) { x = inX; y = inY; z = inZ; } } In the client code, ArrayList is created, and 3 points are added to it: import java.util.ArrayList; import java.util.Collections; public class Lab6Main { public static void main(String[] args) { ArrayList Points = new ArrayList(); Points.add(new Point(1, 2, 3)); Points.add(new Point(0, 0, 0)); Points.add(new Point(0, 34, 68)); //Collections.sort(Points); } } 1. Create 2 files – Point.java and Lab6Main.java for Point class and client code; copy the code. Compile and run the project.

Respuesta :

Answer:

/*Lab6Main.java */

import java.util.ArrayList;

import java.util.Collections;

public class Lab6Main {

   public static void main(String[] args) {

       ArrayList<Point> Points = new ArrayList<Point>();

       Points.add(new Point(1, 2, 3));

       Points.add(new Point(0, 0, 0));

       Points.add(new Point(0, 34, 68));

       Points.add(new Point(0, 34, 68));

       Collections.sort(Points, new NewComparator());

       for (int i=0;i < Points.size();i++){

           System.out.println(Points.get(i).x + " " + Points.get(i).y + " " + Points.get(i).z);

       }

   }

}

/*Point.java */

public class Point {

   public float x;

   public float y;

   public float z;

   public Point(int inX, int inY, int inZ)

   {

       this.x = inX;

       this.y = inY;

       this.z = inZ;

   }

}

/*NewComparator.java */

import java.util.Comparator;

class NewComparator implements Comparator<Point> {

   @Override

   public int compare(Point point01, Point point02) {

       if(point01.x != point02.x)

           return (int)(point01.x - point02.x);

       else if(point01.y != point02.y)

           return (int)(point01.y - point02.y);

       else if(point01.z != point02.z)

           return (int)(point01.z - point02.z);

       /*No Swap Required */

       return 0;

   }

}

Explanation:

ACCESS MORE
EDU ACCESS