Asignación Nº 2 Fecha de Asignación: 15.06.06 Fecha de Entrega

Anuncio
Asignación Nº 2
Fecha de Asignación: 15.06.06 Fecha de Entrega: 29.06.06
O b j e t i v o : aplicar y entender el uso de interfaces, clases abstractas, herencia, polimorfismo y
uso de excepciones.
E j e r c i c i o . Construir un package matrices, que contenga interfaces y clases abstractas tales que
permitan crear una jerarquía de clases de matrices. Para ello usted dispone de:
1. Una Interface
Matrix que define el conjunto de métodos comunes a
cualquier
implementación de una matriz.
import java.io.*;
public interface Matrix {
/**
* Return the number of rows in the matrix.
* @return the number of rows in the matrix.
*/
public abstract int getNumberOfRows();
/**
* Return the number of columns in the matrix.
* @return the number of columns in the matrix.
*/
public abstract int getNumberOfColumns();
/**
* Return element at position (row,column).
* @param row row of element to return.
* @param column column of element to return.
* @return element value of element.
* @exception MatrixException if element is not within the bounds of the matrix.
*/
public abstract double getElement(int row, int column) throws MatrixException;
/**
* Set element at position (row,column). If the element is not within
* the matrix, the matrix is left unchanged and an exception is thrown.
*
* @param row row of element to set.
* @param column column of element to set.
* @param value value to store at (row,column).
* @exception MatrixException if element is not within the bounds of the matrix.
*/
public abstract void setElement(int row, int column, double value) throws
MatrixException;
/**
* Create a new concrete matrix of size (rows, columns). Must be overridden by a
subclass
* that names the actual class used to create the matrix.
* @param rows number of rows in new matrix. Must be greater than zero.
Universidad de Carabobo. Facultad de Ingeniería. Departamento de Computación. Computación Avanzada 1-2006.
Asignación Nº 2
Fecha de Asignación: 15.06.06 Fecha de Entrega: 29.06.06
* @param columns number of columns in new matrix. Must be greater than zero.
* @exception MatrixException if the size of the matrix is invalid. No
* valid matrix is created.
*/
public abstract Matrix create(int rows, int columns) throws MatrixException;
/**
* Add the argument matrix to <i>this</i> and return a new matrix containing the
result.
* The matrices must match in size for the addition to succeed.
* This is a generic method that can add matrices of any concrete implementation
* class.
* @param m matrix to add to <i>this</i>.
* @return new matrix containing the result.
* @exception MatrixException if matrices are of different sizes.
*/
public abstract Matrix add(Matrix m) throws MatrixException;
/**
* Subtract the argument matrix from <i>this</i> and return a new matrix
containing the result.
* The matrices must match in size for the subtraction to succeed.
* This is a generic method that can subtract matrices of any concrete
implementation
* class.
* @param m matrix to subtract from <i>this</i>.
* @return new matrix containing the result.
* @exception MatrixException if matrices are of different sizes.
*/
public abstract Matrix subtract(Matrix m) throws MatrixException;
/**
* Multiply the argument matrix with <i>this</i> and return a new matrix
containing the result.
* The matrix must valid sizes for the multiplication to succeed.
* This is a generic method that can multiply matrices of any concrete
implementation
* class.
* @param m matrix to multiply with <i>this</i>.
* @return new matrix containing the result.
* @exception MatrixException if matrices are of incompatible sizes for
multiplication.
*/
public abstract Matrix multiply(Matrix m) throws MatrixException;
/**
* Este es un método genérico que puede leer objetos matrices de cualquier clase
concreta
* @param obj BufferedReader asociado a un archivo tipo texto.
* @return un String con la última cadena leída del archivo.
*/
public abstract String leer(BufferedReader obj);
}
Universidad de Carabobo. Facultad de Ingeniería. Departamento de Computación. Computación Avanzada 1-2006.
Asignación Nº 2
Fecha de Asignación: 15.06.06 Fecha de Entrega: 29.06.06
2. La clase MatrixException
public class MatrixException extends Exception{
public MatrixException()
{
super("Invalid use of Matrix object has occurred");
}
public MatrixException(String message){
super(message);
}
}
3. Una clase abstracta AbstractMatrix que implementa los métodos de adición, sustracción y
multiplicación de la Interface Matrix. Considere que en esta clase, las implementaciones de
los métodos deben ser genéricos a cualquier matriz y que estos serán heredados por las
subclases de AbstractMatrix.
public abstract class AbstractMatrix implements Matrix{
public final Matrix add(Matrix m) throws MatrixException{
if ((getNumberOfRows() != m.getNumberOfRows()) ||
(getNumberOfColumns() != m.getNumberOfColumns())) {
throw new MatrixException("Unable to add matrices of different sizes");
}
Matrix result = create(getNumberOfRows(), getNumberOfColumns());
for (int row = 0; row < getNumberOfRows(); row++) {
for (int column = 0; column < getNumberOfColumns(); column++) {
double value = getElement(row, column) + m.getElement(row, column);
result.setElement(row, column, value);
}
}
return result;
}
public final Matrix subtract(Matrix m) throws MatrixException{
// El cuerpo de este método debe ser desarrollado por el estudiante
}
public final Matrix multiply(Matrix m) throws MatrixException{
}
// El cuerpo de este método debe ser desarrollado por el estudiante
public String toString(){
}
// El cuerpo de este método debe ser desarrollado por el estudiante
}
Universidad de Carabobo. Facultad de Ingeniería. Departamento de Computación. Computación Avanzada 1-2006.
Asignación Nº 2
Fecha de Asignación: 15.06.06 Fecha de Entrega: 29.06.06
Además de implementar los métodos substract y multiply de la clase AbstractMatrix, el
estudiante debe agregar a este package una subclase de AbstractMatrix
concreta (no
abstracta), que implemente una matriz usando un arreglo bidimensional de doubles. Esta
subclase debe implementar todos los métodos abstractos de Matrix excepto los métodos add,
substract y multiply (AbstractMatrix implementa Matrix, de modo que una subclase de
AbstractMatrix hereda todas las declaraciones de los métodos de Matrix). La subclase deberá
contener el arreglo y cualquier variable o métodos privados que sean necesarios (por ejemplo los
constructores de la clase).
<<Interface>>
AbstractMatrix
Matrix
Matrix add(Matrix m) throws
int getNumberOfRows()
MatrixException
int getNumberOfColumns()
Matrix subtract(Matrix m) throws
double
MatrixException
column)
Matrix multiply(Matrix m) throws
void
MatrixException
column, double value)
String toString()
Matrix
getElement(int
setElement(int
create(int
row,
int
row,
int
rows,
int
columns)
Matrix add(Matrix m)
Matrix subtract(Matrix m)
MatrizDoubles
Matrix multiply(Matrix m)
String leer(BufferedReader obj);
// Por desarrollar
Figura. Jerarquía de clases
Finalmente deberá construir una JavaMainClass Test para probar la clase derivada de
AbstractMatrix. En el método main se deben crear dos objetos de la clase derivada, leer desde
un archivo los datos de los dos objetos, luego debe obtenerse la suma, la resta y el producto de
los dos objetos. Finalmente cree un nuevo objeto de la clase derivada tal que al realizar cualquier
operación (suma, resta o multiplicación) de este con otro de los objetos anteriores se generen
Excepciones.
Debe entregar:Enunciado del problema, Listado del programa, Ejecución
Universidad de Carabobo. Facultad de Ingeniería. Departamento de Computación. Computación Avanzada 1-2006.
Descargar