Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Moving Objects Databases

Authors: Ralf Hartmut Güting, Markus Schneider

1st Edition

0120887991, 978-0120887996

More Books

Students also viewed these Databases questions

Question

using signal flow graph

Answered: 1 week ago

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago