2
\$\begingroup\$

I was supposed to implement set without using any implemented structures. I would love to know what can I correct in my code, because I am not that much satisfied with its quality, so if you have any ideas I will be so grateful!

import java.util.Objects;

abstract class Set {
    Object elements[] = null;
    abstract Set sum(Set set);
    abstract Set antivalence(Set set);
    abstract Set union(Set set);
    abstract void add(Object x);
    abstract void delete(Object x);
    void print() {
        if(this.elements == null) System.out.print("Set is empty.");
        else for (Object element : this.elements) System.out.print(element);
        System.out.println();
    }
}

class NumSet extends Set {

    private int countReps(Set set) {
        int counter = 0;
        for (Object element1 : this.elements)
            for (Object element2 : set.elements)
                if(Objects.equals(element1, element2)) counter++;
        return counter;
    }

    @Override
    public Set sum(Set set) {
        if(this.elements == null) return set;
        if(set.elements == null) return this;
        Set S = new NumSet();
        boolean flag = false;
        S.elements = new Integer[set.elements.length + this.elements.length - this.countReps(set)];
        for(int i = 0; i < this.elements.length; i++) {
            S.elements[i] = this.elements[i];
            for(int j = 0; j < set.elements.length; j++) {
                for (Object element : this.elements)
                    if (set.elements[j] == element) flag = true;
                if(!flag) S.elements[i + 1] = set.elements[j];
                flag = false;
            }
        }
        return S;
    }

    @Override
    public Set antivalence(Set set) {
        if(this.elements == null) return set;
        if(set.elements == null) return this;
        Set A = new NumSet();
        boolean flag = false;
        int counter = 0;
        A.elements = new Integer[set.elements.length + this.elements.length - 2*this.countReps(set)];
        for(int i = 0; i < this.elements.length; i++) {
            for (Object element : set.elements)
                if (this.elements[i] == element) flag = true;
            if(!flag) {
                A.elements[counter] = this.elements[i];
                counter++;
            }
            flag = false;
        }
        for(int i = 0; i < set.elements.length; i++) {
            for (Object element : this.elements)
                if (set.elements[i] == element) flag = true;
            if(!flag) {
                A.elements[counter] = set.elements[i];
                counter++;
            }
            flag = false;
        }
        return A;
    }

    @Override
    public Set union(Set set) {
        Set U = new NumSet();
        if(this.elements == null || set.elements == null) {
            U.elements = null;
            return U;
        }
        boolean flag = false;
        int counter = 0;
        U.elements = new Integer[this.countReps(set)];
        for(int i = 0; i < this.elements.length; i++) {
            for (Object element : set.elements)
                if (this.elements[i] == element) flag = true;
            if(flag) {
                U.elements[counter] = this.elements[i];
                counter++;
            }
            flag = false;
        }
        return U;
    }

    @Override
    public void add(Object x) {
        Integer new_elements[];
        if(this.elements == null) {
            new_elements = new Integer[1];
            new_elements[0] = (Integer)x;
        } else {
            int len = this.elements.length;
            new_elements = new Integer[len + 1];
            for(int i = 0; i < len; i++) new_elements[i] = (Integer) this.elements[i];
            new_elements[len] = (Integer)x;
        }
        this.elements = new_elements;
    }

    @Override
    public void delete(Object x) {
        Integer new_elements[];
        boolean flag = false;
        int i;
        if(this.elements == null) {
            System.out.println("Set is empty! Cannot delete anything.");
        } else {
            int len = this.elements.length;
            for (Object element : this.elements) {
                if (Objects.equals(element, x)) flag = true;
            }
            if(flag) {
                if(len == 1) {
                    this.elements = null;
                } else {
                    new_elements = new Integer[len - 1];
                    for(i = 0; i < len; i++) {
                        if (Objects.equals(this.elements[i], x)) break;
                        new_elements[i] = (Integer) this.elements[i];
                    }
                    for(int j = i+1; j < len - 1; j++) new_elements[i] = (Integer) this.elements[j];
                    this.elements = new_elements;
                }
            } else System.out.println("No such element!");
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ What's with the inconsistent use of space after flow control keywords? Did your IDE do this? \$\endgroup\$ – yuri Mar 6 '18 at 11:18
  • \$\begingroup\$ yeah that's my IDE, I haven't noticed that, thanks a lot! \$\endgroup\$ – Zuzanna Kalinowska Mar 6 '18 at 13:06
3
\$\begingroup\$
  • Use java.lang.System.arraycopy() instead of copying array elements one by one.
  • Introduce some private methods, for example one checking if array contains an object
  • Either start using generics, remove cast to Integer or actually replace Object with Integer in interface if your implementation can't work on other types of elements
  • Consider storing Set elements as sorted array/list, that will allow for faster implementation of operations other than add()
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.