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

I have the following program that I need to use to search for a number in an uns

ID: 3532914 • Letter: I

Question

I have the following program that I need to use to search for a number in an unsorted array:


/** Return true if target appears in the sorted array data. */
public static boolean linearSearch(int[] data, int target) {
    for (int i = 0; (i < data.length) && (data[i] <= target); i++) {
      if (data[i] == target) {
        return true;
      }
    }
    return false;
}


1) how do i create an Array List of numbers? 2) What code do I need in order to do below instructions?


call to search the following integers in the array interactively as below:

          Enter the number you want to search or -1 to quit: 14

                        NOT FOUND!

            Enter the number you want to search or -1 to quit: 9

                        FOUND! It is in the array index #10.

            Enter the number you want to search or -1 to quit: 0

                        NOT FOUND!

Explanation / Answer

import

java.io.BufferedReader;

import

java.io.IOException;

import

java.io.InputStreamReader;

import

java.util.Random;

?

public

class ArraySearch {

public static void main(String[] args) {

int inputNum = 0;

InputStreamReader converter =

new InputStreamReader(System.in);

BufferedReader in =

new BufferedReader(converter);

int[] data = {1,3,4,6,8,9,14,23,134};

//Makes an array of size (20 in this case) of random ints

// int size = 20;

// data = new int[size];

// Random rand = new Random();//Argument is the size limit of the int

// int mod = 1324; // gives a size limit so numbers won't be greater than 1324

// for(int x = 0 ; x < size; x++){

// data[x]= rand.nextInt()%mod;

// }

?

while(inputNum!=-1){

try {

System.

out.println("Enter the number you want to search or -1 to quit:");

inputNum=

new Integer(in.readLine());

if(inputNum!=-1){

if(linearSearch(data, inputNum)){

System.

out.println("FOUND! It is in the array index #"+linearSearchPosition(data, inputNum));

}

else {

System.

out.println("NOT FOUND!");

}

//Alternate version

// int position = linearSearchPosition(data, inputNum);

// if(position>-1){

// System.out.println("FOUND! It is in the array index #"+linearSearchPosition(data, inputNum));

// } else {

// System.out.println("NOT FOUND!");

// }

}

}

catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

System.

out.println("Exiting...");

}

?

?

/** Return true if target appears in the sorted array data. */

public static boolean linearSearch(int[] data, int target) {

for (int i = 0; (i < data.length) && (data[i] <= target); i++) {

if (data[i] == target) {

return true;

}

}

return false;

}

/** Return true if target appears in the sorted array data. */

public static int linearSearchPosition(int[] data, int target) {

for (int i = 0; (i < data.length) && (data[i] <= target); i++) {

if (data[i] == target) {

return i;

}

}

return -1;

}

}