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

I am having issues with this project and was wondering if anyone can help me? I

ID: 3581605 • Letter: I

Question

I am having issues with this project and was wondering if anyone can help me? I am struggling with this and I really would like some help. Thanks!

Part 1: Create a Class file named GenericLinearsearch that contains a generic method named Search which implements the linear search algorithm. Method Search should compare the search key with each element in the array until the search key is found or until the end or the array is reached f the search key is found, return its location in the array; otherwise return -1. Part 2 Write a test application with a user interface that resembles Figure 1. D Generic Linear Search O p Generic Linear Search Create Create Create Create integers doubles integers doubles Index Value 895 783 943 409 Enter Search Key 409 Enter Search Key: Search Search Found value in index 3 Figure 2-Integer Search Result Figure 1 Test Application Interface The interface will provide buttons that the user can click to create arrays of random integer or double values. The values generated for the arrays should be displayed in the ListBox so that the user knows what values can be searched for The user will enter an integer or double in the search key TextBox and click the Search button to begin the search. The search will utilize the generic Search method The Label at the bottom of the form will be used to report the results ofthe search. Figure 2 shows an example of an integer search HINT: The type parameter for method Search should be constrained with IComparable (Of T) so that you can use the method compareTo to compare the search key to the elements in the array Page 1 of 1

Explanation / Answer

PART 1:

public class GenericLinearSearch {

public static void main(String[] args) {
Integer[] list = new Integer[40];
for (int i = 0; i < list.length; i++) { //for loop
list[i] = i;
}
System.out.println(linearSearch(list, 1)); //Print-out
System.out.println(linearSearch(list, 10));
System.out.println(linearSearch(list, 30));
}
public static <E extends Comparable<E>> int linearSearch(E[] list, E value) { //linearsearch declaration
for (int i = 0; i < list.length; i++) {
if (list[i].compareTo(value) == 0) {
return i;
}
}

return -1;               //returns -1 otherwise
}
}

PART II:


import java.util.Scanner;

class LinearSearchusrinterface
{
public static void main(String args[])   
{
int c, n, search, array[]; //Int declarations

Scanner in = new Scanner(System.in);
System.out.println("Please Enter number of elements"); //Enter the no. of elements
n = in.nextInt();
array = new int[n]; //array = new integers

System.out.println("Enter " + n + " integers"); //Enter the integers

for (c = 0; c < n; c++)
array[c] = in.nextInt();

System.out.println("Please Enter the value to find"); //Enter the number to search
search = in.nextInt();

for (c = 0; c < n; c++)
{
if (array[c] == search) /* Searching element is present */
{
System.out.println(search + " is present at location " + (c + 1) + ".");
break;
}
}
if (c == n) /* Searching element is not present*/
System.out.println(search + " is not present in THE array."); //Printout section
}
}