Reputation: 1
I am attempting to create a Junit tester to check if my median method returns a double if it is given a double. There seems to be some sort of casting error but I am missing it all together. Any help would be greatly appreciated. Thank you so much for any help. EDIT: I am required to use a collection of type Number to add a sequences mixed integers and doubles and ensure that when cast back, the integers are returned as integers and doubles are returned as doubles
The question I am attempting to complete is as follows: TestNumberParsimony: test that when mixed Integer and Double concrete types are added to a StatCollection, they maintain their concrete types as Integer or Double. The mode, min, or max should return concrete type as Integer or Double, according what types the numbers were when they were added to the StatCollection. (Hint: you can test the number's type by attempting a cast of the number retrieved from the statcollection.)
Here is my median method. Essentially it is given a generic type which it creates a collection of type array list out of
public class StatCollection<E extends Number> extends java.util.AbstractCollection<E> implements StatsKeeper<E>{
Collection c;
public StatCollection(){
c=new ArrayList<E>();
}
public Number median(){
if(c.size()<1){//throws an exception if attempts method at size less than 1
throw new ArithmeticException();//throws arithmetic acception
}
Number retn;//number to be returned
ArrayList<E> n=new ArrayList<E>();//create new arraylist
n.addAll(c);//copy all elements in the collection to arraylist
Number[] numlist= new Number[n.size()];//create new array
for (int i=0; i<n.size();i++){
numlist[i]=n.get(i);//copy all elements from arraylist to array
}
Arrays.sort(numlist);//use arrays sorting function
return (E) numlist[n.size()/2];//return median
}
Here is my junit tester for median, which checks whether it correctly returns a double
private StatCollection c=new StatCollection<Number>(new ArrayList<Number>())
/*
* testMedianDouble
* tests the median method with a double mode value
*/
@Test
public void testMedianDouble(){
Number[] input={5.0,3.0,2,5.0,10.0,12.0, 5.0, 5.0};//create a new list of inputs to test whether the median is returned as an integer value
Collections.addAll(c, input);//add all new inputs to collection
try{
Double n=(Double)c.median();//attempt to cast the should be integer mode value to an integer
}
catch(ClassCastException ex){
fail();//if the cast exception happens then fail the test, the number is not of the correct type
}
}
Upvotes: 0
Views: 96
Reputation: 94479
Collections.addAll()
accepts two arguments. The first is of type Collect<? super T>
and the second is of type T
. Since Double
is not a super type of Number
you cannot add a Number[]
to a StatCollection<Double>
.
@Test
public void testMedianDouble(){
Double[] input={5.0,3.0,2,5.0,10.0,12.0, 5.0, 5.0};//create a new list of inputs to test whether the median is returned as an integer value
Collections.addAll(c, input);//add all new inputs to collection
try{
Double n=(Double)c.median();//attempt to cast the should be integer mode value to an integer
}
catch(ClassCastException ex){
fail();//if the cast exception happens then fail the test, the number is not of the correct type
}
}
Upvotes: 0
Reputation: 10914
When you load the double
and int
values into the Number[] input
array, they lose identity as doubles or ints--they are autoboxed and stored as instances of the abstract Number
class.
Thus, there is no way to guarantee that any given instance of a Number
is in fact a Double
holding a reference to a double
, and in fact, your array is not holding references to doubles.
Unless there is a really good reason to use the Number
class, I would just store the values as the raw type double
if doubles are what you really need.
Upvotes: 0