Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database System

Authors: Elmasri Ramez And Navathe Shamkant

7th Edition

978-9332582705

More Books

Students also viewed these Databases questions