In: Computer Science
CS 209 Data Structure
3.
a. Create a class named Point3D that contains 3 instance variables x, y, and z.
b. Create a constructor that sets the variables. Also, create get and set methods for each variable.
c. Create a toString() method.
d. Make Point3D implement Comparable.
Also, create a compareTo(Point3D other) method that compares based on
the x-coordinate, then y-coordinate for tiebreakers, then z-coordinate for tiebreakers.
For example, (1, 2, 5) comes before (2, 1, 4), which comes before (2, 2, 3), which comes before (2, 2, 4).
e. Create a TreeSet of Point3D and insert a few elements including duplicates.
Verify that the TreeSet removes all the duplicates.
Also verify that the TreeSet orders the elements properly.
** Compiled in Eclipse **
CODE:
import java.util.Iterator;
import java.util.TreeSet;
// Class Point3D - implements Comparable interface.
class Point3D implements Comparable<Point3D>
{
// Class variables.
int x, y, z;
// Constructor for class Point3D
public Point3D(int x, int y, int z)
{
this.x = x;
this.y = y;
this.z = z;
}
/*
* compareTo() is used to compare the current object with the specified object. It returns
* 1, if the current object is greater than the specified object.
* -1, if the current object is less than the specified object.
* 0, if the current object is equal to the specified object.
*
* Here we will compare x first. If x is greater or lesser than the other object's x,
* we will return 1 or -1 respectively.
* If x is equal, we will compare y. If y is greater or lesser than the other object's y,
* we will return 1 or -1 respectively.
* If y is also equal, we will compare z. If it is greater or lesser than the other object's z,
* we will return 1 or -1 respectively.
* If z is also equal we will return 1.
*/
public int compareTo(Point3D obj)
{
// (1) x == obj.x -> check y
if(x == obj.x)
{
// (2) y == obj.y -> check z
if(y == obj.y)
{
// (3) z == obj.z -> return 1
if(z == obj.z)
{
return 0;
}
// (2) z > obj.z -> return 1
else if(z > obj.z)
{
return 1;
}
// (2) z < obj.z -> return -1
else
{
return -1;
}
}
// (2) y > obj.y -> return 1
else if(y > obj.y)
{
return 1;
}
// (2) y < obj.y -> return -1
else
{
return -1;
}
}
// (1) x > obj.x -> return 1
else if(x > obj.x)
{
return 1;
}
// (1) x < obj.x -> return -1
else
{
return -1;
}
}
// toString() return string in the format (x, y, z)
public String toString()
{
return "(" + x + ", " + y + ", " + z + ")";
}
}
public class testTreeset
{
public static void main(String[] args)
{
// Creating object for TreeSet.
TreeSet<Point3D> ts = new TreeSet<>();
// adding the elements to the TreeSet.
ts.add(new Point3D(1, 2, 5));
ts.add(new Point3D(2, 2, 3));
// adding duplicate element.
ts.add(new Point3D(1, 2, 5));
ts.add(new Point3D(2, 2, 4));
ts.add(new Point3D(2, 1, 4));
//Traversing elements using Iterator object.
Iterator<Point3D> itr = ts.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
Screenshots:
refer this to know the proper indentations.
Sample I/O:
The elements in the TreeSet are stored in ascending order without duplicates.
Hope this Helps!
Please consider giving this answer a thumbs up.
If this answer didn't satisfies your requirements or needs modifications, please let me know in the comment section before giving a thumbs down.
Thank you! Stay Safe!!!