开发者

java two sets of numbers in two fields of constructor?

I have been asked the following question.

The class IntersectionSet also implements the interface IntSet. An instance of the class stores two sets. The constructor takes two parameters to initialise these two sets. The method isElem implements the behaviour of set intersection, that is, returns true if and only if the given parameter is an element of both sets. Give the full definition of the class IntersectionSet.

I have made the IntSet interface but am not sure how I can get two sets of two integers just from having two fields in the constructor. I have been successful with one set of two numbers from my previous clas开发者_如何学JAVAs that I made.I will supply the Intset interface code and the other class I designed to create one set of numbers that I can check if the element is in there. Any help is greatly appreciated. Cheers.

public class IntervallSet implements IntSet
{
    int lowerbound;
    int upperbound;

    public IntervallSet(int a, int b)
    {
        lowerbound = a;
        upperbound = b;
        if(a>b)
        {
            b = a;
            a = b;
        }
        else
        {
            a = a;
            b = b;
        }
    }

    public boolean isElem(int f)
    {
        if (f>= lowerbound && f<=upperbound)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}


The problem definition says:

An instance of the class stores two sets.

You will need to use IntSet for your storage and parameters instead of int:

IntSet set1, set2;

public IntersectionSet(IntSet a, IntSet b)

You can figure it out from here :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