Answer the following questions for the method search() below: public static int
ID: 3801980 • Letter: A
Question
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 Null Pointer Exception//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 "Location of element in list" fails the disjoints property. Give an example that illustrates this. "Location of element in list" fails the completeness property. Give an example that illustrates this. Supply one or more new partitions that capture the intent of "Location of e in list" but do not suffer from completeness or disjoints problems.Explanation / Answer
import java.util.ArrayList;
import java.util.List;
public class SearchList {
static List<Integer> list=new ArrayList<Integer>();
static int searchedIndex;
static int element=10;
public static int search(List list,int element){
if(list==null || element<0){
throw new NullPointerException("List is null or element is not valid, please provide valid input.");
}else if(list.contains(element)){
for(int i=0;i<list.size();i++){
if(list.get(i).equals(element)){
searchedIndex=i;
}
}
return searchedIndex;
}else{
return -1;
}
}
public static void main(String[] args) {
list.add(4);
list.add(7);
list.add(10);
list.add(3);
list.add(2);
list.add(15);
list.add(13);
list.add(20);
int result=search(list,element);
System.out.println("Element at first Location in list is: "+list.get(0));
System.out.println("Element at last Location in list is: "+list.get(list.size()-1));
System.out.println("Location of element "+element+" found at index: "+result);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.