Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with this question in Java Write a method that searches a numeric ar

ID: 3705331 • Letter: I

Question

I need help with this question in Java

Write a method that searches a numeric array for a specified value. The method should return the subscript of the element containing the value if it is found in the array. If the value is not found, the method should throw an exception of the Exception class with the error message “Element not found”.

Write a statement that throws an IllegalArgumentException with the error message “Argument cannot be negative”.

Write an exception class that can be thrown when a negative number is passed to a method.

Explanation / Answer

Java Code: Find method in below code

public class ArraySearch {

               

                public static int search(int[] arr, int val) throws IllegalArgumentException, Exception

                {

                                //throw exception if negative value

                                if(val < 0)

                                                throw new IllegalArgumentException("Argument cannot be negative");

                                int index = -1; //set index to -1

                                //search for element

                                for(int i=0; i<arr.length; i++)

                                                if(arr[i] == val) //if found

                                                {

                                                                index = i; //set index to i

                                                                break; //break loop

                                                }

                                //if index is -1, throw exception

                                if(index == -1)

                                                throw new Exception("Element not found");

                                else //otherwise return index value

                                                return index;

                }

               

                public static void main(String[] args) {

                                int[] arr = {1, 2, 3, 4, 5, 6};

                                int val = 95;

                                try {

                                                int index = search(arr, val);

                                                System.out.println(val + " is present at index "+index);

                                               

                                } catch (IllegalArgumentException e) {

                                                e.printStackTrace();

                                } catch (Exception e) {

                                                e.printStackTrace();

                                }

                }

}

Custom exception class: Just replace IllegalArgumentException with myException in above code.

public class myException extends IllegalArgumentException {

    public myException(){

        super();

    }

    public myException(String message){

        super(message);

    }

}