I'm trying to design a binary relation interface in Java and then implement it, but I'm not sure if I'm doing it right. I'd really appreciate a little feedback just so I know if I'm way in the wrong direction or not.
This is what I've got so far:
Interface:
public boolean compare(Object that, Object ths);
// compares relations to see if they are equal.
public Set<Y> getY(X value);
//return set of all Y values with relation X
public Set<X> getX(Y value);
//return set of all X values with relation Y
public void clear();
//clear relation of values
public Set<Relation<X, Y>> delY(X value);
//remove all relations containing given Y
public Set<Relation<X, Y>> delX(Y value);
//remove all relations containing given X
public String write(Object that);
//output Relation in string format
public Y addPair (X valX, Y valY);
//adds the entry (X, Y) to the Relation
public Y delPair (X valX, Y valY);
//deletes entry (X, Y) from the Relation
Implementation:
int size;
String[][] basket = new String[16][2];
String[][] bag = basket;
public String addPair(String X, String Y){
basket[size][0] = X;
basket[size][1] = Y;
String pair = basket[size][0] + basket[size][1];
size++;
return pair;
}
public boolean compare(String[][] that, String[][] ths){
that = basket;
ths = bag;
boolean equal;
if(that.equals(ths))
equal = true;
else
equal = false;
return equal;
}
public Set<String> getY(String X){
Set<String> Y = new TreeSet<String>();
for(int i = 0;i<size;i++)
{
if(basket[i][0]==X)
Y.add(basket[i][1]);
}
return Y;
}
public Set<String> getX(String Y){
Set<String> X = new TreeSet<String>();
for(int i = 0;i<size;i++)
{
if(basket[i][1]==Y)
X.add(basket[i][0]);
}
return X;
}
public String write(Object o){
o = basket;
String out = "";
out += o.toString();
System.out.println("OUT: " + out);
return out;
}
public void clear(){
for(int i = 0;i<size;i++)
{
if(basket[i][0]!= null){
basket[i][0] = null;
basket[i][1] = null;
}
}
}
public Set<String> delY(String X, Relation r){
Set<String> Y = new TreeSet<String>();
for(int i = 0;i<size;i++)
{
if(basket[i][0]==X){
Y.add(basket[i][1]);
basket[i][1]=null;
}
}
return Y;
}
public Set<String> delX(String Y){
Set<String> X = new TreeSet<String>();
for(int i = 0;i<size;i++)
{
if(basket[i][1]==Y){
X.add(basket[i][1]);
basket[i][1]=null;
}
}
return X;
}
public String delPair (String X, String Y){
X = "Orange";
Y = "Fruit";
for(int i = 0;i<size;i++)
{
if(basket[i][0]==X && basket[i][1]==Y){
basket[i][1]=null;
basket[i][0]=null;
}
}
return (X + Y);
}