Question: A matrix is a rectangular array of values arranged in rows and columns. For example: A B C D E F G H I J
A matrix is a rectangular array of values arranged in rows and columns. For example:
A B C D E F G H I J K L
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.
toString() should take the following form for Integer objects:
8 14 16 10 20
16 21 4 12 2
21 4 10 19 8
21 16 4 2 9
21 7 7 3 7
3 3 13 13 6
2 16 16 12 15
And the following form for String objects:
J VK k J j bX fv
h bO UJ gR Tr k t
jA Za D dg rt jh AX
o ql P pj A nd hd
Note that the dimension size will be randomized in testing (hence why the above two examples are of different dimensions). The tests and the above two examples use tabs to separate the cell values. You can achieve the same result by inserting a \t character (a tab) at the end of each cell value (except for the last) when constructing the string. Cell values will be no more than 2 characters long (but this shouldn't matter).
iterator() is a challenging method. Look into Iterators and Iterables, and the difference between the two. Hint: your first line could be:
return new Iterator() {
Completion of this method implicitly enables foreach iteration over the matrix. For example:
public static void main(String[] args) { Matrixm = new Matrix (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);
}
}
Will output:
a
b
c
d
Finally, upload and submit your 1 class:
Matrix MATRIX.JAVA CODE BELOW
package coll.Matrix;
import java.util.*;
public class Matrix implements Iterable {
/**
* 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 rows = 4;
int columns = 4;
}
/**
* 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 insert() {
}
/**
* 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 get() {
}
/**
* Gets the total number of cells in the matrix.
* @return an int equal to the total number of cells in the matrix.
*/
public size() {
}
/**
* Converts the matrix to String format.
* @return a String representation of the matrix.
*/
public 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 iterator() {
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
