Please help me with this!! package student_classes; import java.util.ArrayList;
ID: 658250 • Letter: P
Question
Please help me with this!!
package student_classes;
import java.util.ArrayList;
/**
* "Sets" are unordered collections of unique objects, called "elements." Note, this
* means, among other things, that each element occurs only once in a set.
* <br>
*
* @author UMD CS Department
*
* @param <T>
*/
public class UnorderedSet<T> implements ISet<T> {
/*
* Do NOT delete or change this instance variable.
*/
/**
* Note: we use the protected visibility class to enable subclasses,
* such as OrderedSet, to access this instance variable directly while
* keeping it private to outsiders.
*/
protected ArrayList< T > elements=null;
public UnorderedSet() { // only empty sets are constructed.
this.elements = new ArrayList<T>();
}
/**
* You must implement a copy-constructor that makes a shallow copy of the otherSet's elements.
* @param otherSet
*/
public UnorderedSet( ISet otherSet ) {
throw new UnsupportedOperationException( "implement this method!" );
}
/**
* You will need this "private" constructor to use ArrayLists that are created as a result of
* combining sets into a "new" set.
*
* <P>
* Note: this constructor MUST add each element from the <code>elements</code>
* parameter into this objects's <code>elements</code>.
* <ol>
* <li>Make sure that the internal ArrayList has been created</li>
* <li><b>Copy</b> objects from the <elements> into the newly-created
* ArrayList, in order.
* </ol>
* In other words: as a result
* of calling this constructor, a new Set is created whose elements are the result
* of shallow copying the objects that reside in the <code>element</code> argument.
* @param elements
*/
private UnorderedSet( ArrayList<T> elements ) {
throw new UnsupportedOperationException( "implement this method!" );
}
/*
* Public methods: you need to implement these.
*/
/* (non-Javadoc)
* @see student_classes.ISet#isEmpty()
*/
@Override
public boolean isEmpty() {
throw new UnsupportedOperationException( "implement this method!" );
}
/* (non-Javadoc)
* @see student_classes.ISet#adjoin(T)
*/
@Override
public boolean adjoin( T element ) {
throw new UnsupportedOperationException( "implement this method!" );
}
/* (non-Javadoc)
* @see student_classes.ISet#size()
*/
@Override
public int size() {
throw new UnsupportedOperationException( "implement this method!" );
}
/* (non-Javadoc)
* @see student_classes.ISet#isSubset(student_classes.ISet)
*/
@Override
public boolean isSubset( ISet<T> other ) {
throw new UnsupportedOperationException( "implement this method!" );
}
/* (non-Javadoc)
* @see student_classes.ISet#isMember(T)
*/
@Override
public boolean isMember( T element ) {
throw new UnsupportedOperationException( "implement this method!" );
}
/* (non-Javadoc)
* @see student_classes.ISet#union(student_classes.ISet)
*/
@Override
public ISet<T> union( ISet<T> otherSet ) {
throw new UnsupportedOperationException( "implement this method!" );
}
/* (non-Javadoc)
* @see student_classes.ISet#intersect(student_classes.ISet)
*/
@Override
public ISet<T> intersect( ISet<T> other ) {
throw new UnsupportedOperationException( "implement this method!" );
}
/* (non-Javadoc)
* @see student_classes.ISet#difference(student_classes.ISet)
*/
@Override
public ISet<T> difference( ISet<T> other ) {
throw new UnsupportedOperationException( "implement this method!" );
}
/*
* Overrides
*/
/**
* You are free to implement any strategy that makes sense for you here.
*/
public String toString() {
throw new UnsupportedOperationException( "implement this method!" );
}
/**
* Two (unordered) sets are equal when they contain exactly the same elements, irrespective
* of their order. Another way of thinking of this: S equals T iff S is a subset of T and
* T is a subset of S.
*
* In your implementation, you need to ensure that <code>other</code> is an instance
* of the appropriate set, i.e., UnorderedSet in this case.
*/
public boolean equals( Object other ) {
throw new UnsupportedOperationException( "implement this method!" );
}
}
Explanation / Answer
Below is the completed code:-
Note: The code assumes you have interface ISet present with you (not provided in this question). It will not compile without the ISet interface
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
/**
* "Sets" are unordered collections of unique objects, called "elements." Note, this
* means, among other things, that each element occurs only once in a set.
* <br>
*
* @author UMD CS Department
*
* @param <T>
*/
public class UnorderedSet<T> implements ISet<T> {
/*
* Do NOT delete or change this instance variable.
*/
/**
* Note: we use the protected visibility class to enable subclasses,
* such as OrderedSet, to access this instance variable directly while
* keeping it private to outsiders.
*/
protected ArrayList< T > elements=null;
public UnorderedSet() { // only empty sets are constructed.
this.elements = new ArrayList<T>();
}
/**
* You must implement a copy-constructor that makes a shallow copy of the otherSet's elements.
* @param otherSet
*/
public UnorderedSet( ISet otherSet ) {
if(!(elements=otherSet.elements)){
throw new UnsupportedOperationException( "implement this method!" );
}
}
/**
* You will need this "private" constructor to use ArrayLists that are created as a result of
* combining sets into a "new" set.
*
* <P>
* Note: this constructor MUST add each element from the <code>elements</code>
* parameter into this objects's <code>elements</code>.
* <ol>
* <li>Make sure that the internal ArrayList has been created</li>
* <li><b>Copy</b> objects from the <elements> into the newly-created
* ArrayList, in order.
* </ol>
* In other words: as a result
* of calling this constructor, a new Set is created whose elements are the result
* of shallow copying the objects that reside in the <code>element</code> argument.
* @param elements
*/
private UnorderedSet( ArrayList<T> elements ) {
int t=0;
for(T e:elements){
this.elements.add(e);
t=1;
}
if(t==0)
throw new UnsupportedOperationException( "implement this method!" );
}
/*
* Public methods: you need to implement these.
*/
/* (non-Javadoc)
* @see student_classes.ISet#isEmpty()
*/
@Override
public boolean isEmpty() {
if(elements.size()==0){
return true;
}else{
throw new UnsupportedOperationException( "implement this method!" );
return false;
}
}
/* (non-Javadoc)
* @see student_classes.ISet#adjoin(T)
*/
@Override
public boolean adjoin( T element ) {
return Set.adjoin(element, this);
throw new UnsupportedOperationException( "implement this method!" );
}
/* (non-Javadoc)
* @see student_classes.ISet#size()
*/
@Override
public int size() {
return elements.size();
throw new UnsupportedOperationException( "implement this method!" );
}
/* (non-Javadoc)
* @see student_classes.ISet#isSubset(student_classes.ISet)
*/
@Override
public boolean isSubset( ISet<T> other ) {
if(this.elements.containsAll(other.elements)){
return true;
}
return false;
throw new UnsupportedOperationException( "implement this method!" );
}
/* (non-Javadoc)
* @see student_classes.ISet#isMember(T)
*/
@Override
public boolean isMember( T element ) {
if(this.elements.contains(element)){
return true;
}
return false;
throw new UnsupportedOperationException( "implement this method!" );
}
/* (non-Javadoc)
* @see student_classes.ISet#union(student_classes.ISet)
*/
@Override
public ISet<T> union( ISet<T> otherSet ) {
return Set.union(otherset, this);
throw new UnsupportedOperationException( "implement this method!" );
}
/* (non-Javadoc)
* @see student_classes.ISet#intersect(student_classes.ISet)
*/
@Override
public ISet<T> intersect( ISet<T> other ) {
return Set.intersect(otherset, this);
throw new UnsupportedOperationException( "implement this method!" );
}
/* (non-Javadoc)
* @see student_classes.ISet#difference(student_classes.ISet)
*/
@Override
public ISet<T> difference( ISet<T> other ) {
return Set.diff(otherset, this);
throw new UnsupportedOperationException( "implement this method!" );
}
/*
* Overrides
*/
/**
* You are free to implement any strategy that makes sense for you here.
*/
public String toString() {
String toString="";
for(T e: elements){
toString+=e+" ";
}
return toString;
throw new UnsupportedOperationException( "implement this method!" );
}
/**
* Two (unordered) sets are equal when they contain exactly the same elements, irrespective
* of their order. Another way of thinking of this: S equals T iff S is a subset of T and
* T is a subset of S.
*
* In your implementation, you need to ensure that <code>other</code> is an instance
* of the appropriate set, i.e., UnorderedSet in this case.
*/
public boolean equals( Object other ) {
if(this.elements.contains(other) && other.contains(this.elements)){
return true;
}
return false;
throw new UnsupportedOperationException( "implement this method!" );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.