In assignment three you are to develop a Java application that take as its input
ID: 663125 • Letter: I
Question
In assignment three you are to develop a Java application that take as its input a mathematical relation and determine if the mathematical relation is what is know as an equivalence relation.
Given a set A={a,b,c,d} the Cartesian product of A and itself called A X A is a set of ordered pairs where the first coordinate is from the set A and second coordinate is from A.
A X A ={ (a,a), (a,b), (a,c), (a,d), (b,a), (b,b), (b,c), (b,d), (c,a), (c,b), (c,c), (c,d),
(d,a), (d,b), (d,c), (d,d) }
A relation R is any subset of A X A.
So R={(a,a), (b,b),(c,c), (d,d), (b,c), (c,b)} is a subset of A x A and thus a relation.
We say a is related to a or aRa if the ordered pair (a,a) is in the set R.
So aRa, bRb, cRc, dRd, bRc, and cRb.
A relation is an equivalence relation if the following three properties are true
- The relation is reflexive if every element is related to its self. i.e. aRa
- The relation is symmetric. If aRb then bRa
- The relation is transitive if aRb and bRc then aRc.
Your program should be implemented as follows.
1. Create a class call relation.
2. Create a constructor for this class that accepts as three one dimensional arrays. The first array list all elements in the set A that the relation comes from. The second array contains the first coordinates for the relations ordered pars and the third array contains the second coordinates of the relations ordered pairs.
3. You should have tostring method that prints out the relation R. for example R={(a,a), (b,b),(c,c), (d,d), (b,c), (c,b)}.
4. You should have a public method that displays the original set . Example A={a,b,c,d}
5. You should have a public method the displays the Cartesian product of the input set A. For example for the set A={a,b,c,d} your method should print out
A X A ={ (a,a), (a,b), (a,c), (a,d), (b,a), (b,b), (b,c), (b,d), (c,a), (c,b), (c,c), (c,d), (d,a), (d,b), (d,c), (d,d) }
6. You should have a public method that displays the matrix representation of the relation. We will discuss how to do this in class.
7. You should have a private method that determines if the relation represented by your relation object is an equivalence relation and a private Boolean variable set to true if the relation is an equivalence relation or false if the relation is not an equivalence relation.
8. You should have a public getter for this Boolean variable.
Use your program to determine if the following relations on the given sets are equivalence relations.
1. The Relation R={ (1,2), (2,3), (3,4), (4,5)} on the set X={1,2,3,4,5} ordering 1,2,3,4,5.
2. The Relation R={(a,a), (b,b),(c,c), (d,d), (b,c), (c,b)} set A={a,b,c,d} ordering a,b,c,d.
Explanation / Answer
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Relation{
String left;
String right;
public Relation(String l,String r){
left = l;
right = r;
}
}
class Ideone
{
public static boolean check(String s,Relation[] rel){
for (int i = 0; i < rel.length; i++){
if (rel[i].left == s && rel[i].right == s)
return true;
}
return false;
}
public static boolean check1(Relation r,Relation[] rel){
for (int i = 0; i < rel.length; i++){
if (rel[i].right == r.left && rel[i].left == r.right)
return true;
}
return false;
}
public static boolean check2(String s1,String s2,Relation[] rel){
for (int i = 0; i < rel.length; i++){
if (rel[i].left == s1 && rel[i].right == s2)
return true;
}
return false;
}
public static boolean answer(Relation[] rel,String[] set){
for (int i = 0; i < set.length; i++){
if (check(set[i],rel) == false)
return false;
}
for (int i = 0; i < rel.length; i++){
if (check1(rel[i],rel) == false)
return false;
}
for (int i = 0; i < rel.length; i++){
String left = rel[i].left;
String right = rel[i].right;
for (int j = 0; j < rel.length; j++){
if (rel[j].left == right){
if (check2(left,rel[j].right,rel) == false)
return false;
}
}
}
return true;
}
public static void main (String[] args) throws java.lang.Exception{
Relation[] Rel = new Relation[6];
Relation R = new Relation("a","a");
Rel[0] = R;
R = new Relation("b","b");
Rel[1] = R;
R = new Relation("c","c");
Rel[2] = R;
R = new Relation("d","d");
Rel[3] = R;
R = new Relation("b","c");
Rel[4] = R;
R = new Relation("c","b");
Rel[5] = R;
String[] Set = {"a","b","c","d"};
System.out.println(answer(Rel,Set));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.