Answer the following questions for the method search below p

Answer the following questions for the method search() below:
public static int search (List list, Object element)
// Effects: if list or element is null throw NullPointerException
// else if element is in the list, return an index
// of element in the list; else return -1
// for example, search ([3,3,1], 3) = either 0 or 1
// search ([1,7,5], 2) = -1
Base your answer on the following characteristic partitioning:
Characteristic: Location of element in list
Block 1: element is first entry in list
Block 2: element is last entry in list
Block 3: element is in some position other than first or last
(a) “Location of element in list” fails the disjointness property. Give an example
that illustrates this.
(b) “Location of element in list” fails the completeness property. Give an
example that illustrates this.
(c) Supply one or more new partitions that capture the intent of “Location
of e in list” but do not suffer from completeness or disjointness
problems.

Solution

Hi, Please find my implementation.

Please let me know in case of any issue.

public static int search (List list, Object element){

   // Effects: if list or element is null throw NullPointerException

   // else if element is in the list, return an index

   // of element in the list; else return -1

   // for example, search ([3,3,1], 3) = either 0 or 1

   // search ([1,7,5], 2) = -1

      

       if(list == null || element == null)

           throw new NullPointerException();

      

       int size = list.size();

      

       if(list.get(0).equals(element))

           return 0;

       if(list.get(size-1).equals(element))

           return size-1;

      

       for(int i=1; i<size-1; i++)

           if(list.get(i).equals(element))

               return i;

      

       return -1; // not available in list

   }

Answer the following questions for the method search() below: public static int search (List list, Object element) // Effects: if list or element is null throw
Answer the following questions for the method search() below: public static int search (List list, Object element) // Effects: if list or element is null throw

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site