The CharacterSet can hold a set of lowercase alphabet characters. Define one or
ID: 3547324 • Letter: T
Question
The CharacterSet can hold a set of lowercase alphabet characters. Define one or more instance
variables to represent CharacterSet properties and hold character elements. CharacterSet
contains the following methods:
a. One or more constructors.
b. Method
public void add(char ch)
inserts a character to the set. If the character ch is uppercase letter, convert it to lowercase and add. If the character already exists in the set, ignore to add. If the character is not a letter, display an error message.
c. Method
public void delete(char ch)
deletes a character from the set. If the character is not in the set, display an error message.
d. Method
public CharacterSet union(CharacterSet x)
returns a third set that is the union of two sets.
Assuming that set1 = {a, b, d} and set2 = {b, c, y}, the union of set1 and set2 (set1 ? set1) should be {a,b,c,d,y}. Remember that the set does not have any duplicating elements.
e. Method
public CharacterSet intersection(CharacterSet x)
creates a third set that is the intersection of two sets.
Assuming that set1 = {a, b, d} and set2 = {b, c, y}, the intersection of set1 and set2 (set1 ? set1) should be {b}
f. Method
public int cardinality()
returns the number of characters in the set.
g. Method
public boolean equals(CharacterSet x)
determines if the set is equal to x.
h. Method
public String toString()
returns a string description of the set.
i. Method
public boolean isEmpty()
returns true if the set is empty.
j. Method
public boolean isSubset(CharacterSet x)
determines if the set is a subset of x.
Your test program should demonstrate CharacterSet class and its methods.
An example of TestCharacterSet.java.
CharacterSet s1 = new CharacterSet();
CharacterSet s2 = new CharacterSet();
s1.add(
Explanation / Answer
Modified for you with ArrayList. Plz rate me...
import java.util.ArrayList;
public class CharacterSet {
ArrayList<Character> charSet;
public CharacterSet(ArrayList<Character> charSet) {
super();
this.charSet = charSet;
}
public CharacterSet() {
super();
this.charSet = new ArrayList<Character>();
}
public void add(char ch) {
if(Character.isDigit(ch)) {
System.out.println("Error adding character " + ch);
return;
}
for(char c: charSet) {
if(ch == c)
return;
}
charSet.add(ch);
}
public void delete(char ch) {
if(!charSet.remove((Character)ch)) {
System.out.println("Error deleting " + ch);
}
}
public CharacterSet union(CharacterSet x) {
charSet.addAll(x.getCharSet());
return this;
}
public CharacterSet intersection(CharacterSet x) {
charSet.retainAll(x.getCharSet());
return this;
}
public int cardinality() {
return charSet.size();
}
public boolean equals(CharacterSet x) {
return charSet.containsAll(x.getCharSet());
}
public String toString() {
return charSet.toString();
}
public boolean isEmpty() {
return charSet.isEmpty();
}
public boolean isSubset(CharacterSet x) {
return charSet.containsAll(x.getCharSet());
}
public ArrayList<Character> getCharSet() {
return charSet;
}
public void setCharSet(ArrayList<Character> charSet) {
this.charSet = charSet;
}
}