File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Question about union and intersection exercise question from self-study book.  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about union and intersection exercise question from self-study book.  " Watch "Question about union and intersection exercise question from self-study book.  " New topic
Author

Question about union and intersection exercise question from self-study book.

Gary Charles
Ranch Hand

Joined: Oct 29, 2012
Posts: 32
I'm self-studying Java and I do not understand some things from one of the end-of-chapter exercises and I don't have a teacher to ask. Here it is in its entirety:

"Create class IntegerSet. Each IntegerSet object can hold integers in the range 0-100. 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 array to the so-called "empty set,"
(i.e., all false values).

Provide the following methods: Method union creates a set that is the
set-theoretic union of two existing sets(i.e., an element of the new set's
array is set to true if that element is true in either or both of the
existing sets---otherwise, the new set's element is set to false). Method
intersection creates a set which is the set-theoretic intersection of two
existing sets(i.e., an element of the new set's array is set to false if that
element is false in either or both of the existing sets--otherwise, the new
set's element 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 toString 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. Test that all your
methods work properly. "

---
I don't want a direct code example answer. There are several of those on the www already that I could use if I wanted.
The first paragraph is easy. Create a boolean array with 100 elements set to false. But what do I use that array for? Please read on.

The second paragraph starts by saying to create a set(I'll call that set 0) from two existing sets(call these sets 1 and 2). What two existing sets? I guess that I'm supposed to make them up with values, right? So I now have two arrays with numbers in them? Then my isEqualTo method compares the sets 1 and 2, and my set 0 is populated based on the comparison of
sets 1 and 2?

Method intersection does something similar except using false values. So far so good?

Method insertElement and deleteElement aren't really inserting or deleting elements because I'm just changing the value to true or false aren't I? So the three sets would look like this when I'm done?
set 1 - [1, 2, 6]
set 2 - [1, 2, 4 ]
union set 0 - [true, true, false, true, false, true, false, false... to 100]
Then output would be "1, 2, 4, 6".

Have I got this right?
Then the last question I have about this instruction "Instantiate several IntegerSet objects". I understand how to instantiate new objects but not what the book is suggesting here? Is the idea to make a separate class for these test sets? Then call this class when I need a new set?


If you read this far, thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
Gary Charles wrote: . . .
The first paragraph is easy. Create a boolean array with 100 elements set to false. But what do I use that array for? Please read on.
. . .
One: Do it is little stages. Get one stage working before you try the next stage.
Two: You have an error in that statement.
Three: you add a value to the set by changing one element in that array to true.
Michael Krimgen
Ranch Hand

Joined: Jul 08, 2012
Posts: 35
Hi Gary,

I see two possible implementations for the set-theoretic methods (union, intersection, ...).

1. Static methods
2. Instance methods.

Now, you are asking what exactly are these sets (set1 and set2). This will be clear if you instantiate several objects of IntegerSet, as asked in the last sentence of the task.
The easiest thing would be a simple main method:


Your example of the union

set 1 - [1, 2, 6]
set 2 - [1, 2, 4 ]
union set 0 - [true, true, false, true, false, true, false, false... to 100]
Then output would be "1, 2, 4, 6".

is almost correct. The possible values of your integer set are 0 to 100, not 1 to 100.
Gary Charles
Ranch Hand

Joined: Oct 29, 2012
Posts: 32
Thank-you for the feedback. Here is my attempt at part of the exercise. It works and I'm using somewhat of a hybrid approach with TestNG by not using assertEquals(for example) and using System.out.printf(). I know I wouldn't do that if I were preparing this for automated testing.

If you have any feedback I would welcome it. Thank-you.




My test class:


Which prints:
Union Set: 3 5.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

Gary Charles wrote:If you have any feedback I would welcome it.

OK:
  • 1. Why is SET_LENGTH == 99?
  • 2. insertElement() is a bit of a misnomer because you're not really 'inserting' anything; you're setting an element if it isn't already set, ie, you're doing a Boolean "union" function (in computer logic, this is called an 'OR') on a single element.
    In the Java collections framework, add() is only used to augment a dataset; the List interface, which allows access by element, also has a set() method, which is much closer to what you're doing.
  • 3. Your union() logic is overly complex, mainly because you're trying to keep everything in the same class. Why not have it return a new IntegerSet that is the result of the union function? Then you could get rid of the set0 field and the printString() method.
    General tip: Apart from setters, don't write methods that return void unless you really have to. Methods (especially public ones) should do something; and that almost always means that they should also return something.
  • 4. Use meaningful names. Things like set0 and set1 don't really mean anything to anyone except you, and programs are designed to be read by other people. Otherwise, why wouldn't we all just write in machine code?
    Also: you don't need to use different identifiers for all your integer variables, especially if they're in different methods or loops. My advice: if it's an index (and all of yours are), use 'i' (or 'index') unless you're forced to use something else.

  • HIH

    Winston

    Isn't it funny how there's always time and money enough to do it WRONG?
    Articles by Winston can be found here
    Gary Charles
    Ranch Hand

    Joined: Oct 29, 2012
    Posts: 32
    Thanks Winston. I appreciate the feedback and it gives me some things to work on.

    Some things I wasn't sure about in your comment are these:

    Winston Gutkowski wrote:
  • 2. insertElement() is a bit of a misnomer ...
  • 3. Your union() logic is overly complex, mainly because you're trying to keep everything in the same class. Why not have it return a new IntegerSet that is the result of the union function? Then you could get rid of the set0 field and the printString() method.



  • On item #2 I wasn't sure if you mean the logic, the method name, or both. FWIW - It's an exercise requirement. The book says:
    Method insertElement inserts a new integer k into a set(by setting a[k] to true).
    I don't have a teacher that I need to turn in the assignment to so I can do whatever I want but I guess the author is trying to get the reader thinking about some concept. Or maybe you mean the whole thing is wrong in how I interpreted the instructions.

    On your item #3, I would then have three classes? Maybe something like this?
  • IntegerSet
  • IntegerSetDriver
  • IntegerSetTestNG


  • It's not clear to me exactly how/when to break a class into smaller classes. There was a similar question here on the ranch some time ago I will find that and review it.
    Thanks again for your feedback.
    Gary
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7545
        
      18

    Gary Charles wrote:On item #2 I wasn't sure if you mean the logic, the method name, or both.

    The method name; and it's more of a convention thing than an outright mistake. insertElement() suggests to me that you are, indeed adding something to your dataset, when in fact you're simply setting a flag if it isn't already set. Personally, I kind of like:

    public void unionElement(int k) ...

    because, for a "boolean set" class, it explains precisely what you're doing. But don't sweat it too much and, as you say, that's what you were asked to call it.

    On your item #3, I would then have three classes?

    Oooh, no. Overthinking again. How about a:

    public IntegerSet union(IntegerSet other) { ...

    method that returns a different IntegerSet instead? Have a think how you might write that (you've basically already written almost all the code you need).

    Do you also see how it makes the rest of your class a lot simpler?

    PS: Another tip - Unless you know that a method is going to be overridden, make them final. You can always remove it later on if you discover you were wrong, but you can't add it later, even if you want to.

    HIH

    Winston
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7545
        
      18

    Winston Gutkowski wrote:How about a:
    public IntegerSet union(IntegerSet other) { ...
    method that returns a different IntegerSet instead?

    Actually, thinking about it; that does rather change the style of your class, which is to be mutable. I'm afraid I much prefer immutable ones, so I've gone overboard here.

    However, you could certainly code the method without the set0 instance field; which is complicating your class definition. I also don't understand why you think you need two separate "toString()"-like methods.

    Winston
    Gary Charles
    Ranch Hand

    Joined: Oct 29, 2012
    Posts: 32
    Winston Gutkowski wrote: I also don't understand why you think you need two separate "toString()"-like methods.

    I created two print methods to meet the exercise objective Method toString returns a String containing a set as a list of numbers separated by spaces of returning a string and also allow me to print it out on the display.

    Thanks for hanging in there with me on this thread. Your answers and my misperceptions (trying to create three classes out of this one) have forced me to think about the exercise in ways the book couldn't. The feedback about not over thinking it was useful to.

    Here's my code for this iteration. I need to add intersection logic next.



    Gary
     
    Consider Paul's rocket mass heater.
     
    subject: Question about union and intersection exercise question from self-study book.
     
    Similar Threads
    Iterate over two ArrayLists
    trying to find union/intersection of arrays containing integers
    Generic methods/classes
    I need help with my project! ADT
    TreeSet