In my Class IntegerSet i hav declared the below method but its not printing when i ran the test

public static IntegerSet union(IntegerSet setA, IntegerSet setB){
        IntegerSet u = new IntegerSet();
        for (int i=0;i <= CAPACITY; i++)
            if (a[i]==true || setB.a[i]==true)
        return u;

In Testing in main,

System.out.println("The union of setA and setB is: " + IntegerSet.union(setA, setB));

The test is provided, by my instructor, i need to make my Class(which i came up myself) print the test.
I am just wondering between the words union and u.

Recommended Answers

All 2 Replies

You are not printing any type in that way. You need to make a function inside IntegerSet that prints each one of the elements from the set. Method union(IntegerSet setA, IntegerSet setB) is returning an IntegerSet class, not an integer or a string. You can implement the toString() method if you wish that System.out.println("The union of setA and setB is: " + IntegerSet.union(setA, setB)); works as you want.

When you code
System.out.println( IntegerSet.union(setA, setB));
the union method returns an instance of IntegerSet, and println then calls that object's toString() method to get a printable version of the object. Every object inherits a version of toString from the Object class, but it's not very useful (it just returns the object's class name and hash code).
When you create your own class one of the first things to do is to override toString() to return a sensible printable representation of that object.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.