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

package student_classes; import java.util.ArrayList; /** * Ordered Sets implemen

ID: 658251 • Letter: P

Question

package student_classes;

import java.util.ArrayList;

/**

* Ordered Sets implement the same methods as their "unordered" counterparts, except

* that the kinds of things that they may contain must be Comparable. This is

* a classic example of where one might choose to use "inheritance" to

* save a lot of extra (unnecessary) method implementation.

* @author UMD CS Department

*

* @param <T>

*/

public class OrderedSet< T extends Comparable<T >> extends UnorderedSet<T> {

/* Because "elements" is protected in the superclass, it's here, but

   * behind the scenes so we DO NOT redeclare it here.

   */

/**

   * Default ctor: creates an empty set of elements. Hint: this method

   * should consist of one line of code that uses a special operator

   * that invokes the parent's constructor with the parameters list ().

   */

   public OrderedSet() {

       throw new UnsupportedOperationException( "implement this method!" );

   }

/**

   * Constructs a new Ordered set from Other set, which

   * must be an ordered set. (Hint: the very first line of

   * code in this method should take advantage of another one

   * of Java's special operators that has the effect of calling

   * the constructor whose pattern is ()).

   * @param otherSet

   */

   public OrderedSet( OrderedSet<T> otherSet ) {

       throw new UnsupportedOperationException( "implement this method!" );

   }

/**

   * Adds <code>element</code> in its proper order to this set (remember, the

   * elements are arranged in ascending order) only if

   * it is NOT already in the ArrayList. Recall, this method returns true only if

   * the element is added; false otherwise.

   */

@Override

   public boolean adjoin(T element) {

       throw new UnsupportedOperationException( "implement this method!" );

   }

/**

   * Note: you must override the equals method for the

   * Ordered Set (which is a subclass of the Unordered Set)

   * because two Ordered sets are equal when they contain the

   * same elements AND those elements appear in the same order.

   * Don't forget to check the the <code>other</code> is an

   * instance of the OrderedSet class before comparing elements.

   */

@Override

   public boolean equals( Object other ) {

       throw new UnsupportedOperationException( "implement this method!" );

   }

}

Explanation / Answer

package student_classes;

import java.util.ArrayList;
import java.util.Collection;
public class OrderedSet< T extends Comparable<T >> extends UnorderedSet<T> {
public OrderedSet( OrderedSet<T> otherSet ) {
throw new UnsupportedOperationException();
}
//constructor is here
public OrderedSet( OrderedSet<T> otherSet ) {
OrderedSet.set(otherSet);
Collections.sort(otherSet);
}
//for sorting comparator is using here   
Collections.sort(otherSet, new Comparator<otherSet>() {
@Override
public int compare(OrderedSet first, OrderedSet second)
{
return first.studentname.compareTo(second.studentname);
}
});
//adding element
@Override
public boolean adjoin(T element) {
OrderedSet.add(element);
Collections.sort(OrderedSet);
return;
}
//checking equals or not
public boolean equals( Object other ) {
if (!(other instanceof OrderedSet)) {
return false;
}
return this.studentname == ((OrderedSet)o).studentname;
}
}