In this exercise, you need to write a static “search” method that will take an a
ID: 3806953 • Letter: I
Question
In this exercise, you need to write a static “search” method that will take an array of Comparable objects and a Comparable key as parameter and it returns true if the key is present in the array, otherwise it should return false. Test your implementation with two different type of Comparable objects. Test your code is to create two arrays, one for Monster and another for Employee , then pass each one to the “search” method for testing. Save this file as SearchClient.java.
"I need a java program"
Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
public class SearchClient {
public static boolean search(Comparable list[], Comparable key){
if(list == null || key == null)
return false;
for(int i=0; i<list.length; i++)
if(list[i].compareTo(key) == 0)
return true;
return false;
}
public static void main(String[] args) {
Integer[] list = {1,2,3,4,5};
System.out.println(search(list, 4));
System.out.println(search(list, 6));
String[] list1 = {"pravesh", "alex", "bob", "vk"};
System.out.println(search(list1, "bob"));
}
}
/*
Sample run:
true
false
true
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.