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

To do\'s in the code public class ArrayLinearSearch { public static void main(St

ID: 3708356 • Letter: T

Question

To do's in the code

public class ArrayLinearSearch {
public static void main(String[] args) {
int[] myList = {95, -10, 23, -3, 78};

System.out.print("myList: ");
printArray(myList);
  
//TODO1: Write code to search for the item 23 and print its index in the array

//TODO2: Write code to search for the item 90 and print its index in the array. If the item is not in the array.
// print -1 as the index
  
}
  
/* TODO3:
* (1)
* Define a method linearSearch. Given an int array and an int key,
* the method searches for the key in the array and returns the
* location (index) of the first occurrence of the key.
*
* Note tha if the key is not in the array, -1 should be returned.
*
* Examples:
* If the list is 10, 40, 20 and 30, and the key is 20,
* the method should return 2.
*
* If the list is 10, 40, 20 and 30, and the key is 50,
* the method should return -1.
*
* (2)
* Change the code in the main to method invocations to linearSearch.
*/

  

  
  
/*
* printArray: Given an int array, the method print out all the elements of the array.
*/
public static void printArray(int[] list) {
for (int number: list) {
System.out.print(number + " ");
}
System.out.println();
}
  
}

Explanation / Answer

Hello ,

Code for ToDo-1 and ToDO -2

public class ArrayLinearSearch {

   public static void main(String[] args) {

      int[] myList = {95, -10, 23, -3, 78};

     

      System.out.print("myList: ");

      printArray(myList);  

      boolean flag;

  

      /*Code for searching 90*/

      flag=false;

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

           if(myList[i]==23){

               flag=true;

               System.out.println(i);

           }

              

       }

      if(!flag){

         System.out.println(-1);

      }

      /*Code for searching 90*/

      flag=false;

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

           if(myList[i]==90){

               flag=true;

               System.out.println(i);

           }

              

       }

      if(!flag){

         System.out.println(-1);

      }

      }

   public static void printArray(int[] list) {

      for (int number: list) {

      System.out.print(number + " ");

      }

      System.out.println();

      }

}

Code for TODO -3 (Modifying above code)

public class ArrayLinearSearch {

/* Linear Search function*/

   public static int linearSearch(int []myList , int x){

       if(myList.length==0)

           return -1;

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

           if(myList[i]==x)

               return i;

       }

       return -1;      

   }

  

   public static void main(String[] args) {

      int[] myList = {95, -10, 23, -3, 78};

     

      System.out.print("myList: ");

      printArray(myList);  

      System.out.println(linearSearch(myList, 23)); //linear search invoked for searching elements and printing output

      System.out.println(linearSearch(myList, 90));

      }

   public static void printArray(int[] list) {

      for (int number: list) {

      System.out.print(number + " ");

      }

      System.out.println();

      }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote