On the code below you should find the largest number stored
On the code below you should find the largest number stored in an ArrayList. This seems like it should be easy, but it currently contains a bug. Write a set of JUnit tests that provide 100% coverage of the ListMax class. If you do this, one (or more) of your tests should fail because of the error in my code. Now that you can see the error, go back and update ListMax to fix the error.
Code:
Solution
Hi,
If you look at the code you have posted there is a flaw in getLargest method that it will not be able to pick the maximum number when its present at start of ArrayList.
I have written JUnit test cases which if you run with existing code it will give you failures.
The JUnit file is as follows:
package listMax;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import org.junit.Test;
public class ListmaxTest {
@Test
public void testMaxElementInListOfSizeOne() {
final ArrayList<Double> list = new ArrayList<Double>();
list.add(18.0);
ListMax listMax = new ListMax(list);
assertEquals(Double.valueOf(\"18.0\").doubleValue(), listMax.getLargest(), 0.001);
}
@Test
public void testMaxELementPresentAtEndInList() {
final ArrayList<Double> list = new ArrayList<Double>();
list.add(18.0);
list.add(9.0);
list.add(28.0);
list.add(48.0);
ListMax listMax = new ListMax(list);
assertEquals(Double.valueOf(\"48.0\").doubleValue(), listMax.getLargest(), 0.001);
}
@Test
public void testMaxELementPresentAtStartInList() {
final ArrayList<Double> list = new ArrayList<Double>();
list.add(78.0);
list.add(9.0);
list.add(28.0);
list.add(48.0);
ListMax listMax = new ListMax(list);
assertEquals(Double.valueOf(\"78.0\").doubleValue(), listMax.getLargest(), 0.001);
}
@Test
public void testMaxELementPresentAtMidInList() {
final ArrayList<Double> list = new ArrayList<Double>();
list.add(18.0);
list.add(9.0);
list.add(48.0);
list.add(28.0);
list.add(27.0);
ListMax listMax = new ListMax(list);
assertEquals(Double.valueOf(\"48.0\").doubleValue(), listMax.getLargest(), 0.001);
}
}
Note: For running this JUnit class from your eclipse you need to add Junit jar file to your build path inside the IDE you are using.
The getLargest method needs to be changed in order to be able to adjust to this failing test case.
The modified method goes as follows which iterates from i=0 so that start element is also covered.
package listMax;
import java.util.ArrayList;
public class ListMax {
/** ArrayList instance whose largest value will be calculated. */
private ArrayList<Double> theList;
/**
* Create a new instance of this class and have it built around the specified list.
*
* @param list List whose maximum element this instance will calculate.
*/
public ListMax(ArrayList<Double> list) {
theList = list;
}
/**
* Find and return the largest element in the list passed to the constructor.
*
* @return Largest element currently stored in the list.
*/
public double getLargest() {
//initialize the retVal to Double.Min value so that negative numbers are also taken into account.
double retVal = Double.MIN_VALUE;
//start iterating from i=0 so that first element is taken into account.
for (int i = 0; i < theList.size(); i++ ) {
Double currentElem = theList.get(i);
if ((currentElem != null) && (retVal < currentElem)) {
retVal = currentElem;
}
}
return retVal;
}
}
So now after using this ListMax class if you run all the test cases you will see that all test cases pass successfully.

