Question: Assignments Data structures Arrays Operation Enhancement Assignment Attached File: ArraysOperations.java (1.174 kb) Improve the code taken at lecture by adding more constraints on array size

Assignments

Data structures Arrays Operation Enhancement Assignment

Attached File: ArraysOperations.java (1.174 kb)

Improve the code taken at lecture by adding more constraints on array size the

add and Remove methods of an array.

For Example,

  1. Adding an element to a full array will lead either to error message or you can enlarge the size of the array and keep the old and new Elements.
  2. Removing an index which already dose not have an element will lead to error Message.

You should Implement the error message using EXCPTION of your own.

Please submit a code that represent your understanding

Do not restrict yourself to any requirements.

public class ArraysOperations {

static int numOfElement; // Num of elements added to array which is different from array length public static void printArray(int [] a ) { for(int i=0;i= pos ; i--) a[i+1]=a[i]; a[pos]=val; numOfElement++; } public static int deleteElementAtPos(int a[], int pos) { // shift left int temp = a[pos]; for ( int i= pos ; i < numOfElement-1 ; i++) a[i]=a[i+1]; numOfElement--; return temp; } public static void main(String args[]) { int [] array = new int[10]; numOfElement = 0; initializeArray(array); printArray(array); int pos = 9; int element = 200; insertElement(array, pos, element); printArray(array); int temp = deleteElementAtPos(array, 2); System.out.println(temp); printArray(array);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!