Skip to content

Commit

Permalink
Añadimos excepciones
Browse files Browse the repository at this point in the history
  • Loading branch information
navarrozuara committed Nov 28, 2015
1 parent d3bed98 commit 0a6196c
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 39 deletions.
12 changes: 12 additions & 0 deletions ArrayMatematicos/css/estilos.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
table td{
width: 1em;
height: 1em;
border: 1px solid black;
text-align: center;
padding: 0.5em;
}

table{
margin: auto;
border-collapse: collapse;
}
45 changes: 25 additions & 20 deletions ArrayMatematicos/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
<!-- Bootstrap core CSS -->
<link href="css/bootstrap.css" rel="stylesheet">

<link rel="stylesheet" type="text/css" href="css/estilos.css">

<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->

<script type="text/javascript" src="js/arrayMatematicos.js"></script>
<script type="text/javascript" src="js/ejercicio.js"></script>
<script type="text/javascript" src="js/principal.js"></script>
</head>
<body>
<noscript>
Expand All @@ -44,49 +46,52 @@ <h1>PseudoClase ArrayMatematicos</h1>
<p>Una vez creada la clase, demuestra su funcionamiento en una página bien diseñada. Evita las cajas de texto y hazla lo más dinámica posible.</p>
<br />
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="col-md-6 col-md-offset-3">
<strong><div id="msgErr" class="text-danger"></div></strong>
</div>
<br />
<div class="col-md-6 col-md-offset-3 well well-lg">
<br /><br />
<div class="col-md-6 col-md-offset-3">
<article>
<h2>Matriz 1</h2>
<label class="col-sm-2 control-label">Filas:</label>
<div class="col-sm-4">
<input type="number" id="filas1" min="1" max="20" required />
<label class="col-sm-2">Filas:</label>
<div class="col-sm-3">
<input type="number" class="form-control input-sm" id="filas1" required />
</div>
<label class="col-sm-2 control-label">Columnas:</label>
<div class="col-sm-4">
<input type="number" id="columnas1" min="1" max="20" required />
<label class="col-sm-2">Columnas:</label>
<div class="col-sm-3">
<input type="number" class="form-control input-sm" id="columnas1" required />
</div>
<br />
<br /><br />
<h2>Matriz 2</h2>
<label class="col-sm-2 control-label">Filas:</label>
<div class="col-sm-4">
<input type="number" id="filas2" min="1" max="20" required />
<label class="col-sm-2">Filas:</label>
<div class="col-sm-3">
<input type="number" class="form-control input-sm" id="filas2" required />
</div>
<label class="col-sm-2 control-label">Columnas:</label>
<div class="col-sm-4">
<input type="number" id="columnas2" min="1" max="20" required />
<label class="col-sm-2">Columnas:</label>
<div class="col-sm-3">
<input type="number" class="form-control input-sm" id="columnas2" required />
</div>
<br /><br /><br />
<div class="text-center">
<div class="btn-group">
<button type="button" id="generar" class="btn btn-primary">Generar matrices</button>
<button type="button" id="sumar" class="btn btn-primary">Sumar</button>
<button type="button" id="restar" class="btn btn-primary">Restar</button>
<button type="button" id="multiplicar" class="btn btn-primary">Multiplicar</button>
<button type="button" id="trasponer" class="btn btn-primary">Trasponer</button>
<button type="button" id="clear" class="btn btn-danger">Limpiar</button>
</div>
</div>
<br /><br /><br />
</article>
</div>
<div class="col-md-6">
<div class="col-md-4">
<div id="matriz1" class="lead text-center"></div>
</div>
<div class="col-md-6">
<div class="col-md-4">
<div id="matriz2" class="lead text-center"></div>
</div>
<div class="col-md-6 col-md-offset-3 ">
<div class="col-md-4">
<div id="resultado" class="lead text-center"></div>
</div>
</div>
Expand Down
46 changes: 27 additions & 19 deletions ArrayMatematicos/js/arrayMatematicos.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Evita las cajas de texto y hazla lo más dinámica posible.
*/

