1. (6 points) Complete the linear search method from today’s lecture. Create a class containing this method (as a static member) and a main method that, when run, creates and initializes an array with 100 random numbers and calls your search method for at least 5 values, three which are in the array and two which are not. Use System.out.printf to report the results of  each search. Test your program and print out the class. Note: the size of  the array (100) should be defined using a constant field of  the class. 2. (4 points) Modify the program to create an array large enough to hold 10,000 random numbers. Otherwise it should work as before. If  you wrote the first version of  the program well, you shouldn’t have to change much at all. Test as before, and print out your modified class. 3. There is a better way to specify the size of  the array that we want the program to create and fill with random numbers, so we don’t have to change the code every time we want to test a different array size. Do you know what it is? Prepare to discuss this question in class (nothing to turn in).

1. Linear Search Method:

The linear search algorithm is a simple and straightforward method used to find the position of a specific value in an array. It involves iterating through the elements of the array one by one until the desired value is found or the entire array has been traversed.

To complete the linear search method in today’s lecture, we can create a class with the method defined as a static member. Here is an example implementation:

“`
public class LinearSearch {
private static final int ARRAY_SIZE = 100;
private static int[] array = new int[ARRAY_SIZE];

public static int search(int key) {
for (int i = 0; i < ARRAY_SIZE; i++) { if (array[i] == key) { return i; } } return -1; } public static void main(String[] args) { // Initialize the array with 100 random numbers for (int i = 0; i < ARRAY_SIZE; i++) { array[i] = (int) (Math.random() * 100); } // Search for at least 5 values int[] values = {10, 20, 30, 40, 50}; for (int value : values) { int index = search(value); if (index != -1) { System.out.printf("%d was found at index %dn", value, index); } else { System.out.printf("%d was not found in the arrayn", value); } } } } ``` In the above code, we define the size of the array using a constant field `ARRAY_SIZE`. We create an array of integers with the specified size and populate it with random numbers between 0 and 99. The `search` method performs the linear search and returns the index of the found value or -1 if the value is not present in the array. The `main` method initializes the array, performs the searches for the specified values, and prints the results using `System.out.printf`. 2. Modified Program with Larger Array: To modify the program to create an array large enough to hold 10,000 random numbers, we simply need to change the value of the `ARRAY_SIZE` constant to 10,000. Here is the modified code: ``` public class LinearSearch { private static final int ARRAY_SIZE = 10000; // Rest of the code remains the same as in the previous version ... } ``` By changing the value of the `ARRAY_SIZE` constant, we can create an array with the desired size without making any significant changes to the rest of the program. 3. A Better Way to Specify Array Size: There is a better way to specify the size of the array without changing the code every time we want to test a different array size. One possible approach is to provide the array size as a command-line argument when running the program. By using command-line arguments, we can pass the desired array size as input at runtime, allowing for more flexibility. This eliminates the need to modify the code manually each time we want to test a different array size. To implement this, we can modify the `main` method as follows: ``` public static void main(String[] args) { if (args.length != 1) { System.out.println("Usage: java LinearSearch “);
return;
}

int arraySize = Integer.parseInt(args[0]);
int[] array = new int[arraySize];
// Rest of the code remains the same

}
“`

In this modified version, we check if the number of command-line arguments is exactly 1. If not, we print a usage message and exit. If a single argument is provided, we parse it as an integer and use it to determine the array size.

Need your ASSIGNMENT done? Use our paper writing service to score better and meet your deadline.


Click Here to Make an Order Click Here to Hire a Writer