Static


Static

EvilRobot class with static
EvilRobotHarness


Classwork

Correct answers will only use concepts and constructs we have covered in class.


You need not write the following in order.  You may find that you can write one method and use it to help in several others.

Create a class IntegerSet and give it a static instance variable universeSize.  

Provide static accessor and mutator for universeSize, and only allow numbers >= 1. 

In a static initializer block, set universeSize to 100.  

 

Each IntegerSet has an instance variable mySet that is an array of booleans, initialized to length universeSize.  True at a position in the array means that the IntegerSet contains that int, and false means it does not.  So if universeSize were 5, {true, true, false, true, false} would mean the set was [0 1 3 ]. Make mySet final so that while we can change the content of the set, we can't swap it out for an entirely different set of some other size.  Don't provide accessor or mutator.

The toString should be a list of the numbers in the set, in square braces, (e.g. [2 7 18 21 22 50 ])) or else [empty] if there are no numbers in the IntegerSet.

Add a method addInt, which takes an int and adds it to the set if it is within the range (you don't need to do anything if it is already in the set or if it is outside the range).

Add a method empty, which returns true if the IntegerSet is empty and false otherwise.

Add a method size, which returns the number of ints in the IntegerSet (so if empty() was true, size would be 0)

 

Add a static instance variable universe, which is an IntegerSet, and in the initializer block, set it up to be a set containing all (100 of) the elements.  Provide a public accessor; but no public mutator.  Whenever the universeSize changes, create a new universe at the new size.

 

For the following methods, if an IntegerSet is given as parameter that does not match the current universeSize, print a complaint and return null or false (as appropriate).

Add a static method union which takes two IntegerSets and returns an IntegerSet that is their union (all elements in at least one of the two).

Add a static method intersection that takes two IntegerSets and returns an IntegerSet that is their intersection (all elements common to both).

Add a static method disjoint that takes two IntegerSets and returns true if they are disjoint (their intersection is empty).

Add a static method complement that takes one IntegerSet and returns the complement (the elements in the universe that are not in the original set).

 

[EC+20] Create methods multiIntersection, multiUnion, and multiDisjoint  that take variable length parameter lists of IntegerSets and handle the union / intersection /disjoint of all those passed in.

[EC+10] Add a static method multiples that takes an int and returns an IntegerSet containing only multiples of that number (if the number is bigger than universeSize, the set will be empty).

[EC+20] Add a static method primes that returns an IntegerSet containing only the primes, using the Sieve of Eratosthenes method (loop through and eliminate all multiples of 2, then find the next non-eliminated number (it will be a prime) and eliminate all its multiples (but not itself), and repeat (think about when you can stop)

In a main, test that all of these work.