Question: Java create a class called charset so each object of class charset can hold printable ASCII characters in the range from space 32 through ~

Java

create a class called charset so each object of class charset can hold printable ASCII characters in the range from space 32 through ~ 127 and most common set operations are available. Your character set must be represented internally as an int array of 128 ones and zeros (or a boolean array of 128 trues and falses), but we will ignore the first 32 elements of the array (non-printable characters). Array element a[i] is 1 if character i is in the set and array element a[j] is 0 if character j is not in the set. The default constructor initializes an empty set, i.e., a set whose array representation contains all zeros. it also has a constructor that takes a char value c as a parameter (a set with one element c) and another constructor that takes a string as a parameter as well. Here is how to represent a set with elements '!' and '$' (values from other positions are all 0's):

index 0 ... 31 32 33 34 35 ... 127
value 0 ... 0 0 1 0 1 ... 0

1. Three overload constructors: CharSet(), CharSet(char c), CharSet(String s)

2. Union of two CharSets(union): union of two sets and return a newly created set

3. Intersection of two CharSets(intersection): intersection of two sets and return a newly created set

4. Insert an element to a CharSet(inset): inset element if valid and return true;

5. Remove an element from a CharSet(remove): remove element if valid and return; otherwise, return false.

6. Check if element c is in the set (isElement): return true if valid element is in the set;

7. Return the number of elements in the set (size): return 0 for an empty set, 1 for a set of element and so on.

8. Return String representation of an CharSet(toSting): return {} for empty set, {!$} for a set with '!' and '$'

9. Copy one CharSet to another CharSet(clone): create a copy of current set and return a newly created set

public class Charset {

private int asciiSet[] = new int [128]; public Charset() { for(int i=0;i=32 && ascii<=127) asciiSet[ascii]=1; } public Charset(String str) { for(int i=0;i=32 && ascii<=127) asciiSet[ascii]=1; } } public void printSet() { System.out.println("*******************"); System.out.println("Index:\tValue"); for(int i=0;i

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!