Uncomment the last line in main() method (Collections.sort(Points);) Try compiling and running the code again and analyze the error. Update the Point class so that sorting works properly. Note: you need to implement Comparable interface. Points should be sorted according to the following logic:

Compare x coordinates:
If they are different, return the difference;
If they are equal, compare y coordinates:
If they are different, return the difference;
If they are equal, compare z coordinates:
If they are different, return the difference;
If they are equal, objects should be equal (so you can use one difference operation to compare z)

Respuesta :

Answer:

Java code is given below

Explanation:

==================== Point.java ============================

public class Point implements Comparable<Point>{

  public float x;

  public float y;

  public float z;

 

  public Point(float x, float y, float z) {

      this.x = x;

      this.y = y;

      this.z = z;

  }

  @Override

  public int compareTo(Point point) {

      // TODO Auto-generated method stub

     

      if(this.x != point.x){

          return Double.compare(this.x, point.x);

      }

      if(this.y != point.y){

          return Double.compare(this.y, point.y);

      }

      if(this.z != point.z){

          return Double.compare(this.z, point.z);

      }

     

      return 0;

  }

  @Override

  public String toString(){

     

      return "(" + this.x + "," + this.y + "," + this.z + ")";

     

  }

 

}

===================== Main Program file ===================================

import java.util.ArrayList;

import java.util.Collections;

public class Lab6Main {

 

 

  public static void main(String[] args) {

      // TODO Auto-generated method stub

     

      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));

      Collections.sort(Points);

     

      //Added the below lines to print the output

      System.out.println(Points.get(0).toString());

      System.out.println(Points.get(1).toString());

      System.out.println(Points.get(2).toString());

  }

}

=============== Output ==========================

(0.0,0.0,0.0)

(0.0,34.0,68.0)

(1.0,2.0,3.0)

ACCESS MORE
EDU ACCESS