HayCZ

ipalp_matice_FEI2

Apr 23rd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 KB | None | 0 0
  1. package izapr_cv07;
  2.  
  3. import java.util.Locale;
  4.  
  5. public class Matice {
  6.  
  7.     private final double[][] pole;
  8.     private final int pocetRadku;
  9.     private final int pocetSloupcu;
  10.  
  11.     public Matice(final int pocetRadku, final int pocetSloupcu) {
  12.         if (0 < pocetRadku && 0 < pocetSloupcu) {
  13.             this.pole = new double[pocetRadku][pocetSloupcu];
  14.             this.pocetRadku = pocetRadku;
  15.             this.pocetSloupcu = pocetSloupcu;
  16.         } else {
  17.             pole = null;
  18.             this.pocetRadku = 0;
  19.             this.pocetSloupcu = 0;
  20.         }
  21.  
  22.     }
  23.  
  24.     public Matice() {
  25.         this(0, 0);
  26.     }
  27.  
  28.     public String toString(String format) {
  29.         StringBuilder str = new StringBuilder();
  30.         for (int i = 0; i < pocetRadku; i++) {
  31.             for (int j = 0; j < pocetSloupcu; j++) {
  32.                 str.append(String.format(
  33.                         Locale.ENGLISH,
  34.                         format, pole[i][j]));
  35.             }
  36.             str.append('\n');
  37.         }
  38.         return str.toString();
  39.     }
  40.  
  41.     @Override
  42.     public String toString() {
  43.         return this.toString("%06.2f ");
  44.     }
  45.  
  46.     public int getPocetRadku() {
  47.         return pocetRadku;
  48.     }
  49.  
  50.     public int getPocetSloupcu() {
  51.         return pocetSloupcu;
  52.     }
  53.        
  54.     public double[] diagonala(){
  55.         //Lokální proměná
  56.         int delka = (pocetRadku<pocetSloupcu)?pocetRadku:pocetSloupcu; //podmínka zkráceně.
  57.         //Referenční promněná
  58.         double[] poleDiagonala = new double[delka];
  59.        /* if(pocetRadku<pocetSloupcu){
  60.             delka = pocetRadku;
  61.         }else{
  62.             delka = pocetSloupcu;
  63.         }*/
  64.         for (int i = 0; i<delka;i++)
  65. {
  66.             poleDiagonala[i] = this.pole[i][i];
  67.         }
  68.         return poleDiagonala;
  69.     }
  70.     public double[] vedlejsiDiagonala(){
  71.         //Lokální proměná
  72.         int delka = (pocetRadku<pocetSloupcu)?pocetRadku:pocetSloupcu; //podmínka zkráceně.
  73.         //Referenční promněná
  74.         double[] poleDiagonala = new double[delka];
  75.         int y = 0;
  76.         for (int i = 0; i < delka-1; i++) {
  77.             y = delka - i-1;
  78.             poleDiagonala[i] = this.pole[i][y];
  79.         }
  80.         return poleDiagonala;
  81.     }
  82.     public void napln(int min, int max){
  83.         for (int i = 0; i < pocetRadku; i++) {
  84.             for (int j = 0; j < pocetSloupcu; j++) {
  85.                 pole[i][j] = Math.round(Math.random()*(max-min) + min);
  86.             }
  87.         }
  88.     }
  89.     public Matice horniNadHlavniDiagonalou(){
  90.             Matice m = new Matice(this.pocetRadku, this.pocetSloupcu);
  91.             for (int i = 0; i < pocetRadku; i++) {
  92.                 for (int j = i+1; j < pocetSloupcu; j++) {
  93.                     m.pole[i][j] = this.pole[i][j];
  94.                 }
  95.         }
  96.            
  97.             return m;
  98.     }
  99.     public Matice dolniPodHlavniDiagonalou(){
  100.             Matice m = new Matice(this.pocetRadku, this.pocetSloupcu);
  101.             for (int i = 1; i < pocetRadku; i++) {
  102.                 for (int j = 0; j < i; j++) {
  103.                     m.pole[i][j] = this.pole[i][j];
  104.                 }
  105.         }
  106.            
  107.             return m;
  108.     }
  109.     public Matice horniNadVedlejsiDiagonalou(){
  110.             Matice m = new Matice(this.pocetRadku, this.pocetSloupcu);
  111.             for (int i = 0; i < pocetRadku-1; i++) {
  112.                 for (int j = 0; j < pocetSloupcu-i-2; j++) {
  113.                     m.pole[i][j] = this.pole[i][j];
  114.                 }
  115.         }
  116.            
  117.             return m;
  118.     }
  119.     public Matice dolniPodVedlejsiDiagonalou(){
  120.         int delka = (pocetRadku<pocetSloupcu)?pocetRadku:pocetSloupcu;
  121.             Matice m = new Matice(this.pocetRadku, this.pocetSloupcu);
  122.             for (int i = 1; i < pocetRadku; i++) {
  123.                 for (int j = pocetSloupcu; pocetSloupcu -1 -1<j ; j--) {
  124.                     m.pole[i][j] = this.pole[i][j];
  125.                 }
  126.         }
  127.            
  128.             return m;
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment