Created
October 13, 2016 23:39
-
-
Save delucas/3688316aedce0c84d5dc6355653d4373 to your computer and use it in GitHub Desktop.
Ejemplos de clase para Matriz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class Matriz { | |
private double[][] valores; | |
public Matriz(int i) { | |
this.valores = new double[i][i]; | |
} | |
public Matriz sumar(Matriz otro) { | |
Matriz resultado = new Matriz(this.valores.length); | |
for (int i = 0; i < valores.length; i++) { | |
for (int j = 0; j < valores[i].length; j++) { | |
resultado.set(i, j, | |
this.get(i, j) + otro.get(i, j)); | |
} | |
} | |
return resultado; | |
} | |
public void set(int i, int j, double valor) { | |
this.valores[i][j] = valor; | |
} | |
public double get(int i, int j) { | |
if(i<0 || i>=valores.length || j<0 || j>=valores.length){ | |
throw new Error ("no funciona"); | |
} | |
return this.valores[i][j]; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) | |
return true; | |
if (obj == null) | |
return false; | |
if (getClass() != obj.getClass()) | |
return false; | |
Matriz other = (Matriz) obj; | |
if (!Arrays.deepEquals(valores, other.valores)) | |
return false; | |
return true; | |
} | |
public Matriz multiplicarPor(double escalar) { | |
Matriz resultado = new Matriz(this.valores.length); | |
for (int i = 0; i < valores.length; i++) { | |
for (int j = 0; j < valores[i].length; j++) { | |
resultado.set(i, j, | |
this.get(i, j)*escalar); | |
} | |
} | |
return resultado; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Assert; | |
import org.junit.Test; | |
public class MatrizTests { | |
@Test | |
public void quePuedoHacerUnaSumaSimple() { | |
Matriz matriz1 = new Matriz(1); | |
matriz1.set(0, 0, 2); | |
Matriz matriz2 = new Matriz(1); | |
matriz2.set(0, 0, 1); | |
Matriz matriz3 = new Matriz(1); | |
matriz3.set(0, 0, 3); | |
Assert.assertEquals(matriz3, matriz1.sumar(matriz2)); | |
} | |
@Test | |
public void quePuedoCambiarLosValoresDeLaMatriz() { | |
Matriz matriz1 = new Matriz(1); | |
matriz1.set(0, 0, 10); | |
Assert.assertEquals(10, matriz1.get(0, 0), 0); | |
matriz1.set(0, 0, 5); | |
Assert.assertEquals(5, matriz1.get(0, 0), 0); | |
} | |
@Test | |
public void quePuedoEstablecerDistintasPosicionesDeLaMatriz() { | |
Matriz matriz1 = new Matriz(2); | |
matriz1.set(0, 0, 1); | |
matriz1.set(0, 1, 2); | |
matriz1.set(1, 0, 3); | |
matriz1.set(1, 1, 4); | |
Assert.assertEquals(1, matriz1.get(0, 0), 0); | |
Assert.assertEquals(2, matriz1.get(0, 1), 0); | |
Assert.assertEquals(3, matriz1.get(1, 0), 0); | |
Assert.assertEquals(4, matriz1.get(1, 1), 0); | |
} | |
@Test | |
public void quePuedoSumarDosMatricesNoTriviales() { | |
Matriz matriz1 = new Matriz(2); | |
Matriz matriz2 = new Matriz(2); | |
Matriz matriz3 = new Matriz(2); | |
matriz1.set(0, 0, 1); | |
matriz1.set(0, 1, 2); | |
matriz1.set(1, 0, 3); | |
matriz1.set(1, 1, 4); | |
matriz2.set(0, 0, 4); | |
matriz2.set(0, 1, 3); | |
matriz2.set(1, 0, 2); | |
matriz2.set(1, 1, 1); | |
matriz3.set(0, 0, 5); | |
matriz3.set(0, 1, 5); | |
matriz3.set(1, 0, 5); | |
matriz3.set(1, 1, 5); | |
Assert.assertEquals(matriz3, matriz1.sumar(matriz2)); | |
} | |
@Test | |
public void quePuedoMultiplicarUnaMatrizTrivial() { | |
Matriz matriz4 = new Matriz(2); | |
Matriz resultado = new Matriz(2); | |
Assert.assertEquals(resultado, matriz4.multiplicarPor(100)); | |
} | |
@Test | |
public void quePuedoMultiplicarUnaMatrizNoTrivial() { | |
Matriz matriz1 = new Matriz(2); | |
Matriz matriz2 = new Matriz(2); | |
matriz1.set(0, 0, 1); | |
matriz1.set(0, 1, 2); | |
matriz1.set(1, 0, 3); | |
matriz1.set(1, 1, 4); | |
matriz2.set(0, 0, 2); | |
matriz2.set(0, 1, 4); | |
matriz2.set(1, 0, 6); | |
matriz2.set(1, 1, 8); | |
Assert.assertEquals(matriz2, matriz1.multiplicarPor(2)); | |
} | |
@Test(expected = Error.class) | |
public void queNoPuedoAccederAUnaPosicionRestringida() { | |
Matriz matriz = new Matriz(2); | |
matriz.get(10, 10); | |
} | |
@Test(expected = Error.class) | |
public void queNoPuedoAccederAUnaPosicionRestringidaParaLasColumnas() { | |
Matriz matriz = new Matriz(2); | |
matriz.get(0, 10); | |
} | |
@Test(expected = Error.class) | |
public void queNoPuedoAccederAUnaPosicionRestringidaParaLasFilas() { | |
Matriz matriz = new Matriz(2); | |
matriz.get(10, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment