In: Computer Science
1.
public class MyString {
private char[] data;
MyString(String string) { data = string.toCharArray(); }
public int compareTo(MyString other) { /* code from HW1 here */ }
public char charAt(int i) { return data[i]; }
public int length() { return data.length; }
public int indexOf(char c) { /* code from HW1 here */ }
public boolean equals(MyString other) { /* code from HW1 here */ }
/*
* Change this MyString by removing all occurrences of
* the argument character c. For example, if MyString s
* represents the text "radar", then s.remove('a') will
* change s so that it now represents the text "rdr".
*/
public void remove(char c) {
/* Type in the box below the code that should go here. */
}
}
public class ArrayIntSet {
private int[] data;
private int size;
public ArrayIntSet(int capacity) {
data = new int[capacity];
size = 0;
}
2.
public int size() { return size; }
public boolean contains(int i) { /* Code from HW3 here */ }
public boolean addElement(int element) { /* Code from HW3 here */ }
private int index(int element) { /* Code from HW3 here */ }
public boolean removeElement(int element) { /* Code from HW3 here */ }
public boolean equals(ArrayIntSet other) { /* Code from HW3 here */ }
public void union(ArrayIntSet other) { /* Code from HW3 here */ }
public void intersect(ArrayIntSet other) { /* Code from HW3 here */ }
/*
* Returns a new ArrayIntSet that contains
* all the even numbers from this set.
*/
public ArrayIntSet evenSubset() {
/* Write code for this method in the box below */
}
}
1.public void
remove(char c) {
int j, count = 0, n
=string.length();//to get the length of string or we can call
'length()'because it already declare function
char []t
= string.toCharArray();//to get string to array or we can call
'data' because it already decalre in above so same 'data' array
will be used as 't'
for
(int i = j =
0; i < n; i++)
{
if
(t[i] != c)
t[j++]
= t[i];
else
count++;
}
while(count
> 0)
{
t[j++]
= '\0';
count--;
}
System.out.println(t);
}
2.public int[] evenSubset(){
int[] temp;
temp = new int
[size];
int j =
0;
for(int
i=0;i<data.length;i++){
//here data used for array set
if(data[i]%2 ==
0) {
temp[j++] = data[i];
}
}
return temp;// temp is new array which contain even
number
}