I have code here which runs a short series of tests to see if the last element in the array is the expected number. The code that I have to put in goes where [???] is currently.
//The answer must be the shortest possible
class EmptyArrayException extends RuntimeException{}
class ArrayUtil{
public static Object lastElement (Object[]array)[???]{
if(array.length>0)return array[array.length-1];
throw new EmptyArrayException();
}
}
public class Exercise{
public static void test(){
assert ArrayUtil.lastElement(new Integer[]{1,2,3}).equals(3);
assert ArrayUtil.lastElement(new Integer[]{1,2}).equals(2);
assert ArrayUtil.lastElement(new Integer[]{1}).equals(1);
assert ArrayUtil.lastElement(new Integer[]{}).equals(1);
assert false:"Code not reachable";
}
public static void main(String [] arg){
try{ test();}
catch(Throwable t){/*the test suit
logs t on the test results:
a single place for error logging.*/}
}
}
I assume that I would write ' throws EmptyArrayException ', but this is wrong. ' throws EmptyArrayException() ' is wrong too.
So what do I put in the [???] space?