JAVA PROBLEM Here is the copied and pasted code: Requirements A matrix is a rect
ID: 3730890 • Letter: J
Question
JAVA PROBLEM
Here is the copied and pasted code:
Requirements A matrix is a rectangular array of values arranged in rows and columns. For example: e f g h You are to implement a new collection, a Matrix. Ordinarily, new collections would implement the Collection interface, but for the purpose of this exercise, we will not do this so as to avoid implementing the eleven additional methods as part of the interface's contract However, because all collections can be instantiated with any type, your Matrix class will need to be"generic". Therefore, the tests for this exercise will attempt to instantiate and test your Matrix class using both Integer and String cell values (in separate unit tests) The provided skeleton will help you get started. The first step is to fix the current compiler errors by inserting the correct return types and parameters for each method (and constructor) according to the JavaDoc comments. Eclipse will help you with this process via suggestions. The next step is figuring out how you will internally model the Matrix. Look into Collection types that currently exist and think about how you can use them and their methods as a backbone for the class. Remember, your class will need to make correct use of a generic type, but it is suggested that you initially experiment with a non-generic type (e.g. by just using integers or strings for the cell values), until you achieve a working matrix. After which, replace your hard-coded cell value type with a generic type.Explanation / Answer
package coll.Matrix;
import java.util.*;
public class Matrix<T> implements Iterable<T> {
ArrayList<ArrayList<T>> arr;
int col;
int row;
/**
* Construct a Matrix object.
*
* @param rows. An int that specifies the number of rows.
* @param columns. An int that specifies the number of columns.
*/
public Matrix(int row,int col) {
this.row=row;
this.col=col;
arr=new ArrayList<ArrayList<T>>();
for(int i=0;i<row;i++)
{
arr.add(new ArrayList<T>());
for(int j=0;j<col;j++)
arr.get(i).add(null);
}
}
/**
* Assigns a value to a given cell, specified by its row, column coordinates.
*
* @param row. An int for the row index with 0-based indexing.
* @param column. An int for the column index with 0-based indexing.
* @param value. A generic value to be assigned to the given cell.
*/
public void insert(int row,int col,T val) {
if(row<=this.row && col<=this.col)
arr.get(row).set(col,val);
}
/**
* Gets the value at a given cell, specified by its row, column coordinates.
* @param row. An int for the row index with 0-based indexing.
* @param column. An int for the column index with 0-based indexing.
* @return value. A generic value located at the given cell.
*/
public T get(int row,int col) {
if(row<=this.row && col<=this.col)
return arr.get(row).get(col);
else
return null;
}
/**
* Gets the total number of cells in the matrix.
* @return an int equal to the total number of cells in the matrix.
*/
public int size() {
return row*col;
}
/**
* Converts the matrix to String format.
* @return a String representation of the matrix.
*/
public String toString() {
StringBuilder str=new StringBuilder();
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
str.append(arr.get(i).get(j).toString()+" ");
str.append(" ");
}
return str.toString();
}
/**
* Returns an Iterator object for the matrix. The Iterator should follow the
* order of row by row. Within each row the order is left to right.
* @return an Iterator object for the matrix.
*/
public Iterator<T> iterator() {
ArrayList<T> list=new ArrayList<T>(row*col);
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
list.add(arr.get(i).get(j));
return list.iterator();
}
public static void main(String[] args) {
Matrix<String> m=new Matrix<String>(2,2);
m.insert(0,0,"a");
m.insert(0,1,"b");
m.insert(1,0,"c");
m.insert(1,1,"d");
for(String i:m)
System.out.println(i);
}
}
/*
I have used a two dimensional ArrayList to Store the data and when returning the iterator, I am just implementing, iterable, I have just converted 2-D arraylist into flat 1-D arraylist and returned its iterator.
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.