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

I need help with the following code. Where there are comments on what to do is w

ID: 3750612 • Letter: I

Question

I need help with the following code. Where there are comments on what to do is what I need help with.
package apps;
public class MyArrayForInt2 {
private int[] nums;
private int numElements=0;

public MyArrayForInt2(){
  nums = new int[100];  //O(1) * 1 = O(1)
}
public MyArrayForInt2(int size){
  nums = new int[size]; //O(size) * 1 = O(size)
}
public MyArrayForInt2(int[] numbers){ //O(numbers.length) or O(N)
  nums = new int[numbers.length]; //O(numbers.length) * 1
  for(int i=0; i<numbers.length;i++) //O(1) * num.length = O(nums.length)
   nums[i] = numbers[i]; //O(1) * nums.length = 0(nums.length)
  numElements = nums.length; //O(1) * 1 = O(1)
}
public void printArray() { //0(numElements) or O(N)
  System.out.print("printArray("+size()+","+capsize()+"): "); //O(1) * 1 = O(1)
  for(int i=0; i<numElements ; i++) //O(1) * numElements = O(numElements)
   System.out.print(nums[i]+" "); //O(1) * numElements = O(numELements)
  System.out.println();    //O(1) * 1 = O(1)
}
public boolean isFull() { //O(1)
  // return true if the array is full. return false if not.
  return numElements == nums.length;
}
public boolean isEmpty() { //O(1)
  // return true if the array is empty. return false if not.
  return numElements == 0;
}
public int size() { //O(1)
  // return number of elements in the array
  return numElements; //o(1) * 1 = O(1)
}
public int capsize() { //O(1)
  // return capacity of the array
  return nums.length; //O(1) * 1 = O(1)
}
public void clear() {
  // remove all the elements in the array.
  numElements = 0;
}
public int[] toArray() {  
  // return a copy of the array 'nums'
  int [] new_numbers = new int[numElements];
  for (int i = 0; i < numElements; i++)
   new_numbers[i] = nums[i];
  return new_numbers;
}
// public MyArrayForInt2 clone() {
//  // return a copy of the this, MyArrayForInt2 class.
//     int [] copyArray = this.toArray();
//     MyArrayForInt2 NewMyArrayForInt2 = new MyArrayForInt2(copyArray);
//  
//  return NewMyArrayForInt2;
//  
// }
private void enlarge() { //O(numELements)
  // increase the size of the array twice
  int[] new_nums = new int[nums.length*2]; //O(nums.length) * 1
  for(int i=0;i<numElements;i++) //O(1) * numElements = O(numElements)
   new_nums[i] = nums[i];  //O(1) * numELements = O(numElements)
  nums = new_nums;     //O(1) * 1
}
public void trimToSize() {
  // trim the capacity of the array to numElements.
  nums = new int[numElements];
}
public void ensureCapacity(int minCapacity) {
  // increase the capacity if it is less than minCapacity
  // Do nothing if it is already greater than that.
  int capacity = numElements;
  if (capacity < minCapacity) {
   capacity++;
  }
   
}
public void add(int val) {
  // add val to the end of the array. no need to keep the order
  if (isFull()) enlarge();  //O(numElements) * 1
  nums[numElements++] = val; //O(1) * 1
}
public void add(int idx, int val) {
  // insert val at the specified position idx. no need to keep the order
  if (isFull()) enlarge();
  nums[numElements++] = nums[idx];
  nums[idx] = val;
}
public void add_k(int idx, int val) {
  // insert the given val at the specified position idx.
  // Need to keep the order. Right-shift the numbers from idx.
  if (isFull()) enlarge();
  for(int i=numElements-1;i>=idx;i--)
   nums[i+1] = nums[i];
  nums[idx] = val;
  numElements++;
}
public void add(int[] valArray) {
  // add all elements of valArray to the end of the array.
  
}
public void add_k(int idx, int[] valArray) {
  // add all elements of valArray from the specified position.
  // need to keeep order
  // eg) [10,20,30] --> add_k(1,[1,2]) makes [10,1,2,20,30]
  
}

public void remove(int val) {
  // remove first instance of val. No need to keep order.
  int index = linearSearch(val);
  if (index >= 0) {
   nums[index] = nums[numElements-1];
   numElements--;
  }
}
public void removeL(int idx) {
  // remove the number at idx. No need to keep order.
}
public void remove_k(int val) {
  // remove first instance of val. Keep order.
  int index = linearSearch(val);
  if (index >= 0) {
   for(int i=index; i<numElements-1;i++)
    nums[i] = nums[i+1];
   numElements--;
  }
}
public void removeL_k(int idx) { // remove Location
  // remove the number at idx. Keep order.
}
public void removeAll_k(int val) {
  // remove all instances of val. Keep order.
  // eg) [10,2,10,-1,10] --> removeAll_k(10) makes [2,-1]
//  for(int i= 0; i<countOf(val); i++)
//   remove_k(val);
  int j = 0;
  for(int i=0; i<numElements; i++)
   if(nums[i] != val)
    nums[j++] = nums[i];
  numElements = j;
}
public void removeRange_k(int start, int end) {
  // remove all in the range of [start,end]. Keep order.
  // eg) [10,20,30,40,50] --> removeRange_k(1,3) makes [10,50]
}
public int countOf(int val) {
  // return the count of val in the array
  int count = 0;
  for(int i=0; i<numElements; i++)
   if(nums[i] == val)
    count++;
  return count;
}

public int findMaxIdx() {
  int maxIdx = 0;
  for(int i=1; i<nums.length; i++)
   if(nums[i] > nums[maxIdx]) // new challenger is greater
    maxIdx = i;
  return maxIdx;
}
private int binarySearch(int val) {
  int start = 0;
  int end = nums.length-1;
  int midpoint;
  while(start<=end) {
   midpoint = (start + end)/2;
   if (val > nums[midpoint])
    start = midpoint + 1;
   else if (val < nums[midpoint])
    end = midpoint - 1;
   else // val == nums[midpoint]
    return midpoint;
  }
  return -1;
}

public int search(int val) {
  return linearSearch(val);
}
private int linearSearch(int val){
  // returns the index of the given value 'val'.
  // if 'val' is not found, returns -1
  for(int i=0;i<nums.length;i++)
   if (nums[i] == val)
    return i;  
  return -1;
}
}

Explanation / Answer

public class MyArrayForInt2 { private int[] nums; private int numElements = 0; public MyArrayForInt2() { nums = new int[100]; // O(1) * 1 = O(1) } public MyArrayForInt2(int size) { nums = new int[size]; // O(size) * 1 = O(size) } public MyArrayForInt2(int[] numbers) { // O(numbers.length) or O(N) nums = new int[numbers.length]; // O(numbers.length) * 1 for (int i = 0; i