Question: I have a couple files involved in a small java matrix program. I need to change the background implementation from a simple array to an

I have a couple files involved in a small java matrix program. I need to change the background implementation from a simple array to an arraylist.

The implementation needs to use an ArrayList to contain the values of the matrix, and default them to 0. I assume a nested for loop would be the best method. I have also included the helper method clear.

Helper Method:

/** * 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); } } }

Original Array Method: public class ArrayImplementation extends AbstractMatrix { /** * Creates an array representation of a matrix of integers. * Elements of the array are initialized to zero. * @param numRows the number of rows in the matrix * @param numColumns the number of columns in the matrix */ public ArrayImplementation (int numRows, int numColumns) { super.setNumRows(numRows); super.setNumColumns(numColumns); elements = new int[numRows][numColumns]; super.clear(); } private int[][] elements;

New ArrayList Method: 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 */ public ArrayListImplementation(int numRows, int numColumns) {

super.setNumRows(numRows); super.setNumColumns(numColumns); NEED TO CHANGE THIS IMPLEMENTATION } 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!