1

My Java assignment is to implement a set class by using an array.

The assignment won't allow me import the set class from the library, so I have to make it on my own. When I tried to print out the array, it prints out numbers in repeats, not unique numbers. I don't know where the problem is, so if you guys can find any errors in my code, it would be great. I tried to add numbers 2, 3, and 4 to the set, so the result should be 2 3 4, but the code shows me 2 3 2 3 2.

I think the source of the problem is from the add method from the set class, but I don't know what the problem is exactly.

import java.util.Arrays;

public final class Set implements SetInterface
{

    private int[] set;
    private int size;
    private int capacity;

    public Set(int c)
    {
        capacity = c;
        set = new int[capacity];
        size = 0;
    }

    public boolean contains(int x)
    {
        boolean contains = false;
        for(int i = 0; i<capacity; i++)
        {
            if(x == set[i])
                contains =  true;
            else
                contains = false;
        }
        return contains;
    }

    public void add(int x)
    {

        for(int i = 0; i<capacity; i++)
        {
            if(!contains(x))
            {
                if(size == capacity)
                {
                    set = Arrays.copyOf(set,size*2);
                }
                if(set[i]==0)
                {
                    set[i++] = x;
                }

            }
        }
        size++;
    }

    public boolean remove(int x)
    {
        boolean remove = false;
        for(int i = 0; i < capacity; i++)
        {
            if(x == set[i])
            {
                set[i] = set[size -1];
                size--;
                remove =  true;
            }
            if(isEmpty())
            {
               remove =  false;
            }
        }
        return remove;
    }

    public void clear()
    {
        set = null;
        size = 0;
    }

    public int size()
    {
        return size;
    }

    public boolean isEmpty()
    {
        if(size == 0)
            return true;
        else
            return false;
    }

    public int[] toArray()
    {
        return Arrays.copyOf(set, capacity);
    }
}

This is the driver class that I test my class.

import java.util.Arrays;

public class SetDriver 
{
    public static void main(String[] args)
    {
        SetDriver driver = new SetDriver();
        Set s1 = new Set(5);
        s1.add(2);
        s1.add(3);
        s1.add(4);
        driver.print(s1);
        System.out.println("Size: "+s1.size());
    }

   public static void print(Set s)
    {
        for(int i = 0; i<s.toArray().length; i++)
        {
            System.out.print(s.toArray()[i]+" ");
        }
        System.out.println("");
    }
}

The outputs are here:

2 3 2 3 2 
Size: 3 
3
  • 1
    Remove else contains = false; from your contains() method. Commented Jul 28, 2017 at 22:59
  • 2
    Your add() method doesn't make sense. What's the loop for? Commented Jul 28, 2017 at 23:00
  • Your current contains method sets the contains variable only to true if the last element was equal since if not it will set it to false again at the last element, even if it was true before. Commented Jul 28, 2017 at 23:01

2 Answers 2

1

There's a likely problem with your contains method. Suppose that you did find a duplicate. What happens is that you assign your variable to true and you continue to iterate. This stomps over the logic entirely; you could have a duplicate but never act on it because your boolean code precludes you from doing so.

Ideally, when you find a match, you must stop iterating and return immediately.

public boolean contains(int value) {
    for(int setItem : set) {
        if(setItem == value) {
            return true;
        }
     }
     return false;
 }
Sign up to request clarification or add additional context in comments.

Comments

0

You should change add method like this.

public void add(int x) {
    if (contains(x))
        return;
    if (size >= capacity) {
        capacity *= 2;
        set = Arrays.copyOf(set, capacity);
    }
    set[size++] = x;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.