Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider the following class Point: public class Point { public float x; public float y; public float z; public Point( int inX, int inY, int

Consider the following class Point:

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.

2. 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)

---------------------------------------------------------------------------------------

I do not understand how to compare the coordinates. i've been trying t figure this out for an hour. I'm stuck

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Understanding Oracle APEX 5 Application Development

Authors: Edward Sciore

2nd Edition

1484209893, 9781484209899