LukasHuhn

MyMatrix

Jun 9th, 2014
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.31 KB | None | 0 0
  1. public class MyMatrix {
  2.     // Variablen
  3.         public int m;           //Zeilen
  4.         public int n;           //Spalten
  5.         public int[][] M;       //mxn-Matrix M
  6.         // Konstruktoren
  7.         public MyMatrix(int n){                     //Ab hier 2a
  8.             m=n;
  9.             this.n=n;
  10.             M = new int [n][n];
  11.         }
  12.         public MyMatrix (int m, int n) {
  13.             this.m = m;
  14.             this.n = n;
  15.             M = new int [m][n];
  16.         }  
  17.         public MyMatrix transpose (){
  18.             MyMatrix temp = new MyMatrix(n, m);
  19.             for(int a = 0; a < n ; a++){
  20.                 for(int b = 0; b < m ; b++){
  21.                     temp.M[a][b] = this.M[b][a];
  22.                 }
  23.             }
  24.             return temp;
  25.         }  
  26.         public void matrixOut() {
  27.         for (int i = 0; i < M.length; i++) {
  28.             for (int j = 0; j < M[i].length; j++){
  29.                 System.out.print( M[i][j]+"\t");
  30.                 }
  31.             System.out.println();
  32.             }
  33.         }
  34.         public void fill (int k){                       //Ab hier Aufgabe 2b
  35.             if(k<1)
  36.                 return;
  37.             int zaehler = 0;
  38.             for (int x = 0; x < M.length; x++){         //Zeilenschleife
  39.                 for(int y = 0; y < M[x].length;y++){    //Spaltenschleife
  40.                     if(zaehler>=k){
  41.                         zaehler=0;
  42.                         k++;
  43.                     }
  44.                     M[x][y] = k;
  45.                     zaehler++;
  46.                 }
  47.             }
  48.         }
  49.         public void fill2 (int k, int l) {             
  50.             if (l < 1)
  51.                 return;
  52.             int zaehler = 0;
  53.             for (int x = 0; x < M.length; x++){    
  54.                 for(int y = 0; y < M[x].length;y++){
  55.                     M[x][y] = k+zaehler;
  56.                     zaehler++;
  57.                     if(zaehler > l)
  58.                         zaehler =0;
  59.                 }
  60.             }
  61.         }
  62.         public int countSymm(){                         //Aufgabe 2c
  63.             //i  j und M[i][j]==M[j][i])
  64.             int zaehler = 0;
  65.             for (int i = 0; i < M.length; i++){    
  66.                 for(int j = 0; j < M[i].length;j++){
  67.                     if(j<i){
  68.                         if(M[i][j]==M[j][i]){
  69.                             System.out.println("Found Symm at : "+i+" and "+j);
  70.                             zaehler++;
  71.                         }
  72.                     }
  73.                 }
  74.             }
  75.             return zaehler;
  76.         }
  77.         public boolean checkSymm(){
  78.             if (n != m)
  79.                 return false;
  80.             for (int i = 0; i < M.length; i++){    
  81.                 for(int j = 0; j < M[i].length;j++){
  82.                     if(j<i){
  83.                         if(M[i][j]!=M[j][i]){
  84.                             return false;
  85.                         }
  86.                     }
  87.                 }
  88.             }
  89.             return true;
  90.         }
  91.         public int countDiffCols() {                //Ab hier Aufgabe 2d
  92.             int zaehler =0;
  93.             MyMatrix temp = this.transpose();
  94.             for(int x=0;x<temp.M.length;x++){
  95.                 for(int y=x+1;y<temp.M.length;y++){
  96.                     if(this.isEqualCols(temp.M[x], temp.M[y])==true)
  97.                         zaehler++;
  98.                 }
  99.             }
  100.             return zaehler;
  101.         }
  102.         public int maxNumberEqualCols() {
  103.             int zaehler =0;
  104.             MyMatrix temp = this.transpose();
  105.             for(int x=0;x<temp.M.length;x++){
  106.                 for(int y=x+1;y<temp.M.length;y++){
  107.                     if(this.isEqualCols(temp.M[x], temp.M[y])==false)
  108.                         zaehler++;
  109.                 }
  110.             }
  111.             return zaehler;
  112.         }
  113.         public boolean isEqualCols(int[] a, int[] b){ //Hilfsfunktion aufgabe 2d
  114.             for(int i=0; i<a.length;i++){
  115.                 if(a[i]!=b[i])
  116.                     return false;
  117.             }
  118.             return true;
  119.         }
  120.         //Hauptmetode
  121.         public static void main(String[] args) {
  122.             //Bsp1
  123.             System.out.println ("Beispiel 1");
  124.             MyMatrix bsp1 = new MyMatrix(6);
  125.             bsp1.fill2(0, 4);
  126.             /*
  127.              * Bei jeder Zahl, die zwei kleiner ist als die laenge einer
  128.              * Matrix mit gleicher laenge und breite, muss sich symmetrie
  129.              * ergeben, weil ...
  130.              */
  131.             bsp1.matrixOut();
  132.             System.out.println(bsp1.checkSymm());
  133.            
  134.             //Bsp2
  135.             System.out.println ("Beispiel 2");
  136.             MyMatrix bsp2 = new MyMatrix(5);
  137.             bsp2.fill2 (0, 1);
  138.             bsp2.matrixOut();
  139.             System.out.println(bsp2.checkSymm());  
  140.         }
  141.    
  142. }
Advertisement
Add Comment
Please, Sign In to add comment