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

Repost Tree Classs import java.util.ArrayList; import java.util.Collections; imp

ID: 3918820 • Letter: R

Question

Repost Tree Classs

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;


public class RepostTree {

    /** The String to be used as a separator in toString() */
    public static final String SEPARATOR = " - ";

    /** The String that marks the start of children in toString() */
    public static final String START_CHILDREN_DELIMITER = "[";

    /** The String that divides children in toString() */
    public static final String DELIMITER = ", ";

    /** The String that marks the end of children in toString() */
    public static final String END_CHILDREN_DELIMITER = "]";

    /** The String that is the space increment in toStringVerbose() */
    public static final String VERBOSE_SPACE_INCREMENT = " ";

    /** The person at the root of this RepostTree.
     * This is the original poster, who first create the content.
     * root is non-null.
     * All Person's in a RepostTree have different names. There are no duplicates
     */
    private Person root;

    /** The immediate children of this RepostTree node.
     * Each element of children saw the post from the person at this node.
     * root is non-null but will be an empty set if this is a leaf. */
    private Set children;

    /** Constructor: a new RepostTree with root p and no children.
     * Throw an IllegalArgumentException if p is null. */
    public RepostTree(Person p) throws IllegalArgumentException {
        if (p == null)
            throw new IllegalArgumentException("Can't construct RepostTree with null root");
        root= p;
        children= new HashSet<>();
    }

    /** Constructor: a new RepostTree that is a copy of tree p.
     * Tree p and its copy have no node in common (but nodes can share a Person).
     * Throw an IllegalArgumentException if p is null. */
    public RepostTree(RepostTree p) throws IllegalArgumentException {
        if (p == null)
            throw new IllegalArgumentException("Can't construct RepostTree as copy of null");
        root= p.root;
        children= new HashSet<>();

        for (RepostTree rt : p.children) {
            children.add(new RepostTree(rt));
        }
    }

    /** Return the person that is at the root of this RepostTree */
    public Person getRoot() {
        return root;
    }

    /** Return the number of direct children of this RepostTree */
    public int getChildrenCount() {
        return children.size();
    }

    /** Return a COPY of the set of children of this RepostTree. */
    public Set getChildren() {
        return new HashSet<>(children);
    }

How do you write the function for this? (java)

/** Return true iff this is equal to ob.
     * If ob is not a RepostTree, it cannot be equal to this RepostTree, return false.
     * Two RepostTrees are equal if they are the same object (==) or:
     *
- they have the same root Person object (==)
     *
- their children sets are the same size:
     *
--their children sets are equal, which, since their sizes
     *
--are equal, requires:
     *
--- for every RepostTree rt in one set there is a RepostTree rt2
     *            in the other set for which rt.equals(rt2) is true.
     *


     * Otherwise the two RepostTrees are not equal.
     * Do not use any of the toString functions to write equals(). */
    public boolean equals(Object ob) {
        //TODO 7
        // Hint about checking whether each child of one tree equals SOME
        // other tree of the other tree's children.
        // First, you have to check them all until you find an equal one (or
        // return false if you don't.)
        // Second, you know that a child of one tree cannot equal more than one
        // child of another tree because the names of Person's are all unique;
        // there are no duplicates.

        return true;

    }

Explanation / Answer

Please find the modified code below.

CODE

=================

import java.util.ArrayList;

import java.util.Collections;

import java.util.HashMap;

import java.util.HashSet;

import java.util.Iterator;

import java.util.List;

import java.util.Set;

public class RepostTree {

   /** The String to be used as a separator in toString() */

   public static final String SEPARATOR = " - ";

   /** The String that marks the start of children in toString() */

   public static final String START_CHILDREN_DELIMITER = "[";

   /** The String that divides children in toString() */

   public static final String DELIMITER = ", ";

   /** The String that marks the end of children in toString() */

   public static final String END_CHILDREN_DELIMITER = "]";

   /** The String that is the space increment in toStringVerbose() */

   public static final String VERBOSE_SPACE_INCREMENT = " ";

   /** The person at the root of this RepostTree.

   * This is the original poster, who first create the content.

   * root is non-null.

   * All Person's in a RepostTree have different names. There are no duplicates

   */

   private Person root;

   /** The immediate children of this RepostTree node.

   * Each element of children saw the post from the person at this node.

   * root is non-null but will be an empty set if this is a leaf. */

   private Set<RepostTree> children;

   /** Constructor: a new RepostTree with root p and no children.

   * Throw an IllegalArgumentException if p is null. */

   public RepostTree(Person p) throws IllegalArgumentException {

       if (p == null)

           throw new IllegalArgumentException("Can't construct RepostTree with null root");

       root= p;

       children= new HashSet<>();

   }

   /** Constructor: a new RepostTree that is a copy of tree p.

   * Tree p and its copy have no node in common (but nodes can share a Person).

   * Throw an IllegalArgumentException if p is null. */

   public RepostTree(RepostTree p) throws IllegalArgumentException {

       if (p == null)

           throw new IllegalArgumentException("Can't construct RepostTree as copy of null");

       root= p.root;

       children= new HashSet<>();

       for (RepostTree rt : p.children) {

           children.add(new RepostTree(rt));

       }

   }

   /** Return the person that is at the root of this RepostTree */

   public Person getRoot() {

       return root;

   }

   /** Return the number of direct children of this RepostTree */

   public int getChildrenCount() {

       return children.size();

   }

   /** Return a COPY of the set of children of this RepostTree. */

   public Set getChildren() {

       return new HashSet<>(children);

   }

   /* (non-Javadoc)

   * @see java.lang.Object#hashCode()

   */

   @Override

   public int hashCode() {

       final int prime = 31;

       int result = 1;

       result = prime * result + ((children == null) ? 0 : children.hashCode());

       result = prime * result + ((root == null) ? 0 : root.hashCode());

       return result;

   }

   /** Return true iff this is equal to ob.

   * If ob is not a RepostTree, it cannot be equal to this RepostTree, return false.

   * Two RepostTrees are equal if they are the same object (==) or:

   *

- they have the same root Person object (==)

   *

- their children sets are the same size:

   *

--their children sets are equal, which, since their sizes

   *

--are equal, requires:

   *

--- for every RepostTree rt in one set there is a RepostTree rt2

   * in the other set for which rt.equals(rt2) is true.

   *

   * Otherwise the two RepostTrees are not equal.

   * Do not use any of the toString functions to write equals(). */

   public boolean equals(Object obj) {

       //TODO 7

       // Hint about checking whether each child of one tree equals SOME

       // other tree of the other tree's children.

       // First, you have to check them all until you find an equal one (or

       // return false if you don't.)

       // Second, you know that a child of one tree cannot equal more than one

       // child of another tree because the names of Person's are all unique;

       // there are no duplicates.

       if (this == obj) {

           return true;

       }

       if (obj == null) {

           return false;

       }

       if (!(obj instanceof RepostTree)) {

           return false;

       }

       RepostTree other = (RepostTree) obj;

       if (children == null) {

           if (other.children != null) {

               return false;

           }

       } else if(children.size() != other.children.size()) {

           return false;

       } else {

           for(RepostTree rt : children) {

               if(!other.children.contains(rt)) {

                   return false;

               }

           }

       }

       if (root == null) {

           if (other.root != null) {

               return false;

           }

       } else if (!root.equals(other.root)) {

           return false;

       }

       return true;

   }

}

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