Question

In: Computer Science

CS 209 Data Structure 3. a. Create a class named Point3D that contains 3 instance variables...

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.

Solutions

Expert Solution

** 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!!!


Related Solutions

CS 209 Data Structure 5. Consider the Pair class covered in class: class Pair {    ...
CS 209 Data Structure 5. Consider the Pair class covered in class: class Pair {     public A first;     public B second;     public Pair(A a, B b)     {         first = a;         second = b;     }     public void setFirst(A a)     {         first = a;     }     public A getFirst()     {         return first;     }     public void setSecond(B b)     {         second = b;     }     public...
CS 209 Data Structure 2. Create a method that takes a HashMap and returns the sum...
CS 209 Data Structure 2. Create a method that takes a HashMap and returns the sum of the keys of the HashMap. 3. Create a method that takes a HashMap and returns the sum of all keys and values of the HashMap. For example, if the input is [1=9, 3=6, 4=9, 6=8, 7=6] then the method should return 59.
CS 209 Data Structure 1. Create a method that takes an ArrayList of Integer and returns...
CS 209 Data Structure 1. Create a method that takes an ArrayList of Integer and returns a sorted copy of that ArrayList with no duplicates. Sample Input: {5, 7, 4, 6, 5, 6, 9, 7} Sample Output: {4, 5, 6, 7, 9}
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String...
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String and returns a copy of that ArrayList with no duplicates. The relative ordering of elements in the new ArrayList should be the same. Sample Input: {"qwerty", "asdfgh", "qwer", "123", "qwerty", "123", "zxcvbn", "asdfgh"} Sample Output: {"qwerty", "asdfgh", "qwer", "123", "zxcvbn"}
Java Create a Project named Chap4b 1. Create a Student class with instance data as follows:...
Java Create a Project named Chap4b 1. Create a Student class with instance data as follows: student id, test1, test2, and test3. 2. Create one constructor with parameter values for all instance data fields. 3. Create getters and setters for all instance data fields. 4. Provide a method called calcAverage that computes and returns the average test score for an object to the driver program. 5. Create a displayInfo method that receives the average from the driver program and displays...
Using C# Create a class called Artist that contains 4 pieces of information as instance variables:...
Using C# Create a class called Artist that contains 4 pieces of information as instance variables: name (datatype string), specialization – i.e., music, pottery, literature, paintings (datatype string), number of art items (datatype int), country of birth (datatype string). Create a constructor that initializes the 4 instance variables. The Artist class must contain a property for each instance variable with get and set accessors. The property for number should verify that the Artist contributions is greater than zero. If a...
Create a Java class named Package that contains the following: Package should have three private instance...
Create a Java class named Package that contains the following: Package should have three private instance variables of type double named length, width, and height. Package should have one private instance variable of the type Scanner named input, initialized to System.in. No-args (explicit default) public constructor, which initializes all three double instance variables to 1.0.   Initial (parameterized) public constructor, which defines three parameters of type double, named length, width, and height, which are used to initialize the instance variables of...
Define a class named Document that contains an instance variable of type String named text that...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value. Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message should...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT