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

/* * (name header) */ import java.util.Scanner; public class ArrayMethods { publ

ID: 3767636 • Letter: #

Question

/*

* (name header)

*/

import java.util.Scanner;

public class ArrayMethods

{

public static final int CAPACITY = 20;

public static final int MAX = 10;

public static Scanner input = new Scanner(System.in);

  

public static void main(String[] args)

{

int myArray[] = new int[CAPACITY];

int numOfElements = processArray(myArray);

  

char again = 'y';

do

{

askToInsert(myArray, numOfElements);

++numOfElements;

printArray(myArray, numOfElements);

  

int numberToRemove = askToRemove();

int idx = search(myArray, numOfElements, numberToRemove);

if (idx >= 0)

{

remove(myArray, numOfElements, idx);

--numOfElements;

}

else

{

System.out.println("Number not found.");

}

printArray(myArray, numOfElements);

  

System.out.print(" Would you like to repeat the process? (y/n) ");

again = input.next().charAt(0);

System.out.println();

}

while (again == 'y');

}

  

// Method processArray

// Your code here...

  

// Method askToInsert

// Your code here...

  

  

// Method insert

// Your code here...

  

  

// Method askToRemove

// Your code here...

  

  

// Method search

// Your code here...

  

  

// Method remove

// Your code here...

  

  

// Method printArray

// Your code here...

  

  

}

  

For this exercise, you need to implement seven (7) methods using the ex_23_array_methods project. The project contains a main method that declares an array of capacity 20 and calls the methods you need to implement. Do NOT modify the main method.

Explanation / Answer

1. Method processArray:

//Method that processes the array
int processArray(int arr[])
{
   int val, i=-1, count;
  
   //Read values from the user and store in array
   printf(" Enter up to %d integers. Enter -1 to quit: ", MAX);
   scanf("%d",&val);
  
   while(val!=-1)
   {
       if(i<=MAX)
       {
           i++;
           arr[i] = val;
           count++;
           scanf("%d",&val);
       }
       else
       {
           break;
       }
   }
  
   return count;
}

_______________________________________________________________________________________________


2. Method askInsert:

//Method for accepting parameter from user for inserting in to array
void askInsert(int arr[], int size)
{
int val, pos;
printf(" Enter the number you would like to add: ");
scanf("%d",&val);

printf(" At what index? (Max Index %d) ",size);
scanf("%d",&pos);

insert(arr, size, val, pos);
}

_______________________________________________________________________________________________

3. Method insert:

//Method to insert values into array
void insert(int arr[], int size, int value, int index)
{
   arr[index] = value;
}

_______________________________________________________________________________________________

4. Method askToRemove:

// Method that removes element from array by accepting vallue from user
int askToRemove()
{
   int value;
   printf(" Enter the number you would like to remove: ");
   scanf("%d",&value);
   return value;
}

_______________________________________________________________________________________________

5. Method search:

//Method for searching an element in the array
int search(int arr[], int size, int value)
{
   int i=0, index=-1;
  
   while(i<size)
   {
       if(arr[i] == value)
       {
           index = i;
           break;
       }
       else
       {
           i++;
       }
   }
  
   return index;
}

_______________________________________________________________________________________________

6. Method remove:

//Method that removes the element from array
void remove(int arr[], int size, int index)
{
   int i;
  
   for(i=index;i<size-1;i++)
   {
       arr[i] = arr[i+1];
   }
}
_______________________________________________________________________________________________

7. Method printArray:

//Method that pints the array elements
void printArray(int arr[], int size)
{
   int i;
  
   if(size == 0)
   {
       printf(" Array is empty......");
   }
   else
   {
       for(i=0;i<size;i++)
       {
           printf("%d ", arr[i]);
       }
   }
}