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

2. Consider the following method (5 pts) public static int mystery(int[] list) {

ID: 3689296 • Letter: 2

Question

2. Consider the following method (5 pts)

public static int mystery(int[] list)

{

int x =0;

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

{

int y = list[i] -list[0];

if (y > x) x =y; }

return x;

}

What value does the method return when passed each of the following arrays?

a. {5} b. {3, 12} c. {4, 2, 10, 8} d. {1, 9, 3, 5, 7} e. {8, 2, 10, 4, 10, 9}

and also,

public boolean addNumber(int numberToAdd) :

The method checks if the integer specified by the parameter exists in the array (This can be done using the indexOf method to see if it returns -1 or not) and also checks if the array has not reached its capacity. If both are satisfied, the number is added to the array at the smallest available index. If the array reached its capacity, double its size by calling the method doubleArrayCapacity() and add the number. If the number is added successfully, then the method returns true. If the number already exists in the array, the new number will not be added, and the method returns false

Explanation / Answer

Hello ,

Values return by the method : mystery are as follow..

a.0
b.9
c.6
d.8
e.2

Find below code for the second function and its O/P .

import java.util.Arrays;

/**
* It contains array operations.
*
* @author dipal.prajapati
*
*/
public class App10 {
   public static void main(String args[]) {
       int[] list1 = { 5 };
       System.out.println("a." + mystery(list1));
       int[] list2 = { 3, 12 };
       System.out.println("b." + mystery(list2));
       int[] list3 = { 4, 2, 10, 8 };
       System.out.println("c." + mystery(list3));
       int[] list4 = { 1, 9, 3, 5, 7 };
       System.out.println("d." + mystery(list4));
       int[] list5 = { 8, 2, 10, 4, 10, 9 };
       System.out.println("e." + mystery(list5) + " ");

       int[] list = new int[4];
       list[0] = 2;
       list[1] = 3;
       list[2] = 65;
       App10 obj = new App10();
       System.out.println(obj.addNumber(4, list) + " ");
       System.out.println(obj.addNumber(5, list) + " ");

   }

   /**
   * Given method in question.
   *
   * @param list
   * @return
   */
   public static int mystery(int[] list) {
       int x = 0;
       for (int i = 1; i < list.length; i++) {
           int y = list[i] - list[0];
           if (y > x)
               x = y;
       }
       return x;
   }

   /**
   * To get the index of element in the array.
   *
   * @param arr
   * @param value
   * @return
   */
   public static int getArrayIndex(int[] arr, int value) {

       int k = -1;
       for (int i = 0; i < arr.length; i++) {

           if (arr[i] == value) {
               k = i;
               break;
           }
       }
       return k;
   }

   /**
   * It adds the number to array if does not already contains and also check
   * if array is full and then double its capacity.
   *
   * @param numberToAdd
   * @param array
   * @return
   */
   public boolean addNumber(int numberToAdd, int[] array) {
       boolean isFull = true;
       if (getArrayIndex(array, numberToAdd) == -1) {
           // check if there are any empty spots.
           for (int i = 0; i < array.length; i++) {
               if (array[i] == 0) {
                   isFull = false;
               }
           }
           if (isFull) {
               System.out.println("Array is full.");
               int oldSize = array.length;
               int[] tmp = new int[2 * oldSize];
               System.arraycopy(array, 0, tmp, 0, array.length);
               array = tmp;
               array[oldSize] = numberToAdd;
               System.out.println("Array is resized.");
               System.out.println("Element " + numberToAdd + " added at index " + oldSize);
               return true;
           } else {
               for (int i = 0; i < array.length; i++) {
                   if (array[i] == 0) {
                       array[i] = numberToAdd;
                       System.out.println("Element " + numberToAdd + " added at index " + i);
                       return true;
                   }
               }
           }
       } else {
           System.out.println("Array contains " + numberToAdd);
           return false;
       }
       return false;
   }
}

==O/P==

a.0
b.9
c.6
d.8
e.2

Element 4 added at index 3
true

Array is full.
Array is resized.
Element 5 added at index 4
true

Let me know if you have any quereis.

Thanks.