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...
Create a Java class named Trivia that contains three instance variables, question of type String that...
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question. Also create the following methods: getQuestion( ) – it will return the question. getAnswer( ) – it will return the answer. getPoints( ) – it will...
Create a Java class named Trivia that contains three instance variables, question of type String that...
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question. Also create the following methods: getQuestion( ) – it will return the question. getAnswer( ) – it will return the answer. getPoints( ) – it will...
Design a class named Student that contains the followingprivate instance variables: A string data field name...
Design a class named Student that contains the followingprivate instance variables: A string data field name for the student name. An integer data field id for the student id. A double data field GPA for the student GPA. An integer data field registeredCredits. It contains the following methods: An empty default constructor. A constructor that creates a student record with a specified id and name. The get and set methods for id, name, GPA, and registeredCredits. A method called registerCredit...
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 conplete class named patient. the patient class shouls include teo private instance variables...
java: create a conplete class named patient. the patient class shouls include teo private instance variables named PatientID of type int lastName of type string. include all the parts of a well-formed class described. set the instance variable defaults to appropriate values.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT