package lezione10; import java.util.Random; public class MatriceDouble implements InterfacciaMatriceDoubleEx { private double[][] matrice; private int prossimaRigaPerRighe; private int prossimaColonnaPerRighe; private int prossimaRigaPerColonne; private int prossimaColonnaPerColonne; private double[] flatRows; private int nextRow; private double[] flatCols; private int nextCol; private boolean firstRowAlt; private boolean firstColAlt; public MatriceDouble(int nRighe, int nColonne) { this.matrice = new double[nRighe][nColonne]; this.prossimaRigaPerRighe = 0; this.prossimaColonnaPerRighe = 0; this.prossimaRigaPerColonne = 0; this.prossimaColonnaPerColonne = 0; this.flatRows = new double[nRighe * nColonne]; this.flatCols = new double[nRighe * nColonne]; this.nextRow = 0; this.nextCol = 0; this.firstRowAlt = true; this.firstColAlt = true; } @Override public void riempiACaso(double maxVal) { Random rand = new Random(); // MAGIA NERA: creiamo un generatore di numeri casuali for (int i = 0; i < this.matrice.length; i++) { for (int j = 0; j < this.matrice[0].length; j++) { this.matrice[i][j] = rand.nextDouble() * maxVal; // MAGIA NERA: otteniamo un intero casuale tra 0 e maxVal-1 (inclusi) } } } @Override public double[][] asArray() { return this.matrice; } @Override public void visualizza() { for (int i = 0; i < this.matrice.length; i++) { for (int j = 0; j < this.matrice[0].length; j++) { System.out.printf("%02.2f ", this.matrice[i][j]); } System.out.println(); } System.out.println("----------"); } @Override public double leggiCella(int iRiga, int iColonna) { return this.matrice[iRiga][iColonna]; } @Override public void scriviCella(int iRiga, int iColonna, double valore) { this.matrice[iRiga][iColonna] = valore; } public double[] leggiRiga(int iRiga) { return this.matrice[iRiga]; } @Override public void scriviRiga(int iRiga, double[] riga) { this.matrice[iRiga] = riga; } @Override public double[] leggiColonna(int iColonna) { double[] colonna = new double[this.matrice.length]; for (int j = 0; j < this.matrice.length; j++) { colonna[j] = this.matrice[j][iColonna]; } return colonna; } @Override public void scriviColonna(int iColonna, double[] colonna) { for (int i = 0; i < colonna.length; i++) { for (int j = 0; j < this.matrice.length; j++) { this.matrice[j][iColonna] = colonna[i]; } } } @Override public int numRighe() { return this.matrice.length; } @Override public int numColonne() { return this.matrice[0].length; } @Override public int numCelle() { return this.matrice.length * this.matrice[0].length; } @Override public double prossimaCellaPerRighe() { if (this.prossimaColonnaPerRighe >= this.matrice[0].length) { if (this.prossimaRigaPerRighe >= this.matrice.length - 1) { this.prossimaRigaPerRighe = 0; this.prossimaColonnaPerRighe = 1; return this.matrice[0][0]; } this.prossimaRigaPerRighe++; this.prossimaColonnaPerRighe = 1; return this.matrice[this.prossimaRigaPerRighe][0]; } double cella = this.matrice[this.prossimaRigaPerRighe][this.prossimaColonnaPerRighe]; this.prossimaColonnaPerRighe++; return cella; } // @Override public double prossimaCellaPerRigheAlt() { if (this.firstRowAlt) { this.firstRowAlt = false; int k = 0; for (int i = 0; i < this.matrice.length; i++) { for (int j = 0; j < this.leggiRiga(i).length; j++) { this.flatRows[k] = this.leggiRiga(i)[j]; k++; } } } double cella; if (this.nextRow < this.flatRows.length) { cella = this.flatRows[this.nextRow]; this.nextRow++; } else { cella = this.flatRows[0]; this.nextRow = 1; } return cella; } @Override public double prossimaCellaPerColonne() { if (this.prossimaRigaPerColonne >= this.matrice.length) { if (this.prossimaColonnaPerColonne >= this.matrice[0].length - 1) { this.prossimaRigaPerColonne = 1; this.prossimaColonnaPerColonne = 0; return this.matrice[0][0]; } this.prossimaRigaPerColonne = 1; this.prossimaColonnaPerColonne++; return this.matrice[0][this.prossimaColonnaPerColonne]; } double cella = this.matrice[this.prossimaRigaPerColonne][this.prossimaColonnaPerColonne]; this.prossimaRigaPerColonne++; return cella; } // @Override public double prossimaCellaPerColonneAlt() { if (this.firstColAlt) { this.firstColAlt = false; int k = 0; for (int i = 0; i < this.matrice[0].length; i++) { for (int j = 0; j < this.leggiColonna(i).length; j++) { this.flatCols[k] = this.leggiColonna(i)[j]; k++; } } } double cella; if (this.nextCol < this.flatCols.length) { cella = this.flatCols[this.nextCol]; this.nextCol++; } else { cella = this.flatCols[0]; this.nextCol = 1; } return cella; } }