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: 3730863 • 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

// removes e from the set

public void remove(String e) {

}

  

// Returns a formatted string version of this set

// Examples: If set contains "a" and "b", this method should

// return the string "{a, b}". If the set is empty, this

// method should return the string "{}".

public String toString() {

return null;

}

}  

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); } // Throws an IllegalArgumentException if e is null, otherwise // removes e from the set public void remove(String e) { if(contains(e)) { set.remove(e); } } // Returns a formatted string version of this set // Examples: If set contains "a" and "b", this method should // return the string "{a, b}". If the set is empty, this // method should return the string "{}". public String toString() { String result = "{"; for(int i = 0; i
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