sandeshMC

Practical 2 - SC

Feb 2nd, 2016
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.90 KB | None | 0 0
  1. package fuzzyrelations;
  2.  
  3. import java.util.*;
  4.  
  5. public class FuzzyRelations {
  6.  
  7.     static Scanner sc = new Scanner(System.in);
  8.     static double[][] matrix1, matrix2;
  9.  
  10.     static void printMatrix(double[][] matrix) {
  11.  
  12.         for (double[] d : matrix) {
  13.             System.out.println(Arrays.toString(d));
  14.         }
  15.     }
  16.  
  17.     static void union(double[][] a, double[][] b) {
  18.  
  19.         for (int i = 0; i < a.length; i++) {
  20.  
  21.             for (int j = 0; j < a[i].length; j++) {
  22.                 a[i][j] = a[i][j] > b[i][j] ? a[i][j] : b[i][j];
  23.             }
  24.  
  25.         }
  26.  
  27.         System.out.println("Union is:");
  28.         printMatrix(a);
  29.  
  30.     }
  31.  
  32.     static void intersection(double[][] a, double[][] b) {
  33.  
  34.         for (int i = 0; i < a.length; i++) {
  35.  
  36.             for (int j = 0; j < a[i].length; j++) {
  37.                 a[i][j] = a[i][j] < b[i][j] ? a[i][j] : b[i][j];
  38.             }
  39.  
  40.         }
  41.  
  42.         System.out.println("Intersection is:");
  43.         printMatrix(a);
  44.  
  45.     }
  46.  
  47.     static void maxMinComposition() {
  48.  
  49.  
  50.         System.out.println("Enter your first relation matrix m,n ");
  51.         int x1 = sc.nextInt();
  52.         int y1 = sc.nextInt();
  53.  
  54.         double m1[][] = new double[x1][y1];
  55.         System.out.println("Enter your relation matrix ");
  56.         for (int i = 0; i < m1.length; i++) {
  57.             for (int j = 0; j < m1[i].length; j++) {
  58.                 m1[i][j] = sc.nextDouble();
  59.             }
  60.         }
  61.  
  62.         System.out.println("Enter the second relation matrix m,n ");
  63.         int x2 = sc.nextInt();
  64.         int y2 = sc.nextInt();
  65.         double m2[][] = new double[x2][y2];
  66.         System.out.println("Enter your relation matrix ");
  67.         for (int i = 0; i < m2.length; i++) {
  68.             for (int j = 0; j < m2[i].length; j++) {
  69.                 m2[i][j] = sc.nextDouble();
  70.             }
  71.         }
  72.  
  73.  
  74.         int max = m1.length > m2.length ? m1.length : m2.length;
  75.         double ans[][] = new double[max][max];
  76.         int p = 0;
  77.         for (int i = 0; i < max; i++) {
  78.  
  79.             double outer[] = m1[i];
  80.             double inner[] = new double[m2.length];
  81.             //System.out.println(Arrays.toString(outer)+"\t");
  82.  
  83.             for (int y = 0; y < max; y++) {
  84.                 for (int j = 0; j < m2.length; j++) {
  85.  
  86.                     inner[j] = m2[j][y];
  87.  
  88.  
  89.                 }
  90.  
  91.                 double maximum = 0.0;
  92.                 double minimum = 0.0;
  93.  
  94.                 for (int k = 0; k < outer.length; k++) {
  95.                     minimum = inner[k] < outer[k] ? inner[k] : outer[k];
  96.                     if (maximum < minimum) {
  97.  
  98.                         maximum = minimum;
  99.                     }
  100.  
  101.  
  102.                 }
  103.                 ans[i][y] = maximum;
  104.  
  105.             }
  106.             // System.out.print(Arrays.toString(inner));
  107.  
  108.  
  109.  
  110.         }
  111.         printMatrix(ans);
  112.     }
  113.  
  114.     static double[][] getMatrix() {
  115.         System.out.println("Enter your relation matrix m,n ");
  116.         int x1 = sc.nextInt();
  117.         int y1 = sc.nextInt();
  118.  
  119.         double m1[][] = new double[x1][y1];
  120.         System.out.println("Enter your relation matrix ");
  121.         for (int i = 0; i < m1.length; i++) {
  122.             for (int j = 0; j < m1[i].length; j++) {
  123.                 m1[i][j] = sc.nextDouble();
  124.             }
  125.         }
  126.  
  127.         return m1;
  128.  
  129.     }
  130.  
  131.     public static void main(String[] args) {
  132.         // TODO code application logic here
  133.         //  Scanner sc = new Scanner(System.in);
  134.  
  135.         int choice = 0;
  136.  
  137.         do {
  138.             System.out.println("Enter your choice:\n1:Union\n2:Intersection\n3:MaxMin Composition.\n0:exit ");
  139.             choice = sc.nextInt();
  140.             switch (choice) {
  141.                 case 3:
  142.                     maxMinComposition();
  143.  
  144.                     break;
  145.                 case 2:
  146.                     matrix1 = getMatrix();
  147.                     matrix2 = getMatrix();
  148.                     intersection(matrix1, matrix2);
  149.                     break;
  150.                 case 1:
  151.                     matrix1 = getMatrix();
  152.                     matrix2 = getMatrix();
  153.                     union(matrix1, matrix2);
  154.                     break;
  155.  
  156.  
  157.             }
  158.  
  159.  
  160.         } while (choice != 0);
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.     }
  170. }
  171.  
  172.  
  173. /*
  174. run:
  175. Enter your choice:
  176. 1:Union
  177. 2:Intersection
  178. 3:MaxMin Composition.
  179. 0:exit
  180. 2
  181. Enter your relation matrix m,n
  182. 2 2
  183. Enter your relation matrix
  184. 0.2 0.3
  185. 0.1 0.5
  186. Enter your relation matrix m,n
  187. 2 2
  188. Enter your relation matrix
  189. 0.4 0.6
  190. 0.7 0.8
  191. Intersection is:
  192. [0.2, 0.3]
  193. [0.1, 0.5]
  194. Enter your choice:
  195. 1:Union
  196. 2:Intersection
  197. 3:MaxMin Composition.
  198. 0:exit
  199. 3
  200. Enter your first relation matrix m,n
  201. 3 2
  202. Enter your relation matrix
  203. 0.5 0.1 0.2 0.9 0.8 0.6
  204. Enter the second relation matrix m,n
  205. 2 3
  206. Enter your relation matrix
  207. 0.6 0.4 0.7 0.5 0.8 0.9
  208. [0.5, 0.4, 0.5]
  209. [0.5, 0.8, 0.9]
  210. [0.6, 0.6, 0.7]
  211. Enter your choice:
  212. 1:Union
  213. 2:Intersection
  214. 3:MaxMin Composition.
  215. 0:exit
  216.  
  217.  */
Add Comment
Please, Sign In to add comment