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

Use Java Language. // A StringSet is a collection of non-null strings, with no d

ID: 3730858 • Letter: U

Question

Use Java Language.

// A StringSet is a collection of non-null strings, with no duplicates

// (i.e., no two elements may be equal).  

public class StringSet {

private

public StringSet() {

}

  

// Throws an IllegalArgumentException if e is null, otherwise adds

// e to the set if there is not already an element in the set equal

// to e

public void insert(String e) {

}

  

// Throws an IllegalArgumentException if e is null, otherwise

// indicates whether the set contains e

public boolean contains(String e) {

}

Explanation / Answer

import java.util.ArrayList; import java.util.List; // A StringSet is a collection of non-null strings, with no duplicates // (i.e., no two elements may be equal). public class StringSet { private List set; public StringSet() { set = new ArrayList(); } // Throws an IllegalArgumentException if e is null, otherwise adds // e to the set if there is not already an element in the set equal // to e public void insert(String e) { if(!contains(e)) { set.add(e); } } // Throws an IllegalArgumentException if e is null, otherwise // indicates whether the set contains e public boolean contains(String e) { if(e == null) { throw new IllegalArgumentException("can not add null strings"); } return set.contains(e); } }
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