Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyMatrix {
- // Variablen
- public int m; //Zeilen
- public int n; //Spalten
- public int[][] M; //mxn-Matrix M
- // Konstruktoren
- public MyMatrix(int n){ //Ab hier 2a
- m=n;
- this.n=n;
- M = new int [n][n];
- }
- public MyMatrix (int m, int n) {
- this.m = m;
- this.n = n;
- M = new int [m][n];
- }
- public MyMatrix transpose (){
- MyMatrix temp = new MyMatrix(n, m);
- for(int a = 0; a < n ; a++){
- for(int b = 0; b < m ; b++){
- temp.M[a][b] = this.M[b][a];
- }
- }
- return temp;
- }
- public void matrixOut() {
- for (int i = 0; i < M.length; i++) {
- for (int j = 0; j < M[i].length; j++){
- System.out.print( M[i][j]+"\t");
- }
- System.out.println();
- }
- }
- public void fill (int k){ //Ab hier Aufgabe 2b
- if(k<1)
- return;
- int zaehler = 0;
- for (int x = 0; x < M.length; x++){ //Zeilenschleife
- for(int y = 0; y < M[x].length;y++){ //Spaltenschleife
- if(zaehler>=k){
- zaehler=0;
- k++;
- }
- M[x][y] = k;
- zaehler++;
- }
- }
- }
- public void fill2 (int k, int l) {
- if (l < 1)
- return;
- int zaehler = 0;
- for (int x = 0; x < M.length; x++){
- for(int y = 0; y < M[x].length;y++){
- M[x][y] = k+zaehler;
- zaehler++;
- if(zaehler > l)
- zaehler =0;
- }
- }
- }
- public int countSymm(){ //Aufgabe 2c
- //i j und M[i][j]==M[j][i])
- int zaehler = 0;
- for (int i = 0; i < M.length; i++){
- for(int j = 0; j < M[i].length;j++){
- if(j<i){
- if(M[i][j]==M[j][i]){
- System.out.println("Found Symm at : "+i+" and "+j);
- zaehler++;
- }
- }
- }
- }
- return zaehler;
- }
- public boolean checkSymm(){
- if (n != m)
- return false;
- for (int i = 0; i < M.length; i++){
- for(int j = 0; j < M[i].length;j++){
- if(j<i){
- if(M[i][j]!=M[j][i]){
- return false;
- }
- }
- }
- }
- return true;
- }
- public int countDiffCols() { //Ab hier Aufgabe 2d
- int zaehler =0;
- MyMatrix temp = this.transpose();
- for(int x=0;x<temp.M.length;x++){
- for(int y=x+1;y<temp.M.length;y++){
- if(this.isEqualCols(temp.M[x], temp.M[y])==true)
- zaehler++;
- }
- }
- return zaehler;
- }
- public int maxNumberEqualCols() {
- int zaehler =0;
- MyMatrix temp = this.transpose();
- for(int x=0;x<temp.M.length;x++){
- for(int y=x+1;y<temp.M.length;y++){
- if(this.isEqualCols(temp.M[x], temp.M[y])==false)
- zaehler++;
- }
- }
- return zaehler;
- }
- public boolean isEqualCols(int[] a, int[] b){ //Hilfsfunktion aufgabe 2d
- for(int i=0; i<a.length;i++){
- if(a[i]!=b[i])
- return false;
- }
- return true;
- }
- //Hauptmetode
- public static void main(String[] args) {
- //Bsp1
- System.out.println ("Beispiel 1");
- MyMatrix bsp1 = new MyMatrix(6);
- bsp1.fill2(0, 4);
- /*
- * Bei jeder Zahl, die zwei kleiner ist als die laenge einer
- * Matrix mit gleicher laenge und breite, muss sich symmetrie
- * ergeben, weil ...
- */
- bsp1.matrixOut();
- System.out.println(bsp1.checkSymm());
- //Bsp2
- System.out.println ("Beispiel 2");
- MyMatrix bsp2 = new MyMatrix(5);
- bsp2.fill2 (0, 1);
- bsp2.matrixOut();
- System.out.println(bsp2.checkSymm());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment