Question: 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

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:

package edu.buffalo.cse116; import java.util.ArrayList; /** * Instances of this class are built around an ArrayList, but add a method which returns the largest value currently in * that List. This is not the most useful of classes, but its simplicity is perfect for learning JUnit testing. * * @author Matthew Hertz */ public class ListMax { /** ArrayList instance whose largest value will be calculated. */ private ArrayList 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 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() { double retVal; // The code is below is intentionally buggy -- students will need to fix this. if (theList.size() == 1) { retVal = theList.get(0); } else { retVal = theList.get(1); } for (int i = 1; i < theList.size(); i++ ) { Double currentElem = theList.get(i); if ((currentElem != null) && (retVal < currentElem)) { retVal = currentElem; } } return retVal; } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!