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

The method will take an array of doubles and replace any values in the array tha

ID: 3598994 • Letter: T

Question

The method will take an array of doubles and replace any values in the array that are larger than a give target value by 0.0 For example, if the array contained (7.9,2.3,4.1, 5.2, 6.3), and the target value were 6.0, the array would contain (0.0, 2.3, 4.1, 5.2,0.0) after the method call. a) Write the signature of the method. The method signature contains a return type, the method name and any necessary parameter(s). b) Complete the declaration of variables below and call the method with the data given in the examp above doublel array int target c) What will the value of index be after the code below is run? intl) array- (9, 4, 2, 1,7,3, 5) Arrays.sort(array): int index Arrays.binarySearch(array, 6):

Explanation / Answer

a)    Signature of the method is

   void replaceHigher(double a[], int target)


   void replaceHigher(double a[], int target){
       for(int i=0;i<a.length;i++){
           if(a[i]>target){
               a[i] = 0;
           }
       }
   }
  
b)    double[] array = {7.9,2.3,4.1,5.2,6.3};
   int target = 6;
   replaceHigher(array,target)
  
c)    Answer is -6

   Given
   int array[] = {9,4,2,1,7,3,5};
   Arrays.sort(array); //This statement will sort the array in ascending order
   //After the sorting array will be {1,2,3,4,5,7,9}
  
   int index = Arrays.binarySearch(array,6);
   //binarySearch method will binary search the given key and returns the index of the elemnt, if the element is found.
   If the element is not found, it will return the negative 1-index where this element would go in the sorted array.
   Here 6 is not present in the array. But it would go after 5 in the sorted array
   So, ans is -6
   Arrays.binarySearch(array,0) would give -1 since 0 will go to the beginning of the array

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