0

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)
                u.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.

3
Contributors
2
Replies
3
Views
4 Years
Discussion Span
Last Post by JamesCherrill
0

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.

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.