Question: Have a program here that is for displaying a matrix, it is implemented using a list of lists. I believe there is a problem in

Have a program here that is for displaying a matrix, it is implemented using a list of lists. I believe there is a problem in my implementation of the set and get methods. Any help would be greatly appreciated :)

package matrix;

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

/** * This class provides a data representation for the AbstractMatrix * implementation of the Matrix API. * It uses a list of list of integers to store matrix elements. */ 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++) { elements.add(new ArrayList(numColumns)); // creates our list of lists to form a 2d matrix } super.clear(); // simple helper method that initializes all the values of the lists to 0 } /** * 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); // simple helper for throwing the exception, this part is correct return elements.get(row).get(column); this is a part I am confused on, probably wrong }

/** * 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); // simple helper for throwing the exception, this part is correct elements.get(row).set(column, element); this is the part I am confused on } // 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!