Frage

I have the following project to complete but the person who has set it isn't making much sense. I have done around 60% of the program and it just doesn't feel right and I would like to check what other people think they are going on about.

For this part of the project you are asked to implement a class IntegerSet. The set should be represented by an array of booleans. The ith element will be true if and only if i is in the set. The largest integer that can be an element of the set is thus determined by the size of the array.

You should provide the following methods: A constructor that has a single integer argument domainMax and creates an instance of IntegerSet whose elements may be any non-negative integer not greater than the domainMax. Initially the set should be empty. You may assume that the argument will always be greater than zero.

A method addElement that takes a non-negative integer as argument and inserts it into a set. If the element exceeds the domain maximum for the set, the domain maximum should be increased to accommodate it. If the element is already present, the set is unchanged. You may assume that the method will never be called with negative argument.

A static method union that takes two sets as its arguments and returns a new set that is the union of the two sets. Note that the two sets supplied as arguments may have different domain maxima; the resulting union should have whichever of these two domain maxima is larger.

A static method intersection that takes two sets as its arguments and returns a new set that is the intersection of the two sets. Note that the two sets supplied as arguments may have different domain maxima; the resulting intersection should have whichever of the two domain maxima is smaller.

  1. What is meant by the word "set" I am unable to find anything relevant to this project.
  2. How would I take two objected instances in union and intersection if the methods inside an instanced object. It makes no sense to me.
  3. What do I do to store the integer sets as it needs to be adapted to size.
War es hilfreich?

Lösung

The specifications seem pretty good to me. They mean Set as in "Set Theory". One way to represent a set of integers, albeit inefficient from a storage point of view, is to use an array of booleans. This is a perfectly reasonable exercise.

Clearly it would be better to use Set<Integer> for this project, but that would be missing the point. This is a programming exercise that is fun.

As always, break this down into manageable parts. I would:

  1. Create the class
  2. Write the constructor. This will lead you to the proper member variables.
  3. Write the addElement method

Those things should be pretty easy and perhaps you will gain more insight. Once you have that work on the union and intersection methods. If you still have trouble, check back here!

Keep in mind if you add the value 3, to the set twice, it will only be in the set once. (Definition of sets). So your method will look something like this (keep in mind this is not the 100% correct definition):

public void addElement(int element) {
    myArray[element] = true;
}

So when you call addElement(3); a second or third time, it does not change the state of the set as is proper.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top