In this assignment, you\'ll write your own custom Java exception class and a cla
ID: 3563576 • Letter: I
Question
In this assignment, you'll write your own custom Java exception class and a class that contains some utility methods for working with arrays of integer values.
Write a custom Java exception class named BadArrayException that has two constructors:
Write a Java class named ArrayUtils that contains the following public methods:
public static int minValue(int[] list) - this static method searches its parameter array to locate the minimum value, and returns it. This method must implement the following behavior:
Note that this method does not catch the BadArrayException it throws. When it throws a BadArrayException (due to a null or empty array parameter), the exception is thrown back to the calling code to handle. This is the most common technique for throwing/catching exceptions - a called method throws an exception back to the client code calling it to indicate an exception condition, and the client code must handle the exception condition appropriately (in it's catch block). This implies that calls to this method must be enclosed in a try/catch block that includes a catch(BadArrayException) block. The following methods have the same exception handling behavior.
public static int[] copyRange(int[] list, int startIndex) - this static method copies the specified range of elements from the parameter array into a new array, and returns it. The initial index of the range (startIndex) must lie between 0 and list.length, inclusive. The value at list[startIndex] is placed into the initial element of the new array (unless startIndex == list.length). Values from subsequent elements in the parameter array are placed into subsequent elements in the new array. The length of the returned array will be list.length - startIndex.
public static int indexOf(int[] list, int searchValue) - this static method searches its parameter array to locate the first occurrence of the parameter searchValue, and returns its index position if found, or -1 if not found. This method must implement the following behavior:
public static int lastIndexOf(int[] list, int searchValue) - this static method searches its parameter array to locate the last occurrence of the parameter searchValue, and returns its index position if found, or -1 if not found. This method must implement the following behavior:
Your class must implement all the search and copy logic for the above methods. It cannot use array methods from the Java API.
Example Output (partial)
All tests should have an OK result. If any have an ERROR result, check your implementation for logic errors.
Testing ArrayUtils class exception handling...
--- Testing minValue method ---
Getting minVal of a null array
OK - minValue threw exception for null array: BadArrayException: Array is null
Getting minVal of an empty array
OK - minValue threw exception for empty array: BadArrayException: Array is empty
...
Getting minVal() of: [50,-10,40,-25,50,40,30]
OK - expected minValue to return -25 and got: -25
Getting minVal() of: [10]
OK - expected minValue to return 10 and got: 10
...
--- Testing copyRange method ---
Getting copyRange of a null array
OK - copyRange threw exception for null array: BadArrayException: Array is null
...
Getting copyRange(list,0) of: []
OK - copyRange copied the array correctly as []
Getting copyRange(list,0) of: [10,15]
OK - copyRange copied the array correctly as [10,15]
Getting copyRange(list,1) of: [10,15]
OK - copyRange copied the array correctly as [15]
Getting copyRange(list,2) of: [10,15]
OK - copyRange copied the array correctly as []
Getting copyRange(list,3) of: [10,15]
OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 3
...
--- Testing indexOf and lastIndexOf method ---
Getting indexOf of a null array
OK - indexOf threw exception for null array: BadArrayException: Array is null
Getting lastIndexOf of a null array
OK - lastIndexOf threw exception for null array: BadArrayException: Array is null
Getting lastIndexOf(5) of: []
OK - expected lastIndexOf to return -1 and got: -1
Getting indexOf(5) of: [5,10,5,15,5]
OK - expected indexOf to return 0 and got: 0
Getting indexOf(0) of: [5,10,5,15,5]
OK - expected indexOf to return -1 and got: -1
Getting indexOf(15) of: [5,10,5,15,5]
OK - expected indexOf to return 3 and got: 3
ArrayUtilsDemo with main() test code
Use the attached ArrayUtilsDemo.java source file to test your class.
Save this source as a separate ArrayUtilsDemo.java file that contains the supplied main() method test code. Compile your BadArrayException.java file first, then your ArrayUtils.java file next, then this ArrayUtilsDemo.java file last.
Note: you must re-compile the ArrayUtilsDemo.java class whenever you change any method signatures (method name, parameters, return type) in your ArrayUtils or BadArrayException class.
Explanation / Answer
please rate - thanks
any questions - ask
sample run
----jGRASP exec: java ArrayUtilsDemo
Testing ArrayUtils class exception handling - 2/07/2014
--- Testing minValue method ---
Getting minVal of a null array
*************
OK - minValue threw exception for null array: BadArrayException: Array is null
Getting minVal of an empty array
OK - minValue threw exception for empty array: BadArrayException: Array is empty
Getting minVal() of: [10]
OK - expected minValue to return 10 and got: 10
Getting minVal() of: [20,30]
OK - expected minValue to return 20 and got: 20
Getting minVal() of: [40,30]
OK - expected minValue to return 30 and got: 30
Getting minVal() of: [10,10]
OK - expected minValue to return 10 and got: 10
Getting minVal() of: [15,25,35]
OK - expected minValue to return 15 and got: 15
Getting minVal() of: [25,20,30]
OK - expected minValue to return 20 and got: 20
Getting minVal() of: [50,40,30]
OK - expected minValue to return 30 and got: 30
Getting minVal() of: [50,-10,40,-25,50,40,30]
OK - expected minValue to return -25 and got: -25
Getting minVal() of: [200,50,-40,60,-15,30,75]
OK - expected minValue to return -40 and got: -40
--- Testing copyRange method ---
Getting copyRange of a null array
OK - copyRange threw exception for null array: BadArrayException: Array is null
Getting copyRange(list,-1) of: []
OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1
Getting copyRange(list,0) of: []
OK - copyRange copied the array correctly as []
Getting copyRange(list,1) of: []
OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1
Getting copyRange(list,-1) of: [20]
OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1
Getting copyRange(list,0) of: [20]
OK - copyRange copied the array correctly as [20]
Getting copyRange(list,1) of: [20]
OK - copyRange copied the array correctly as []
Getting copyRange(list,2) of: [20]
OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 2
Getting copyRange(list,-1) of: [10,15]
OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1
Getting copyRange(list,0) of: [10,15]
OK - copyRange copied the array correctly as [10,15]
Getting copyRange(list,1) of: [10,15]
OK - copyRange copied the array correctly as [15]
Getting copyRange(list,2) of: [10,15]
OK - copyRange copied the array correctly as []
Getting copyRange(list,3) of: [10,15]
OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 3
Getting copyRange(list,-1) of: [30,35,40,45,50]
OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1
Getting copyRange(list,0) of: [30,35,40,45,50]
OK - copyRange copied the array correctly as [30,35,40,45,50]
Getting copyRange(list,1) of: [30,35,40,45,50]
OK - copyRange copied the array correctly as [35,40,45,50]
Getting copyRange(list,2) of: [30,35,40,45,50]
OK - copyRange copied the array correctly as [40,45,50]
Getting copyRange(list,3) of: [30,35,40,45,50]
OK - copyRange copied the array correctly as [45,50]
Getting copyRange(list,4) of: [30,35,40,45,50]
OK - copyRange copied the array correctly as [50]
Getting copyRange(list,5) of: [30,35,40,45,50]
OK - copyRange copied the array correctly as []
Getting copyRange(list,6) of: [30,35,40,45,50]
OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 6
--- Testing indexOf and lastIndexOf method ---
Getting indexOf of a null array
OK - indexOf threw exception for null array: BadArrayException: Array is null
Getting lastIndexOf of a null array
OK - lastIndexOf threw exception for null array: BadArrayException: Array is null
Getting indexOf(5) of: []
OK - expected indexOf to return -1 and got: -1
Getting lastIndexOf(5) of: []
OK - expected lastIndexOf to return -1 and got: -1
Getting indexOf(20) of: [20]
OK - expected indexOf to return 0 and got: 0
Getting indexOf(25) of: [20]
OK - expected indexOf to return -1 and got: -1
Getting lastIndexOf(20) of: [20]
OK - expected lastIndexOf to return 0 and got: 0
Getting lastIndexOf(25) of: [20]
OK - expected lastIndexOf to return -1 and got: -1
Getting indexOf(5) of: [5,10,15,20,10,15,5,10,15,20]
OK - expected indexOf to return 0 and got: 0
Getting indexOf(10) of: [5,10,15,20,10,15,5,10,15,20]
OK - expected indexOf to return 1 and got: 1
Getting indexOf(15) of: [5,10,15,20,10,15,5,10,15,20]
OK - expected indexOf to return 2 and got: 2
Getting indexOf(20) of: [5,10,15,20,10,15,5,10,15,20]
OK - expected indexOf to return 3 and got: 3
Getting indexOf(0) of: [5,10,15,20,10,15,5,10,15,20]
OK - expected indexOf to return -1 and got: -1
Getting lastIndexOf(5) of: [5,10,15,20,10,15,5,10,15,20]
OK - expected lastIndexOf to return 6 and got: 6
Getting lastIndexOf(10) of: [5,10,15,20,10,15,5,10,15,20]
OK - expected lastIndexOf to return 7 and got: 7
Getting lastIndexOf(15) of: [5,10,15,20,10,15,5,10,15,20]
OK - expected lastIndexOf to return 8 and got: 8
Getting lastIndexOf(20) of: [5,10,15,20,10,15,5,10,15,20]
OK - expected lastIndexOf to return 9 and got: 9
Getting lastIndexOf(0) of: [5,10,15,20,10,15,5,10,15,20]
OK - expected lastIndexOf to return -1 and got: -1
Done - press enter key to end program
----jGRASP: operation complete.
-----------------------------------------------
ArrayUtilsDemo unchanged so not posting it
------------------------------------------------
public class BadArrayException extends Exception
{
public BadArrayException()
{
}
public BadArrayException(String message)
{super(message); //pass message to to the parent Exception(String) constructor.
}
}
------------------------------------------------
public class ArrayUtils
{public static int minValue(int[] list)throws BadArrayException
{int min,i;
if(list==null) //null?
throw new BadArrayException("Array is null");
else if(list.length==0) //empty
throw new BadArrayException("Array is empty");
else //neither so find smallest
{min=list[0]; //start assuming element 0 is smallest
for(i=1;i<list.length;i++) //and if any other are smaller, make the substitution
if(list[i]<min)
min=list[i];
return min; //return the smallest
}
}
public static int[] copyRange(int[] list,int startIndex)throws BadArrayException,ArrayIndexOutOfBoundsException
{int i,j=0;
if(list==null) //null?
throw new BadArrayException("Array is null");
else if(startIndex<0||startIndex>list.length) //index out of bounds?
throw new ArrayIndexOutOfBoundsException(startIndex);
else //good index, good array
{int[] a=new int[list.length-startIndex]; //create new array
for(i=startIndex;i<list.length;i++) //fill it
{a[j] = list[i];
j++;
}
return a; //return it
}
}
public static int indexOf(int[] list, int searchValue) throws BadArrayException
{int i;
if(list==null) //null?
throw new BadArrayException("Array is null");
else if(list.length==0) //no elements?
return -1; //return -1
else
{ for(i=0;i<list.length;i++) //starting at beginning of list
if(list[i]==searchValue) //return the index as soon as element is found
return i;
return -1; //get here only if element not found
}
}
public static int lastIndexOf(int[] list, int searchValue) throws BadArrayException
{int i;
if(list==null) //null?
throw new BadArrayException("Array is null");
else if(list.length==0) //no elements?
return -1; //return -1
else
{ for(i=list.length-1;i>=0;i--) //starting at end of list
if(list[i]==searchValue) //return the index as soon as element is found
return i;
return -1; //get here only if element not found
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.