function ArrayMatematicos(filas, columnas) {
if (filas < 1 || columnas < 1 )
throw new FilasColumnasError("El número de filas y columnas ha de ser mayor de 0.");
this.filas = filas;
this.columnas = columnas;
this.contenido = this.inicializarArray();
Expand All @@ -22,19 +24,21 @@ ArrayMatematicos.prototype.inicializarArray = function() {
for (var i = 0; i < this.filas; i++) {
matriz[i] = new Array();
for (var j = 0; j < this.columnas; j++)
matriz[i][j] = Math.round(Math.random()*11);
matriz[i][j] = Math.round(Math.random()*10);
}
return matriz;
}

ArrayMatematicos.prototype.mostrar = function() {
var cadena = "";
var cadena = "<table>";
for (var i = 0; i < this.filas; i++) {
cadena += "<tr>";
for (var j = 0; j < this.columnas; j++) {
cadena += this.contenido[i][j] + " ";
cadena += "<td>"+this.contenido[i][j]+"</td>";
}
cadena += "<br />";
cadena += "</tr>";
}
cadena += "</table>";
return cadena;
}

Expand All @@ -50,11 +54,20 @@ ArrayMatematicos.prototype.comprobarDimensionesM = function(matriz) {
return false;
}

function FilasColumnasError(message) {
this.name = "FilasColumnasError";
this.message = message;
}

function DimensionesInvalidasError(message) {
this.name = "DimensionesError";
this.message = message;
}

ArrayMatematicos.prototype.sumar = function(matriz2) {
var resultado;
if (!this.comprobarDimensiones(matriz2)) {
throw new ExceptionDimensiones("Las dimensiones introducidas no son válidas. Las filas y las columnas de ambas matrices han de ser iguales.");
}
if (!this.comprobarDimensiones(matriz2))
throw new DimensionesInvalidasError("Las dimensiones introducidas no son válidas. Las filas y las columnas de ambas matrices han de ser iguales.");
resultado = new ArrayMatematicos(this.filas, this.columnas);
for (var i = 0; i < this.filas; i++)
for (var j = 0; j < this.columnas; j++)
Expand All @@ -64,9 +77,8 @@ ArrayMatematicos.prototype.sumar = function(matriz2) {

ArrayMatematicos.prototype.restar = function(matriz2) {
var resultado;
if (!this.comprobarDimensiones(matriz2)) {
throw new ExceptionDimensiones("Las dimensiones introducidas no son válidas. Las filas y las columnas de ambas matrices han de ser iguales.");
}
if (!this.comprobarDimensiones(matriz2))
throw new DimensionesInvalidasError("Las dimensiones introducidas no son válidas. Las filas y las columnas de ambas matrices han de ser iguales.");
resultado = new ArrayMatematicos(this.filas, this.columnas);
for (var i = 0; i < this.filas; i++)
for (var j = 0; j < this.columnas; j++)
Expand All @@ -76,9 +88,8 @@ ArrayMatematicos.prototype.restar = function(matriz2) {

ArrayMatematicos.prototype.multiplicar = function(matriz2) {
var resultado;
if (!this.comprobarDimensionesM(matriz2)) {
throw new ExceptionDimensiones("Las dimensiones introducidas no son válidas. Las columnas de la matriz1 han de coincidir con las filas de la matriz2.");
}
if (!this.comprobarDimensionesM(matriz2))
throw new DimensionesInvalidasError("Las dimensiones introducidas no son válidas. Las columnas de la matriz1 han de coincidir con las filas de la matriz2.");
resultado = new ArrayMatematicos(this.filas, matriz2.columnas);
for (var i = 0; i < this.filas; i++) {
for (var j = 0; j < matriz2.columnas; j++) {
Expand All @@ -96,10 +107,7 @@ ArrayMatematicos.prototype.trasponer = function() {
for (var i = 0; i < this.columnas; i++)
for (var j = 0; j < this.filas; j++)
traspuesta.contenido[i][j] = this.contenido[j][i];
return traspuesta;
}

function ExceptionDimensiones(mensaje) {
this.mensaje = mensaje;
this.nombre = "ExceptionDimensiones";
this.contenido = traspuesta.contenido;
this.filas = traspuesta.filas;
this.columnas = traspuesta.columnas;
}
134 changes: 134 additions & 0 deletions ArrayMatematicos/js/principal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**
Mediante prototype, agrega los métodos sumar, restar, trasponer y multiplicar
a la clase ArraysMatematicos.
Recuerda que el estado de un array deberían de ser los elementos.
Recuerda las restricciones y posibilidades de un Array:
1. Sobre las dimensines de los arrays implicados (unidimensional, bidimensional...).
2. Sobre las longitudes de los arrays implicados (1 en adelante).
3. Sobre los contenidos de los arrays implicados (numéricos).
4. Podemos rellenar un Array con valores aleatorios o directamente desde teclado.
Una vez creada la clase, demuestra su funcionamiento en una página bien diseñada.
Evita las cajas de texto y hazla lo más dinámica posible.
*/

var misGlobales = {
m1: undefined,
m2: undefined
}

function inicio() {
var resultado;
var filas1, columnas1, filas2, columnas2;
var msgErr = document.getElementById("msgErr");
var matriz1 = document.getElementById("matriz1");
var matriz2 = document.getElementById("matriz2");
var msgResultado = document.getElementById("resultado");

function validarDatos(dato) {
if (isNaN(dato)) {
msgErr.innerHTML = "Debe introducir números.";
return false;
} else if (dato < 1) {
msgErr.innerHTML = "El número de filas y columnas ha de ser mayor de 0.";
return false;
} else if (dato % 1 != 0) {
msgErr.innerHTML = "No se permiten números decimales.";
return false;
}
msgErr.innerHTML = "";
return true;
}

function generarMatrices() {
filas1 = document.getElementById("filas1").value;
columnas1 = document.getElementById("columnas1").value;
filas2 = document.getElementById("filas2").value;
columnas2 = document.getElementById("columnas2").value;

if (validarDatos(filas1) && validarDatos(columnas1) && validarDatos(filas2) && validarDatos(columnas2)) {
try {
misGlobales.m1 = new ArrayMatematicos(filas1, columnas1);
misGlobales.m2 = new ArrayMatematicos(filas2, columnas2);
matriz1.innerHTML = "MATRIZ 1:<br />" + misGlobales.m1.mostrar();
matriz2.innerHTML = "MATRIZ 2:<br />" + misGlobales.m2.mostrar();
} catch (e) {
msgErr.innerHTML = e.message;
}
}
}

function sumarMatrices() {
if (misGlobales.m1 === undefined || misGlobales.m2 === undefined) {
msgErr.innerHTML = "Antes de realizar la suma tienes que crear las matrices.";
return;
}
msgErr.innerHTML = "";
try {
resultado = misGlobales.m1.sumar(misGlobales.m2);
msgResultado.innerHTML = "SUMA:<br />" + resultado.mostrar();
} catch (e) {
msgErr.innerHTML = e.message;
msgResultado.innerHTML = "";
}
}

function restarMatrices() {
if (misGlobales.m1 === undefined || misGlobales.m2 === undefined) {
msgErr.innerHTML = "Antes de realizar la resta tienes que crear las matrices.";
return;
}
msgErr.innerHTML = "";
try {
resultado = misGlobales.m1.restar(misGlobales.m2);
msgResultado.innerHTML = "RESTA:<br />" + resultado.mostrar();
} catch (e) {
msgErr.innerHTML = e.message;
msgResultado.innerHTML = "";
}
}

function multiplicarMatrices() {
if (misGlobales.m1 === undefined || misGlobales.m2 === undefined) {
msgErr.innerHTML = "Antes de realizar la multiplicacion tienes que crear las matrices.";
return;
}
msgErr.innerHTML = "";
try {
resultado = misGlobales.m1.multiplicar(misGlobales.m2);
msgResultado.innerHTML = "PRODUCTO:<br />" + resultado.mostrar();
} catch (e) {
msgErr.innerHTML = e.message;
msgResultado.innerHTML = "";
}
}

function trasponerMatrices() {
if (misGlobales.m1 === undefined || misGlobales.m2 === undefined) {
msgErr.innerHTML = "Antes de realizar la traspuesta tienes que crear las matrices.";
return;
}
msgErr.innerHTML = "";
misGlobales.m1.trasponer();
misGlobales.m2.trasponer();
msgResultado.innerHTML = "TRASPUESTA MATRIZ 1:<br />" + misGlobales.m1.mostrar() +
"<br />TRASPUESTA MATRIZ 2:<br />" + misGlobales.m2.mostrar();
}

function limpiarMatrices() {
misGlobales.m1 = undefined;
misGlobales.m2 = undefined;
resultado = "";
matriz1.innerHTML = "";
matriz2.innerHTML = "";
msgResultado.innerHTML = "";
}

document.getElementById("generar").addEventListener("click", generarMatrices);
document.getElementById("sumar").addEventListener("click", sumarMatrices);
document.getElementById("restar").addEventListener("click", restarMatrices);
document.getElementById("multiplicar").addEventListener("click", multiplicarMatrices);
document.getElementById("trasponer").addEventListener("click", trasponerMatrices);
document.getElementById("clear").addEventListener("click", limpiarMatrices);
}

window.addEventListener("load", inicio);

0 comments on commit 0a6196c

Please sign in to comment.