patternjavaMinor
Binary relation interface
Viewed 0 times
interfacerelationbinary
Problem
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:
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 getY(String X){
Set Y = new TreeSet();
for(int i = 0;i getX(String Y){
Set X = new TreeSet();
for(int i = 0;i delY(String X, Relation r){
Set Y = new TreeSet();
for(int i = 0;i delX(String Y){
Set X = new TreeSet();
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){
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 getY(X value);
//return set of all Y values with relation X
public Set getX(Y value);
//return set of all X values with relation Y
public void clear();
//clear relation of values
public Set> delY(X value);
//remove all relations containing given Y
public Set> 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 RelationImplementation:
```
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 getY(String X){
Set Y = new TreeSet();
for(int i = 0;i getX(String Y){
Set X = new TreeSet();
for(int i = 0;i delY(String X, Relation r){
Set Y = new TreeSet();
for(int i = 0;i delX(String Y){
Set X = new TreeSet();
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){
Solution
There are a couple of things in here that concern me.... but, the most concerning is:
Use
Code like this:
will fail when you get data that is not String-constant data.
That line should be:
This problem is found all over your code.
In addition, there are other problems:
-
your arrays are pre-sized. This code:
is potentially OK if you are initializing the array, but what if there are more than 16 pairs?
-
it would be better to create a 'Pair' class to contain your two strings, than to use the 2-size array on the
your StringPair class could look something like:
-
but, that method is pretty broken. It has the two input parameters
now, since you set
Further, that last 5 lines of the method can be replaced with:
Use
.equals() to compare String valuesCode like this:
if(basket[i][0]==X){will fail when you get data that is not String-constant data.
That line should be:
if(basket[i][0].equals(X)){This problem is found all over your code.
In addition, there are other problems:
-
your arrays are pre-sized. This code:
String[][] basket = new String[16][2];is potentially OK if you are initializing the array, but what if there are more than 16 pairs?
-
it would be better to create a 'Pair' class to contain your two strings, than to use the 2-size array on the
basket. Then your basket could be something like:StringPair[] basket = new StringPair[SIZE];your StringPair class could look something like:
public class StringPair {
private final String first, second;
public StringPair(String first, String second) {
this.first = first;
this.second = second;
}
..... getFirst() .....
..... getSecond() .....
}-
compare(String[][] that, String[][] ths) is a method that needs no state data. It should be a static method, or a utility method. Alternatively, it should be a simple 1-value method compare(MyClass that) ... which, for what it is worth, would be very similar to the equals(...) method.but, that method is pretty broken. It has the two input parameters
that and ths, but it then overwrites those values with the internal versions bag and basket.that = basket;
ths = bag;
boolean equal;
if(that.equals(ths))
equal = true;
else
equal = false;
return equal;now, since you set
bag = basket earlier, this will always return true.Further, that last 5 lines of the method can be replaced with:
return (that.equals(ths));Code Snippets
if(basket[i][0]==X){if(basket[i][0].equals(X)){String[][] basket = new String[16][2];StringPair[] basket = new StringPair[SIZE];public class StringPair {
private final String first, second;
public StringPair(String first, String second) {
this.first = first;
this.second = second;
}
..... getFirst() .....
..... getSecond() .....
}Context
StackExchange Code Review Q#45452, answer score: 2
Revisions (0)
No revisions yet.