Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class, \"Coordinate\" that implements the \"Comparable\" interface. The

ID: 3539781 • Letter: W

Question

Write a class, "Coordinate"  that implements the "Comparable" interface.  The class contains "x", "y" and "z" attributes (type "double"),  a constructor, and accessor and mutator methods for each of the attributes.  The "compareTo" method will be based on the "Coordinate" object distance from the origin of 0, 0, 0.      The formula for distance is,

distance = sqrt ( (deltaX)2+ (deltaY)2 + (deltaZ)2 ) // sqrt repreesnts the mathematical square root

where deltaY represents the difference between the Y components of each Coordinate object

where deltaZ represents the difference between the Z components of each Coordinate object

For example,

Coordinate coordinate1(1,1,1);

Coordinate coordinate2(5,6,7);

The following expression will return a negative value,

coordinate1.compareTo(coordinate2)

Save and upload the file as "Coordinate.java"

Explanation / Answer

public class Coordinate implements Comparable<Coordinate>{
double x;
double y;
double z;
public Coordinate(double x, double y, double z)
{
    this.x = x;
    this.y = y;
    this.z = z;
}
public void setX(int x)
{
    this.x = x;
}
public void setY(int y)
{
    this.y = y;
}
public void setZ(int z)
{
    this.z = z;
}
public double getX()
{
    return x;
}
public double getY()
{
    return y;
}
public double getZ()
{
    return x;
}

public int compareTo(Coordinate secondOne) {
//As the distance is from original, deltax is always equal to x
double firstDistance = Math.sqrt(x*x+y*y+z*z);
double secondDistance = Math.sqrt(secondOne.getX()*secondOne.getX()+secondOne.getY()*secondOne.getY()+secondOne.getZ()*secondOne.getZ());
if (secondDistance < firstDistance)
return 1;
if (secondDistance > firstDistance)
return -1;
else
return 0;

    }

     public static void main(String []args){
        Coordinate c1 = new Coordinate(1,1,1);
        Coordinate c2 = new Coordinate(5,6,7);
        System.out.println(c1.compareTo(c2));
     }
    
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote