Question: Writing test case for this code. How would I write test cases for prepareGraph method? /** * If categoryMap is of size zero, throws an

Writing test case for this code.

How would I write test cases for prepareGraph method?

/** * If categoryMap is of size zero, throws an IllegalArgumentException whose message explains what is wrong. * * Else if any of the values in the category map is an empty set, throws an IllegalArgumentException whose message * explains what is wrong. * * Else if any of the numbers in the categoryMap is not positive, throws an IllegalAgumentException whose message * explains what is wrong. * * Else if operation is anything other than SUM, AVG, MAX, or MIN, throws an IllegalArgumentException whose message * explains what is wrong. * * Else, returns a TreeMap (here called summaryMap) such that: * * (1) The sets of keys contained by categoryMap and summaryMap are the same * * (2) For all keys, summaryMap.get(key) is the result of combining the numbers contained in the set * categoryMap.get(key) using the specified operation. If the operation is MAX, "combining" means finding the * largest of the numbers. If the operation is MIN, "combining" means finding the smallest of the numbers. If the * operation is SUM, combining means summing the numbers. If the operation is AVG, combining means averaging the * numbers. * * For example, suppose the categoryMap maps like this: * *

 * Arizona {21 * California {14, 7, 6, 1} * Nevada {3, 11} * Utah {10, 2, 2} * 
* * and the operation is SUM. The map that is returned must map like this: * *
 * Arizona 21 * California 28 * Nevada 14 * Utah 14 * 
*/ public static TreeMap prepareGraph (TreeMap> categoryMap, int operation) { // This implementation ignores its parameters and returns the map described in the Javadoc. //check for exceptions if (categoryMap.size() < 0) { throw new IllegalArgumentException("size of categorymap must be greater than 0"); } else if(operation < 0 || operation > 3) { throw new IllegalArgumentException("operation has to be between 0 and 3"); } else { if (categoryMap.keySet().size() <= 0) { throw new IllegalArgumentException("no key exists"); } } TreeMap summaryMap = new TreeMap<>(); if (operation == MAX) { for (String key : categoryMap.keySet()) { double maxKey = categoryMap.get(key).get(0); for (double aVal : categoryMap.get(key)) { if (aVal > maxKey) { maxKey = aVal; } summaryMap.put(key, maxKey); } } }

if (operation == MIN) { for (String key : categoryMap.keySet()) { double minval = categoryMap.get(key).get(0); for (double val : categoryMap.get(key)) { if (val < minval) { minval = val; } } summaryMap.put(key, minval); } } if (operation == SUM) { for (String key : categoryMap.keySet()) { double keySum = 0.0; for (double value : categoryMap.get(key)) { keySum = value + keySum; } summaryMap.put(key, keySum); } } if (operation == AVG) { for (String key : categoryMap.keySet()) { double keySum = 0.0; for (double val : categoryMap.get(key)) { keySum = val + keySum; } double avgVal = keySum / categoryMap.get(key).size(); summaryMap.put(key, avgVal); } } return summaryMap; }

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!