Montag, 13. Mai 2013

 

Übung zu Klassen und Testen (POS1: 2BHIF)

Erstellen Sie eine Klasse IntTable, welches das folgende Interface IIntTable implementiert. Die nötigen Methoden und deren Zweck ist im Interface in den Kommentaren beschrieben.
// @formatter:off
/**
 * Project: IntTable
 * Package: bo
 * File:    IIntTable.java
 * Created: May 13, 2013, Harald R. Haberstroh
 */
// @formatter:on
package bo;

/**
 * Describe interface of IntTable.
 * 
 * @author Harald R. Haberstroh (May 13, 2013)
 *
 */
public interface IIntTable {
    /**
     * Add one empty row (extend the table by one row).
     */
    public void addRow();
    
    /**
     * add an int array as new row (at the end).
     * numCols will be increased by one.
     * @param row int array with data
     * @throws IllegalArgumentException if row.size != numCols()
     */
    public void addRow(int[] row) throws IllegalArgumentException;
    
    /**
     * Add one empty column (extend the table by one column).
     */
    public void addCol();
    
    /**
     * add an int array as new column (at the right end).
     * numRows will be increased by one.
     * @param col
     * @throws IllegalArgumentException if col.size != numRows()
     */
    public void addCol(int[] col) throws IllegalArgumentException;

    /**
     * @return number of rows
     */
    public int numRows();
    
    /**
     * @return number of columns
     */
    public int numCols();
    
    /**
     * get element denoted by row and column.
     * @param row
     * @param column
     * @return element at given position
     * @throws NoSuchElementException if row or column invalid
     */
    public int get(int row, int column) throws NoSuchElementException;
    
    /**
     * set element denoted by row and column.
     * @param row
     * @param column
     * @param value
     * @throws NoSuchElementException if row or column invalid
     */
    public void put(int row, int column, int value) throws NoSuchElementException;
    
    /**
     * get whole row.
     * @param row index
     * @return array of values at given row
     * @throws IllegalArgumentException if row is invalid
     */
    public int[] getRow(int row) throws IllegalArgumentException;
    
    /**
     * get whole column.
     * @param column index
     * @return array of values at given column
     * @throws IllegalArgumentException if row is invalid
     */
    public int[] getCol(int column) throws IllegalArgumentException;
    
    /**
     * generate string containing the whole table in python format
     * (list of list in brackets).
     * For example:
     * [
     *   [ 1, 2, 3, 4],
     *   [ 2, 4, 6, 8],
     *   [ -1, 3, 6, 9]
     * ]
     * @return readable table
     */
    public String toString();
}
Ergänzen Sie die Klasse IntTable um folgende Konstruktoren:
    /**
     * default matrix has no elements at all.
     */
    public IntTable() { }

    /**
     * initialize with given 2 dimensional array
     * @param matrix
     */
    public IntTable(int[][] matrix) { }

    /**
     * create new table filled with zeros.
     * @param rows number of rows
     * @param columns number of columns
     */
    public IntTable(int rows, int columns) { }
Warum können Konstruktoren nicht über das Interface vorgegeben werden?

Schreiben Sie eine main() mit der die Funktionsweise der Klasse getestet werden soll. Testen Sie auch die Exceptions, indem Sie diese auslösen (und abfangen). Beachten Sie auch Grenzfälle (leere Tabelle: Zugriff, addRow(), toString() usw.)

Ergänzen Sie Ihr Projekt gegebenenfalls um fehlende Exceptions.

Labels: ,


Kommentare:

Kommentar veröffentlichen

Abonnieren Kommentare zum Post [Atom]





<< Startseite

This page is powered by Blogger. Isn't yours?

Abonnieren Posts [Atom]