Question: (java) Implement search as specified below in two ways: using for loops, and using while(true) loops that are terminated when accessing the array raises IndexOutOfBoundsException.

(java)

  1. Implement search as specified below in two ways: using for loops, and using while(true) loops that are terminated when accessing the array raises IndexOutOfBoundsException. Which implementation is better? Discuss

// REQUIRES: a is sorted

// EFFECTS: If a is null throws NullPointerException; else if x is not

// in a, throws NotFoundException; else returns i such that a[i]==x.

public static int search (int[] a, int x)

throws NullPointerException, NotFoundException

Which implementation is better? Discuss

*note

Implement the two static methods with names searchForLoop and searchWhileLoop, respectively, in a class named Searches. You may also need to define new exception types for the implementation.

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!