PDA

Click to See Complete Forum and Search --> : Java: How To Program (e-Book) exercise


scientist_nl
Sep 14th, 2007, 09:49 AM
Hi guys,

I am using the sixth edition of Java: How To Program, written by Deitel, and now I'm trying to handle the exercises. But one of them is bugging me:

Set of Integers) Create class IntegerSet. Each IntegerSet object can hold integers in the range 0100. The set is represented by an array of booleans. Array element a[i] is true if integer i is in the set. Array element a[j] is false if integer j is not in the set. The no-argument constructor initializes the Java array to the "empty set" (i.e., a set whose array representation contains all false values).

Provide the following methods: Method union creates a third set that is the set-theoretic union of two existing sets (i.e., an element of the third set's array is set to TRue if that element is true in either or both of the existing setsotherwise, the element of the third set is set to false). Method intersection creates a third set which is the set-theoretic intersection of two existing sets (i.e., an element of the third set's array is set to false if that element is false in either or both of the existing setsotherwise, the element of the third set is set to true). Method insertElement inserts a new integer k into a set (by setting a[k] to true). Method deleteElement deletes integer m (by setting a[m] to false). Method toSetString returns a string containing a set as a list of numbers separated by spaces. Include only those elements that are present in the set. Use --- to represent an empty set. Method isEqualTo determines whether two sets are equal. Write a program to test class IntegerSet. Instantiate several IntegerSet objects.

Anyone who knows how to handle this one, or even better, knows where to find the answers for these exercises?

Thanks in advance!

ComputerJy
Sep 14th, 2007, 05:21 PM
public class IntegersSet {

private boolean[] integers;

public IntegersSet() {
integers = new boolean[100];
}

public static IntegersSet union(IntegersSet set1, IntegersSet set2) {
IntegersSet result = new IntegersSet();
for (int i = 0; i < 100; i++) {
if (set1.getValue(i) || set2.getValue(i)) {
result.insertElement(i);
}
}
return result;
}

public static IntegersSet intersection(IntegersSet set1, IntegersSet set2) {
IntegersSet result = new IntegersSet();
for (int i = 0; i < 100; i++) {
if (set1.getValue(i) && set2.getValue(i)) {
result.insertElement(i);
}
}
return result;
}

private boolean getValue(int i) {
return integers[i];
}

public void insertElement(int i) {
integers[i] = true;
}

public void deleteElement(int i) {
integers[i] = false;
}

public String toSetString() {
String result = "";
for (int i = 0; i < 100; i++) {
result += ((integers[i]) ? i : "---") + " ";
}

return result.substring(0, result.length() - 1);
}

public boolean isEqualTo(IntegersSet anotherSet) {
for (int i = 0; i < 100; i++) {
if (anotherSet.getValue(i) != this.getValue(i)) {
return false;
}
}
return true;
}
}

scientist_nl
Sep 24th, 2007, 07:36 AM
Thanks bro!!

oceanebelle
Sep 24th, 2007, 07:55 AM
Anyone who knows how to handle this one, or even better, knows where to find the answers for these exercises?

You haven't tried?

ComputerJy
Sep 24th, 2007, 08:17 AM
You haven't tried?
here's your answer
I am using the sixth edition of Java: How To Program, written by Deitel, and now I'm trying to handle the exercises. But one of them is bugging me: