....can any one knows how to sort my class union....
...since when i run it the output is correct but i need to sort it to feel much better......
here is my program....
this is my MAIN.....
public class FinalProjectBitString
{
public static void main(String[] args)
{
MySet universalSet = new MySet(50);
MySet a = new MySet(8);
MySet b = new MySet(11);
int element = 1;
int elementForA = 5;
for(int i=0;i<universalSet.getSetSize();i++)
{
universalSet.addElement(element);
element+=1;
}
a.addElement(5);
a.addElement(10);
a.addElement(15);
a.addElement(20);
a.addElement(25);
a.addElement(30);
a.addElement(35);
a.addElement(40);
b.addElement(10);
b.addElement(13);
b.addElement(17);
b.addElement(20);
b.addElement(25);
b.addElement(29);
b.addElement(33);
b.addElement(37);
b.addElement(41);
b.addElement(45);
b.addElement(49);
System.out.println("U = "+universalSet);
System.out.println();
System.out.println("A= "+a);
System.out.println("B= "+b);
System.out.println();
System.out.println("BitString Equivalent:");
System.out.println("A = "+a.bitString(universalSet));
System.out.println("B = "+b.bitString(universalSet));
System.out.println();
System.out.println();
System.out.println("1. Ž = "+a.complement());
System.out.println();
MySet union = a.union(b);
System.out.println("2. A U B = "+union);
System.out.println(" A U B = "+union.bitString(universalSet));
System.out.println();
MySet intersection = a.intersection(b);
System.out.println("3. A ï B = "+intersection);
System.out.println(" A ï B = "+intersection.bitString(universalSet));
System.out.println();
MySet difference = a.difference(b);
System.out.println("4. A - B = "+difference);
System.out.println(" A - B = "+difference.bitString(universalSet));
System.out.println();
System.out.println("5. A í B = "+a.xorVal(b));
System.out.println();
}
}
this is for my SET
interface Set
{
public boolean isFull();
public boolean isEmpty();
public int getNumberOfElements();
public int getElementAt(int index);
public void addElement(int element);
public boolean isValidIndex(int index);
public boolean isElement(int element);
}
and this is for MYSET...(class is present)
import java.io.*;
import javax.swing.*;
import java.util.*;
class MySet implements Set
{
private int[] element;
private int[] bitString;
private int count;
private int elementSize;
private int bitStringCount;
public MySet()
{
element=new int[100];
count = 0;
bitStringCount=0;
}
public MySet(int size)
{
element = new int[size];
count = 0;
bitStringCount = 0;
}
public boolean isFull() { return count==element.length; }
public boolean isEmpty() { return count==0; }
public int getNumberOfElements() { return count; }
public void addElement(int element)
{
if(!isFull())
{
if(!isElement(element))
{
this.element[count++]=element;
}
}
}
public boolean isElement(int element)
{
boolean found = false;
if(!isEmpty())
{
for(int i=0;i<count;i++)
{
if(this.element[i]==element)
{
found = true;
break;
}
}
}
return found;
}
public int getElementAt(int index) { return element[index]; }
public boolean isValidIndex(int index) { return index<=count && index>0; }
public MySet intersection(MySet set)
{
MySet mySet = new MySet();
for(int i=0;i<set.getNumberOfElements();i++)
{
if(this.isElement(set.getElementAt(i)))
{
mySet.addElement(set.getElementAt(i));
}
}
return mySet;
}
public MySet union(MySet ms)
{
MySet mySet = new MySet(this.getNumberOfElements()+ms.getNumberOfElements());
for(int i=0;i<getNumberOfElements();i++)
{
mySet.addElement(getElementAt(i));
}
for(int i=0;i<ms.getNumberOfElements();i++)
{
if(!isElement(ms.getElementAt(i)))
{
mySet.addElement(ms.getElementAt(i));
}
}
//MySet.sort(mySet);
return mySet;
}
public MySet difference(MySet ms)
{
MySet set = new MySet();
for(int i=0;i<getNumberOfElements();i++)
{
if(!ms.isElement(this.getElementAt(i)))
{
set.addElement(this.getElementAt(i));
}
}
return set;
}
public String xorVal(MySet ms)
{
int[] bit = new int[ms.getNumberOfBit()];
for(int i=0;i<bit.length;i++)
{
if(this.getBitString(i)==ms.getBitString(i))
{
bit[i] = 1;
}
else
{
bit[i] = 0;
}
}
StringBuffer sb = new StringBuffer();
sb.append("{ ");
for(int i=0;i<bit.length;i++)
{
sb.append(bit[i]);
if(i<bit.length-1)
{
sb.append(", ");
}
}
sb.append(" }");
return sb.toString();
}
public int[] getBitStrings()
{
return bitString;
}
public int getBitString(int index)
{
return bitString[index];
}
public int getNumberOfBit()
{
return bitStringCount;
}
public int[] getElements()
{
return element;
}
public String complement()
{
int[] bit = this.getBitStrings();
for(int i=0;i<bit.length;i++)
{
if(bit[i]==0)
{
bit[i]=1;
}
else
{
bit[i]=0;
}
}
StringBuffer sb = new StringBuffer();
sb.append("{ ");
for(int i=0;i<bit.length;i++)
{
sb.append(bit[i]);
if(i<bit.length-1)
{
sb.append(", ");
}
}
sb.append(" }");
return sb.toString();
}
public String bitString(MySet ms)
{
int falseBit = 0;
int bitSize=0;
if(this.getNumberOfElements()!=ms.getNumberOfElements())
{
if(this.getNumberOfElements()>ms.getNumberOfElements())
{
bitSize = this.getNumberOfElements();
}
else
{
bitSize = ms.getNumberOfElements();
}
}
bitString = new int[bitSize];
if(!this.isEmpty()||!ms.isEmpty())
{
boolean found = false;
for(int i=0;i<ms.getNumberOfElements();i++)
{
found = this.isElement(ms.getElementAt(i));
if(found==true)
{
bitString[bitStringCount++]=1;
}
else
{
bitString[bitStringCount++]=falseBit;
}
}
}
StringBuffer sb = new StringBuffer();
sb.append("{ ");
for(int i=0;i<bitStringCount;i++)
{
sb.append(bitString[i]);
if(i<bitStringCount-1)
{
sb.append(", ");
}
}
sb.append(" }");
return sb.toString();
}
public int getSetSize()
{
int size = element.length+1;
if(!isEmpty())
{
size = element.length-1;
}
return size;
}
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append("{ ");
for(int i=0;i<count;i++)
{
sb.append(element[i]);
if(i<count)
{
sb.append(", ");
}
}
sb.append(" }");
return sb.toString();
}
}