КАПУСТА: почитай,обсуди,отдохни!

Информация о пользователе

Привет, Гость! Войдите или зарегистрируйтесь.



Лаба)

Сообщений 1 страница 5 из 5

1

Заполнить матрицу ЛП, от левого нижнего угла по диагонали: влево - вверх.
Пример 3*3
4   7   9
2   5   8
1   3   6

public interface Matrix
{
    /**
   * Return the number of rows in the matrix.
   * @return the number of rows in the matrix.
   */
  public int getNumberOfRows();

  /**
   * Return the number of columns in the matrix.
   * @return the number of columns in the matrix.
   */
  public 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.
   * @throws InvalidMatrixElementException if element position is not within
   * matrix.
   */
  public double getElement(final int row, final int column)
    throws InvalidMatrixElementException;

  /**
   * Set element at position (row,column). If the element is not within
   * the matrix, the matrix is left unchanged.
   *
   * @param row row of element to set.
   * @param column column of element to set.
   * @param value value to store at (row,column).
   * @throws InvalidMatrixElementException if element position is not within
   * matrix.
   */
  public void setElement(final int row, final int column, final double value)
    throws InvalidMatrixElementException;

  /**
   * 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.
   * @param columns number of columns in new matrix. Must be greater than zero.
   * @return the new matrix or null if the size is invalid.
   * @throws InvalidMatrixSizeException if size of matrix is not valid
   * (row/column lessthan size 1).
   */
  public Matrix create(final int rows, final int columns)
    throws InvalidMatrixSizeException;
}

0

2

public abstract class AbstractMatrix implements Matrix
{
  public final boolean isSameSize(final Matrix m)
  {
    return (getNumberOfRows() == m.getNumberOfRows())
      && (getNumberOfColumns() == m.getNumberOfColumns());
  }
}

0

3

public class ArrayMatrix extends AbstractMatrix
{
  private double[][] elements;
 
  //Constructor
  public ArrayMatrix(final int rows, final int columns) throws InvalidMatrixSizeException
  {
    if ((rows<1)||(columns<1))
    {
      throw new InvalidMatrixSizeException();
    }
    elements = new double[rows][columns];
  }
 
  //Overriding methods declared in Matrix
  public int getNumberOfRows()
  {
    return elements.length;
  }
  public int getNumberOfColumns()
  {
    return elements[0].length;
  }
  /*-------------------------------------------------------------------------*/
  public double getElement(final int row, final int column)
    throws InvalidMatrixElementException
  {
    if ((row>getNumberOfRows())||(column>getNumberOfColumns()))
    {
      throw new InvalidMatrixElementException();
    }
      return elements[row][column];
  }
  /*-------------------------------------------------------------------------*/
  public void setElement(final int row, final int column, final double value)
    throws InvalidMatrixElementException
  {
    if ((row>getNumberOfRows())||(column>getNumberOfColumns()))
    {
      throw new InvalidMatrixElementException();
    }
    elements[row][column] = value;
  }
  /*-------------------------------------------------------------------------*/
 
  public Matrix create(final int rows, final int columns)
    throws InvalidMatrixSizeException
  {
    if ((rows<1)||(columns<1))
    {
      throw new InvalidMatrixSizeException();
    }
    return new ArrayMatrix(rows, columns);
  }
}

0

4

public class InvalidMatrixElementException extends RuntimeException
{
  public InvalidMatrixElementException()
  {
    super("Matrix element does not exist");
  }

  public InvalidMatrixElementException(final int row, final int column)
  {
    super("Matrix element (" + row + "," + column + ") does not exist");
  }
}

0

5

public class InvalidMatrixSizeException extends Exception
{
  public InvalidMatrixSizeException()
  {
    super("Matrix size invalid");
  }

  public InvalidMatrixSizeException(final int row, final int column)
  {
    super("Matrix of size (" + row + "," + column + ") is invalid");
  }

}

0