HayCZ

Java - TestovaciZapocetTrida

Apr 26th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. package tryzapocet;
  2.  
  3. public class Matice {
  4.  
  5.     private final int[][] pole;
  6.  
  7.     public Matice(int pocetRadku, int pocetSloupcu) {
  8.         if (pocetRadku > 0 && pocetSloupcu > 0) {
  9.             pole = new int[pocetRadku][pocetSloupcu];
  10.         } else {
  11.             pole = null;
  12.             System.out.println("Nelze vytvořit. Matici nelze vytvořit.");
  13.         }
  14.     }
  15.  
  16.     public Matice(int velikost) {
  17.         this(velikost, velikost);
  18.     }
  19.  
  20.     @Override
  21.     public String toString() {
  22.         StringBuilder str = new StringBuilder();
  23.         for (int i = 0; i < pole.length; i++) {
  24.             for (int j = 0; j < pole[i].length; j++) {
  25.                 str.append(String.format("%4d ", pole[i][j]));
  26.             }
  27.             str.append('\n');
  28.         }
  29.         return str.toString();
  30.     }
  31.  
  32.     public void naplnMatici(int min, int max) {
  33.         for (int i = 0; i < pole.length; i++) {
  34.             for (int j = 0; j < pole[i].length; j++) {
  35.                 pole[i][j] = (int) Math.round(Math.random() * (max - min) + min);
  36.             }
  37.         }
  38.     }
  39.  
  40.     public Matice odectiRadky(int radek1, int radek2) {
  41.         Matice m = this;
  42.         if ((radek1 >= 0 && radek1 < m.pole.length) && (radek2 >= 0 && radek2 < m.pole.length) && (radek1 != radek2) && (m.pole.length == m.pole[0].length) ){
  43.             int[] temp = new int[m.pole.length];
  44.             for (int i = 0; i < m.pole.length; i++) {
  45.                temp[i] = m.pole[radek1][i] - m.pole[radek2][i];
  46.             }
  47.             for (int i = 0; i < pole.length; i++) {
  48.                 m.pole[i][2] = temp[i];
  49.             }
  50.         }
  51.         else
  52.         {
  53.             System.out.println("Nelze odečíst řádky. Neplatné zadání! Matice ponechána v původním stavu:");
  54.         }
  55.         return m;
  56.     }
  57.    
  58.     public Matice setridSloupec(int sloupec)
  59.     {
  60.         Matice m1 = this;
  61.         for (int i = 0; i < m1.pole.length - 1; i++) { //Cyklus s určitým počtem opakování
  62.             for (int j = 0; j < m1.pole.length - i - 1; j++) { //Cyklus s určitým počtem opakování
  63.                 if (m1.pole[j][sloupec] < m1.pole[j + 1][sloupec]) {
  64.                     int tmp = m1.pole[j][sloupec];
  65.                     m1.pole[j][sloupec] = m1.pole[j + 1][sloupec];
  66.                     m1.pole[j + 1][sloupec] = tmp;
  67.                 }
  68.             }
  69.         }
  70.         return m1;
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment