Question: Linear Search- I will need you to comment on each line of the code. Be specific what does each code do or mean and why?

Linear Search- I will need you to comment on each line of the code. Be specific what does each code do or mean and why? This code was in java, thank you!

/**

* create an integer list class that randomly generates an array of numbers

*

*/

public class IntegerList{

int[] list; //values in the list

//-------------------------------------------------------

//create a list of the given size

//-------------------------------------------------------

public IntegerList(int size)

{

list = new int[size];

}

//-------------------------------------------------------

//fill array with integers between 1 and 10, inclusive

//-------------------------------------------------------

public void randomize()

{

for (int i=0; i

list[i] = (int)(Math.random() * 10) + 1;

}

//-------------------------------------------------------

//print array elements with indices

//-------------------------------------------------------

public void print()

{

for (int i=0; i

System.out.println(i + ":\t" + list[i]);

}

public int linearSearch(int target)

{

for(int i = 0; i < list.length; i++)

{

if(list[i] == target)

return i;

}

Return -1;

}

}

/**

* Write a description of class ListDriver here.

*

* @author (your name)

* @version (a version number or a date)

*/

public class ListDriver

{

public static void main (String args[])

{

IntegerList temp = new IntegerList(5);

temp.randomize();

temp.print();

System.out.println("Search for the value 8");

if(temp.linearSearch(8) == -1)

System.out.println("You didn't find the value 8");

else

System.out.println("Congrats, 8 is found");

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!