Junit test provided but won\'t take you more than 15 mins. ---------------------
ID: 3891071 • Letter: J
Question
Junit test provided but won't take you more than 15 mins.
------------------------
Complete the hasNext() and next() methods so that the class iterates through each entry in a 2d array.Since accessing an entry in arr requires specifying a row AND column, this class will have TWO instance variables for the cursor: rowCursor holds the row and colCursor tracks the column. Your Iterator will start at row 0 and column 0. It should then go column-by-column through each entry in row 0. Once you reach the end of a row, increase rowCursor by 1 and reset colCursor to 0. The Iterator will not have any more elements to return when rowCursor is not a valid row in the array.
------------------
package edu.buffalo.cse116;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* A simple iterator class for 2-dimensional arrays. The entries of the array are returned by this iterator by starting
* at the entry at (row 0, column 0), through each of the entries in row 0, to the entry at (row 1, column 0), through
* the entries in (row 1, column 1), and so on.
*
* @author Matthew Hertz
* @param <E> Type of elements in the 2-d array over which we are iterating.
*/
public class RowAndColIterator<E> implements Iterator<E> {
/** The iterator's row position as it works through the array. */
private int rowCursor;
/** The iterator's column position as it works through the array. */
private int colCursor;
/** The array over which we are iterating. */
private E[][] arr;
/**
* Creates an iterator over the given 2-d array.
*
* @param array 2-d array over which we wish to iterate.
*/
public RowAndColIterator(E[][] array) {
arr = array;
rowCursor = 0;
colCursor = 0;
}
/** Returns whether the iterator has a next object. */
public boolean hasNext() {
}
/** Returns the next object in the iterator. */
public E next() {
if(hasNext()) {
}
}
public void remove() {
throw new UnsupportedOperationException();
}
}
-----------------------
testClass
package edu.buffalo.cse116;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.lang.reflect.Field;
import java.util.NoSuchElementException;
import org.junit.Before;
import org.junit.Test;
/**
* Class which includes the needed JUnit tests for our implementations of an 2-d array Iterator.
*/
public class RowAndColArrayIteratorTest {
@Test
public void checkHasNextStart() throws Exception {
Double[][] arr = new Double[1][1];
RowAndColIterator<Double> testee = new RowAndColIterator<Double>(arr);
assertTrue("hasNext() should return true after it was just created and given an array with 1 row and 1 column",
testee.hasNext());
int rowCursor = getRowCursor(testee);
assertEquals("hasNext() should not change the value of rowCursor", 0, rowCursor);
int colCursor = getColCursor(testee);
assertEquals("hasNext() should not change the value of colCursor", 0, colCursor);
Double[][] sndArr = new Double[10][10];
RowAndColIterator<Double> sndTestee = new RowAndColIterator<Double>(sndArr);
assertTrue("hasNext() should return true after it was just created and given an array with 10 rows and 10 columns",
sndTestee.hasNext());
rowCursor = getRowCursor(sndTestee);
assertEquals("hasNext() should not change the value of rowCursor", 0, rowCursor);
colCursor = getColCursor(sndTestee);
assertEquals("hasNext() should not change the value of colCursor", 0, colCursor);
}
@Test
public void checkHasNextMiddle() throws IllegalArgumentException, IllegalAccessException {
Integer[][] arr = { { 0xDEADBEEF, 0xFEEDFACE }, { 7, 0xBADACE } };
RowAndColIterator<Integer> testee = new RowAndColIterator<Integer>(arr);
setRowCursor(testee, 0);
setColCursor(testee, 1);
assertTrue("hasNext() should return true when rowCursor = 0 and colCursor = 1 and arr is a 2x2 array.",
testee.hasNext());
int rowCursor = getRowCursor(testee);
assertEquals("hasNext() should not change the value of rowCursor", 0, rowCursor);
int colCursor = getColCursor(testee);
assertEquals("hasNext() should not change the value of colCursor", 1, colCursor);
Integer[][] arrTwo = { { 0xDEADBEEF, 0xFEEDFACE, 99 }, { 1, 7, 0xBADACE }, { 0, -3, 2 } };
RowAndColIterator<Integer> testeeTwo = new RowAndColIterator<Integer>(arrTwo);
setRowCursor(testeeTwo, 2);
setColCursor(testeeTwo, 0);
assertTrue("hasNext() should return true when rowCursor = 2 and colCursor = 0 and arr is a 3x3 array.",
testeeTwo.hasNext());
rowCursor = getRowCursor(testeeTwo);
assertEquals("hasNext() should not change the value of rowCursor", 2, rowCursor);
colCursor = getColCursor(testeeTwo);
assertEquals("hasNext() should not change the value of colCursor", 0, colCursor);
setRowCursor(testeeTwo, 1);
setColCursor(testeeTwo, 1);
assertTrue("hasNext() should return true when rowCursor = 1 and colCursor = 1 and arr is a 3x3 array.",
testeeTwo.hasNext());
rowCursor = getRowCursor(testeeTwo);
assertEquals("hasNext() should not change the value of rowCursor", 1, rowCursor);
colCursor = getColCursor(testeeTwo);
assertEquals("hasNext() should not change the value of colCursor", 1, colCursor);
Integer[][] arrThree = { { 0xDEADBEEF, 0xFEEDFACE, 99 } };
RowAndColIterator<Integer> testeeThree = new RowAndColIterator<Integer>(arrThree);
setRowCursor(testeeThree, 0);
setColCursor(testeeThree, 0);
assertTrue("hasNext() should return true when rowCursor = 0 and colCursor = 0 and arr is a 1x3 array.",
testeeThree.hasNext());
rowCursor = getRowCursor(testeeThree);
assertEquals("hasNext() should not change the value of rowCursor", 0, rowCursor);
colCursor = getColCursor(testeeThree);
assertEquals("hasNext() should not change the value of colCursor", 0, colCursor);
setRowCursor(testeeThree, 0);
setColCursor(testeeThree, 1);
assertTrue("hasNext() should return true when rowCursor = 0 and colCursor = 1 and arr is a 1x3 array.",
testeeThree.hasNext());
rowCursor = getRowCursor(testeeThree);
assertEquals("hasNext() should not change the value of rowCursor", 0, rowCursor);
colCursor = getColCursor(testeeThree);
assertEquals("hasNext() should not change the value of colCursor", 1, colCursor);
}
@Test
public void checkHasNextLast() throws IllegalArgumentException, IllegalAccessException {
Integer[][] arr = { { 0xDEADBEEF, 0xFEEDFACE }, { 7, 0xBADACE } };
RowAndColIterator<Integer> testee = new RowAndColIterator<Integer>(arr);
setRowCursor(testee, 1);
setColCursor(testee, 1);
assertTrue("hasNext() should return true when rowCursor = 1 and colCursor = 1 and arr is a 2x2 array.",
testee.hasNext());
int rowCursor = getRowCursor(testee);
assertEquals("hasNext() should not change the value of rowCursor", 1, rowCursor);
int colCursor = getColCursor(testee);
assertEquals("hasNext() should not change the value of colCursor", 1, colCursor);
Integer[][] arrTwo = { { 0xDEADBEEF, 0xFEEDFACE, 99 }, { 1, 7, 0xBADACE }, { 0, -3, 2 } };
RowAndColIterator<Integer> testeeTwo = new RowAndColIterator<Integer>(arrTwo);
setRowCursor(testeeTwo, 2);
setColCursor(testeeTwo, 2);
assertTrue("hasNext() should return true when rowCursor = 2 and colCursor = 2 and arr is a 3x3 array.",
testeeTwo.hasNext());
rowCursor = getRowCursor(testeeTwo);
assertEquals("hasNext() should not change the value of rowCursor", 2, rowCursor);
colCursor = getColCursor(testeeTwo);
assertEquals("hasNext() should not change the value of colCursor", 2, colCursor);
Integer[][] arrThree = { { 0xDEADBEEF, 0xFEEDFACE, 99 } };
RowAndColIterator<Integer> testeeThree = new RowAndColIterator<Integer>(arrThree);
setRowCursor(testeeThree, 0);
setColCursor(testeeThree, 2);
assertTrue("hasNext() should return true when rowCursor = 0 and colCursor = 2 and arr is a 1x3 array.",
testeeThree.hasNext());
rowCursor = getRowCursor(testeeThree);
assertEquals("hasNext() should not change the value of rowCursor", 0, rowCursor);
colCursor = getColCursor(testeeThree);
assertEquals("hasNext() should not change the value of colCursor", 2, colCursor);
}
@Test
public void checkHasNextAfterLast() throws IllegalArgumentException, IllegalAccessException {
Integer[][] arr = { { 0xDEADBEEF, 0xFEEDFACE }, { 7, 0xBADACE } };
RowAndColIterator<Integer> testee = new RowAndColIterator<Integer>(arr);
setRowCursor(testee, 1);
setColCursor(testee, 2);
boolean result = testee.hasNext();
int rowCursor = getRowCursor(testee);
assertEquals("hasNext() should not change the value of rowCursor", 1, rowCursor);
int colCursor = getColCursor(testee);
assertEquals("hasNext() should not change the value of colCursor", 2, colCursor);
setRowCursor(testee, 2);
setColCursor(testee, 0);
result = result && testee.hasNext();
assertFalse(
"hasNext() should return false after reading with rowCursor = 1 and colCursor = 1 and arr is a 2x2 array.",
result);
rowCursor = getRowCursor(testee);
assertEquals("hasNext() should not change the value of rowCursor", 2, rowCursor);
colCursor = getColCursor(testee);
assertEquals("hasNext() should not change the value of colCursor", 0, colCursor);
Integer[][] arrTwo = { { 0xDEADBEEF, 0xFEEDFACE, 99 }, { 1, 7, 0xBADACE }, { 0, -3, 2 } };
RowAndColIterator<Integer> testeeTwo = new RowAndColIterator<Integer>(arrTwo);
setRowCursor(testeeTwo, 2);
setColCursor(testeeTwo, 3);
result = testeeTwo.hasNext();
rowCursor = getRowCursor(testeeTwo);
assertEquals("hasNext() should not change the value of rowCursor", 2, rowCursor);
colCursor = getColCursor(testeeTwo);
assertEquals("hasNext() should not change the value of colCursor", 3, colCursor);
setRowCursor(testeeTwo, 3);
setColCursor(testeeTwo, 0);
result = result && testeeTwo.hasNext();
assertFalse(
"hasNext() should return false after reading with rowCursor = 2 and colCursor = 2 and arr is a 3x3 array.",
result);
rowCursor = getRowCursor(testeeTwo);
assertEquals("hasNext() should not change the value of rowCursor", 3, rowCursor);
colCursor = getColCursor(testeeTwo);
assertEquals("hasNext() should not change the value of colCursor", 0, colCursor);
Integer[][] arrThree = { { 0xDEADBEEF, 0xFEEDFACE, 99 } };
RowAndColIterator<Integer> testeeThree = new RowAndColIterator<Integer>(arrThree);
setRowCursor(testeeThree, 0);
setColCursor(testeeThree, 3);
result = testeeThree.hasNext();
rowCursor = getRowCursor(testeeThree);
assertEquals("hasNext() should not change the value of rowCursor", 0, rowCursor);
colCursor = getColCursor(testeeThree);
assertEquals("hasNext() should not change the value of colCursor", 3, colCursor);
setRowCursor(testeeThree, 1);
setColCursor(testeeThree, 0);
result = result && testeeThree.hasNext();
assertFalse(
"hasNext() should return false after reading with rowCursor = 0 and colCursor = 2 and arr is a 1x3 array.",
result);
rowCursor = getRowCursor(testeeThree);
assertEquals("hasNext() should not change the value of rowCursor", 1, rowCursor);
colCursor = getColCursor(testeeThree);
assertEquals("hasNext() should not change the value of colCursor", 0, colCursor);
}
@Test
public void checkNextStart() throws IllegalAccessException {
Double[][] arr = { { 3.4 } };
RowAndColIterator<Double> testee = new RowAndColIterator<Double>(arr);
assertEquals(
"next() should return the value of the entry at row 0 and column 0 when rowCursor = 0 and colCursor = 0 and given a 1x1 array.",
3.4, testee.next(), 0.0);
int rowCursor = getRowCursor(testee);
int colCursor = getColCursor(testee);
assertEquals(
"next() should advance colCursor, but then set it to zero when it was equal to the number columns in the array.",
0, colCursor);
assertEquals("next() should advance rowCursor when it reset colCursor to zero", 1, rowCursor);
Double[][] sndArr = new Double[10][10];
sndArr[0][0] = 7.7;
RowAndColIterator<Double> sndTestee = new RowAndColIterator<Double>(sndArr);
assertEquals(
"next() should return the value of the entry at row 0 and column 0 when rowCursor = 0 and colCursor = 0 and given a 10x10 array.",
7.7, sndTestee.next(), 0.0);
rowCursor = getRowCursor(sndTestee);
colCursor = getColCursor(sndTestee);
assertEquals("next() should advance colCursor by 1 that is is smaller than the number of columns in the array.", 1,
colCursor);
assertEquals("next() should not change rowCursor when the colCursor did not advance past the end of the row", 0,
rowCursor);
}
@Test
public void checkNextMiddle() throws IllegalArgumentException, IllegalAccessException {
Integer[][] arr = { { 0xDEADBEEF, 0xFEEDFACE }, { 7, 0xBADACE } };
RowAndColIterator<Integer> testee = new RowAndColIterator<Integer>(arr);
setRowCursor(testee, 0);
setColCursor(testee, 1);
assertEquals(
"next() should return the value of the entry at row 0 and column 1 when rowCursor = 0 and colCursor = 1 and given a 2x2 array.",
0xFEEDFACE, (int) testee.next());
int rowCursor = getRowCursor(testee);
int colCursor = getColCursor(testee);
assertEquals(
"next() should advance colCursor, but then set it to zero when it was equal to the number columns in the array.",
0, colCursor);
assertEquals("next() should advance rowCursor when it reset colCursor to zero", 1, rowCursor);
Integer[][] arrTwo = { { 0xDEADBEEF, 0xFEEDFACE, 99 }, { 1, 7, 0xBADACE }, { 0, -3, 2 } };
RowAndColIterator<Integer> testeeTwo = new RowAndColIterator<Integer>(arrTwo);
setRowCursor(testeeTwo, 0);
setColCursor(testeeTwo, 2);
assertEquals(
"next() should return the value of the entry at row 0 and column 2 when rowCursor = 0 and colCursor = 2 and given a 3x3 array.",
99, (int) testeeTwo.next());
rowCursor = getRowCursor(testeeTwo);
colCursor = getColCursor(testeeTwo);
assertEquals(
"next() should advance colCursor, but then set it to zero when it was equal to the number columns in the array.",
0, colCursor);
assertEquals("next() should advance rowCursor when it reset colCursor to zero", 1, rowCursor);
setRowCursor(testeeTwo, 1);
setColCursor(testeeTwo, 1);
assertEquals(
"next() should return the value of the entry at row 1 and column 1 when rowCursor = 1 and colCursor = 1 and given a 3x3 array.",
7, (int) testeeTwo.next());
rowCursor = getRowCursor(testeeTwo);
colCursor = getColCursor(testeeTwo);
assertEquals(
"next() should advance colCursor by 1 when that is is smaller than the number of columns in the array.", 2,
colCursor);
assertEquals("next() should not change rowCursor when the colCursor did not advance past the end of the row", 1,
rowCursor);
Integer[][] arrThree = { { 0xDEADBEEF, 0xFEEDFACE, 99 } };
RowAndColIterator<Integer> testeeThree = new RowAndColIterator<Integer>(arrThree);
setRowCursor(testeeThree, 0);
setColCursor(testeeThree, 1);
assertEquals(
"next() should return the value of the entry at row 0 and column 1 when rowCursor = 0 and colCursor = 1 and given a 1x3 array.",
0xFEEDFACE, (int) testeeThree.next());
rowCursor = getRowCursor(testeeThree);
colCursor = getColCursor(testeeThree);
assertEquals(
"next() should advance colCursor by 1 when that is is smaller than the number of columns in the array.", 2,
colCursor);
assertEquals("next() should not change rowCursor when the colCursor did not advance past the end of the row", 0,
rowCursor);
}
@Test
public void checkNextLast() throws IllegalArgumentException, IllegalAccessException {
Integer[][] arr = { { 0xDEADBEEF, 0xFEEDFACE }, { 7, 0xBADACE } };
RowAndColIterator<Integer> testee = new RowAndColIterator<Integer>(arr);
setRowCursor(testee, 1);
setColCursor(testee, 1);
assertEquals(
"next() should return the value of the entry at row 1 and column 1 when rowCursor = 1 and colCursor = 1 and given a 2x2 array.",
0xBADACE, (int) testee.next());
int rowCursor = getRowCursor(testee);
int colCursor = getColCursor(testee);
assertEquals(
"next() should advance colCursor, but then set it to zero when it was equal to the number columns in the array.",
0, colCursor);
assertEquals("next() should advance rowCursor when it reset colCursor to zero", 2, rowCursor);
Integer[][] arrTwo = { { 0xDEADBEEF, 0xFEEDFACE, 99 }, { 1, 7, 0xBADACE }, { 0, -3, 2 } };
RowAndColIterator<Integer> testeeTwo = new RowAndColIterator<Integer>(arrTwo);
setRowCursor(testeeTwo, 2);
setColCursor(testeeTwo, 2);
assertEquals(
"next() should return the value of the entry at row 2 and column 2 when rowCursor = 2 and colCursor = 2 and given a 3x3 array.",
2, (int) testeeTwo.next());
rowCursor = getRowCursor(testeeTwo);
colCursor = getColCursor(testeeTwo);
assertEquals(
"next() should advance colCursor, but then set it to zero when it was equal to the number columns in the array.",
0, colCursor);
assertEquals("next() should advance rowCursor when it reset colCursor to zero", 3, rowCursor);
Integer[][] arrThree = { { 0xDEADBEEF, 0xFEEDFACE, 199 } };
RowAndColIterator<Integer> testeeThree = new RowAndColIterator<Integer>(arrThree);
setRowCursor(testeeThree, 0);
setColCursor(testeeThree, 2);
assertEquals(
"next() should return the value of the entry at row 0 and column 2 when rowCursor = 0 and colCursor = 2 and given a 1x3 array.",
199, (int) testeeThree.next());
rowCursor = getRowCursor(testeeThree);
colCursor = getColCursor(testeeThree);
assertEquals(
"next() should advance colCursor, but then set it to zero when it was equal to the number columns in the array.",
0, colCursor);
assertEquals("next() should advance rowCursor when it reset colCursor to zero", 1, rowCursor);
}
@Test
public void checkNextAfterLast() throws IllegalArgumentException, IllegalAccessException {
Integer[][] arr = { { 0xDEADBEEF, 0xFEEDFACE }, { 7, 0xBADACE } };
RowAndColIterator<Integer> testee = new RowAndColIterator<Integer>(arr);
try {
try {
setRowCursor(testee, 1);
setColCursor(testee, 2);
testee.next();
} catch (IndexOutOfBoundsException iobe) {
// Ignore this if it exists -- it could be that something else works
}
setRowCursor(testee, 2);
setColCursor(testee, 0);
testee.next();
fail("next() should throw a NoSuchElementException when reading past the end of a 2x2 array.");
} catch (NoSuchElementException e) {
} catch (IndexOutOfBoundsException iobe) {
fail("next() should throw a NoSuchElementException when reading past the end of a 2x2 array.");
}
Integer[][] arrTwo = { { 0xDEADBEEF, 0xFEEDFACE, 99 }, { 1, 7, 0xBADACE }, { 0, -3, 2 } };
RowAndColIterator<Integer> testeeTwo = new RowAndColIterator<Integer>(arrTwo);
try {
try {
setRowCursor(testeeTwo, 2);
setColCursor(testeeTwo, 3);
testeeTwo.next();
} catch (IndexOutOfBoundsException iobe) {
// Ignore this if it exists -- it could be that something else works
}
setRowCursor(testeeTwo, 3);
setColCursor(testeeTwo, 0);
testeeTwo.next();
fail("next() should throw a NoSuchElementException when reading past the end of a 3x3 array.");
} catch (NoSuchElementException e) {
} catch (IndexOutOfBoundsException iobe) {
fail("next() should throw a NoSuchElementException when reading past the end of a 3x3 array.");
}
Integer[][] arrThree = { { 0xDEADBEEF, 0xFEEDFACE, 99 } };
RowAndColIterator<Integer> testeeThree = new RowAndColIterator<Integer>(arrThree);
try {
try {
setRowCursor(testeeThree, 0);
setColCursor(testeeThree, 3);
testeeThree.next();
} catch (IndexOutOfBoundsException iobe) {
// Ignore this if it exists -- it could be that something else works
}
setRowCursor(testeeThree, 1);
setColCursor(testeeThree, 0);
testeeThree.next();
fail("next() should throw a NoSuchElementException when reading past the end of a 3x3 array.");
} catch (NoSuchElementException e) {
} catch (IndexOutOfBoundsException iobe) {
fail("next() should throw a NoSuchElementException when reading past the end of a 3x3 array.");
}
}
private Field rowCursorField;
private Field colCursorField;
@Before
public final void checkFieldsUnchanged() {
Class<?> mArrIt = RowAndColIterator.class;
Field[] fields = mArrIt.getDeclaredFields();
assertEquals(
"You should not add any fields to the TwoDAIterator class. It should only have the rowCursor, colCursor, and arr fields.",
3, fields.length);
try {
rowCursorField = mArrIt.getDeclaredField("rowCursor");
rowCursorField.setAccessible(true);
assertEquals("The rowCursor field should be of type int", Integer.TYPE, rowCursorField.getType());
} catch (Exception e) {
fail("Your TwoDAIterator class should still define a field named "rowCursor"");
}
try {
colCursorField = mArrIt.getDeclaredField("colCursor");
colCursorField.setAccessible(true);
assertEquals("The colCursor field should be of type int", Integer.TYPE, rowCursorField.getType());
} catch (Exception e) {
fail("Your TwoDAIterator class should still define a field named "colCursor"");
}
try {
Field arr = mArrIt.getDeclaredField("arr");
assertTrue("The arr field should be an 2-d array", arr.getType().isArray());
assertTrue("The arr field should be an 2-d array", arr.getType().getComponentType().isArray());
assertEquals("The arr field should be an 2-d array", Object.class,
arr.getType().getComponentType().getComponentType());
} catch (Exception e) {
fail("Your PartialArrayMultiSet class should still define a field named "_store"");
}
}
private int getRowCursor(RowAndColIterator<?> testee) throws IllegalAccessException {
return rowCursorField.getInt(testee);
}
private int getColCursor(RowAndColIterator<?> testee) throws IllegalAccessException {
return colCursorField.getInt(testee);
}
private void setRowCursor(RowAndColIterator<?> testee, int newRow) throws IllegalAccessException {
rowCursorField.setInt(testee, newRow);
}
private void setColCursor(RowAndColIterator<?> testee, int newRow) throws IllegalAccessException {
colCursorField.setInt(testee, newRow);
}
}
Explanation / Answer
I have updated the two methods and please find below your code changes. This is your existing code.
import java.util.Iterator;
public class RowAndColIterator<E> implements Iterator<E> {
/** The iterator's row position as it works through the array. */
private int rowCursor;
/** The iterator's column position as it works through the array. */
private int colCursor;
/** The array over which we are iterating. */
private E[][] arr;
/**
* Creates an iterator over the given 2-d array.
*
* @param array
* 2-d array over which we wish to iterate.
*/
public RowAndColIterator(E[][] array) {
arr = array;
rowCursor = 0;
colCursor = 0;
}
/** Returns whether the iterator has a next object. */
public boolean hasNext() {
for (int i = 0; i<arr.length; i++){
for (int j = 0; j<arr[i].length; j++){
return rowCursor < arr.length ;
}
}
return false;
}
/** Returns the next object in the iterator. */
public E next() {
if (hasNext()) {
return arr[rowCursor][colCursor];
}
return null;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.