**REQUEST YOU GUYS TO NOT PROVIDE INCOMPLETE ANSWERS, THE PROGRAM SHOULD SATISFY
ID: 3594209 • Letter: #
Question
**REQUEST YOU GUYS TO NOT PROVIDE INCOMPLETE ANSWERS, THE PROGRAM SHOULD SATISFY ALL THE NEEDS OF THE QUESTION BEING ASKED**
A JAVA program that will read a boolean matrix corresponding to a relation R and output whether R is Reflexive, Symmetric, Anti-Symmetric and/or Transitive. Input to the program will be the size n of an n x n boolean matrix followed by the matrix elements. Document your program nicely.
NOTE: The program must output a reason in the case that an input relation fails to have a certain property.
Explanation / Answer
package org.rohit.practice;
import java.util.Scanner;
public class Relation {
public static void main(String[] args) {
boolean a[][]=new boolean[50][50];
boolean reflexive=true,transitive=true,symmetric=true,anti=true;
int n,i,j,count=0;
System.out.println("Please Enter n*n Matrix containing : ");
System.out.println("Please enter size of Matrix: ");
Scanner s=new Scanner(System.in);
n=s.nextInt();
System.out.println("Please enter the elements of Matrix: ");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
a[i][j]=s.nextBoolean();
}
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
//Checking for Reflexive
if(i==j && a[i][j]==false){
reflexive=false;
}
//Checking for Symmetric
if(i != j && ((a[i][j]== true && a[j][i]==false) || (a[i][j]== false && a[j][i]==true))){
symmetric=false;
}
//Checking for Transitive
if(i != j && (a[i][j] != a[j][i]) && (a[i][j] != a[i][i])){
transitive=false;
}
//Checking for Anti Symmetric
if(i!=j && a[i][j]==true){
anti = false;
}
}
}
//Printing the final Output
System.out.println("Relation is: ");
if(reflexive == false){
System.out.println("Not reflexive as all a[x][x] are not true.");
}else{
System.out.println("Reflexive");
}
if(transitive == false){
System.out.println("Not Transitive as if a[x][y] = a[y][x] = true then a[x][x] is false.");
}else{
System.out.println("Transitive");
}
if(symmetric == false){
System.out.println("Not Symmetric as if a[x][y] is true then a[y][x] is false.");
}else{
System.out.println("Symmetric");
}
if(anti == false){
System.out.println("Not Anti Symmetric as if a[x][y] = a[y][x] = true then x is not equals to y.");
}else{
System.out.println("Anti Symmetric");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.