Question: Looking for assistance on this matrix java program, there's a method I'm stuck on. The method in question is fillColumnWise. I'm unsure why it is

Looking for assistance on this matrix java program, there's a method I'm stuck on. The method in question is fillColumnWise. I'm unsure why it is not working. I'll bold it

Matrix Class:

package matrix; public interface Matrix { /** * Creates a new matrix with given dimensions and initializes the elements * to zero. * This is a static method with a default implementation. * * @param rows the number of rows in the matrix * @param columns the number of columns in the matrix * @throws MatrixException if dimensions are not positive * @return the created matrix */ static Matrix create(int rows, int columns) { // return new ArrayImplementation(rows, columns); return new ArrayListImplementation(rows, columns); }

/** * Returns the number of rows in this matrix. * This is an abstract method. * * @return the number of rows in this matrix. */ int getNumRows(); /** * Sets the number of rows in this matrix. * @param numRows the number of rows in this matrix * @throws MatrixException if numRows is not positive */ void setNumRows(int numRows); /** * Sets the number of columns in this matrix. * @param numColumns the number of columns in this matrix * @throws MatrixException if numColumns is not positive */ void setNumColumns(int numColumns);

/** * Returns the number of columns in this matrix. * This is an abstract method. * * @return the number of columns in this matrix. */ int getNumColumns();

/** * Gets the element at the indicated row and column in this matrix. * This is an abstract method. * * @param row the row position for the element. * @param column the column position for the element. * @return the element at the indicated row and column * @throws MatrixException if row or column is out of range */ int get(int row, int column);

/** * Sets the element at the indicated row and column in this matrix. * This is an abstract method. * * @param row the row position for the element. * @param column the column position for the element. * @param element the value to set in the matrix * @throws MatrixException if row or column is out of range */ void set(int row, int column, int element); /** * Checks a row index for this matrix. * This is a default method and also a template method. * * @param row the row index to check * @throws MatrixException if row is out of range */ default void checkRowBounds(int row) { if (row >= getNumRows() || row < 0) { throw new MatrixException(String.format("Index (%s) out of bounds", row)); } } /** * Checks a column index for this matrix. * This is a default method and also a template method. * * @param column the column index to check * @throws MatrixException if row is out of range */ default void checkColumnBounds(int column) { if (column >= getNumColumns() || column < 0) { throw new MatrixException(String.format("Index (%s) out of bounds", column)); } } /** * Checks row and column indices for this matrix. * This is a default method. * * @param row the row index to check * @param column the column index to check */ default void checkBounds(int row, int column) { checkRowBounds(row); checkColumnBounds(column); } /** * Sets all elements of this matrix to zero. * This is a default method; it provides an implementation. * This is also a template method; it calls methods whose implementations * are provided by subclasses. */ default void clear() { for (int r = 0; r < getNumRows(); r++) { for (int c = 0; c < getNumColumns(); c++) { set(r, c, 0); } } } /** * Fills this matrix row-wise, starting with 1 * in top left and incrementing successive elements by 1 * from left to right. */ default void fillRowWise() { // implementation here } /** * Fills this matrix column-wise, starting with 1 * in top left and incrementing successive elements by 1 * from top to bottom. */ default void fillColumnWise() { THIS IS THE ONE int count = 1; for (int i=1; i <= getNumRows(); i++) { for (int j=1; j <= getNumColumns(); j++) { this.set(i, j, count); count++; } } }

}

Abstract Class:

package matrix; public abstract class AbstractMatrix implements Matrix {

/** * Returns the number of rows in this matrix. * * @return the number of rows in this matrix. */ @Override public int getNumRows() { return numRows; }

/** * Returns the number of columns in this matrix. * * @return the number of columns in this matrix. */ @Override public int getNumColumns() { return numColumns; }

/** * Sets the number of rows in this matrix. * @param numRows the number of rows in this matrix * @throws MatrixException if numRows is not positive */ @Override public void setNumRows(int numRows) { if (numRows <= 0) { throw new MatrixException(String.format("numRows (%s) must be positive", numRows)); } this.numRows = numRows; }

/** * Sets the number of columns in this matrix. * @param numColumns the number of columns in this matrix * @throws MatrixException if numColumns is not positive */ @Override public void setNumColumns(int numColumns) { if (numColumns <= 0) { throw new MatrixException(String.format("numColumns (%s) must be positive", numColumns)); } this.numColumns = numColumns; }

/** * Creates a visual representation of this matrix as a string. This method * can be used for debugging. This is a template method; it uses a method * (get) that must be implemented by a subclass. This method overrides a * method in the Object class. * * @return the string representation of this matrix. */ @Override public String toString() { StringBuilder builder = new StringBuilder(); for (int r = 0; r < getNumRows(); r++) { for (int c = 0; c < getNumColumns(); c++) { builder.append(String.format("%6s", get(r, c))); } builder.append(" "); } return builder.toString(); } /** * Private instance fields follow */

private int numRows; private int numColumns;

}

Implementation:

package matrix;

import java.util.List; import java.util.ArrayList;

public class ArrayListImplementation extends AbstractMatrix {

/** * Creates a list representation of a matrix of integers. * Elements of the list are initialized to zero. * @param numRows the number of rows in the matrix * @param numColumns the number of columns in the matrix * @throws MatrixException if dimensions are not positive */ public ArrayListImplementation(int numRows, int numColumns) { // You must provide super.setNumRows(numRows); super.setNumColumns(numColumns); elements = new ArrayList<>(numRows); for (int i=0; i < numRows; i++) { ArrayList list = new ArrayList(numColumns); for (int j=0; j < numColumns; j++) { list.add(0); } elements.add(list); } super.clear(); } /** * Gets the element at the indicated row and column in this matrix. * * @param row the row position for the element. * @param column the column position for the element. * @return the element at the indicated row and column * @throws MatrixException if row or column is out of range */ @Override public int get(int row, int column) { checkBounds(row, column); return elements.get(row).get(column); }

/** * Sets the element at the indicated row and column in this matrix. * * @param row the row position for the element. * @param column the column position for the element. * @param element the value to set in the matrix * @throws MatrixException if row or column is out of range */ @Override public void set(int row, int column, int element) { checkBounds(row, column); // elements.get(row).remove(column); elements.get(row).add(column, element); } // Private instance fields go here private List> elements; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!