Please Help with filling in methods that add a number into the array at a specif
ID: 3891556 • Letter: P
Question
Please Help with filling in methods that add a number into the array at a specified index, and a method that removes whatever number is at a specified index. /* Arrays, File input and methods inserts and removes a number from the array */ import java.io.*; import java.util.*; public class Project { public static void main (String[] args) throws Exception { final int ARRAY_MAX = 30; // "args" is the list of tokens you put after "java Project3" on command line if (args.length == 0 ) // i.e If you did not type anything after "java Project4" on command line { System.out.println("FATAL ERROR: Must type a filename on cmd line " + "Like this -> C:\Users\tim\Desktop>java Project4 P4input.txt"); System.exit(0); //ABORT program. Make user try again with a filename this time. } Scanner infile = new Scanner( new File(args[0]) ); int[] array = new int[ARRAY_MAX]; int count=0; while ( infile.hasNextInt() ) array[count++] = infile.nextInt(); // POST increment NOT pre. Do you see why? System.out.println( "array capacity: " + array.length + " array count: " + count); System.out.println( " 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18"); printArray( array, count, "numbers from " + args[0] + ": " ); // ECHO ALL (count) ELEMENTS ON ONE LINE insertNumberAtIndex( array, count, 17, 13 ); // add the number 17 to the array at index [13] ++count; printArray( array, count, "after insert of 17 at [13]: " ); insertNumberAtIndex( array, count, 55, 6); // add the number 17 to the array at index [13] ++count; printArray( array, count, "after insert of 55 at [6]: " ); removeNumberAtIndex( array, count, 9 ); // remove the number at [9] --count; printArray( array, count, "after removal at index [9]: " ); removeNumberAtIndex( array, count, 3 ); // remove the number at [3] --count; printArray( array, count, "after removal at index [3]: " ); } // END main // GIVEN AS IS: DO MOT MODIFY Or DELETE private static void printArray( int[] a, int cnt, String caption ) { System.out.print( caption ); for ( int i=0 ; i<cnt ;i++) System.out.format( "%3d ", a[i] ); System.out.println(); } // ############################################################################################# // YOU MUST WRITE THE DEFINITIONS OF THE METHODS BELOW // ############################################################################################# // insertNumberAtIndex - adds a number to the array at specified slot # // opens up ahole for it by shifting everything at and above [atIndex] up one slot private static void insertNumberAtIndex( int arr[], int count, int newNumber, int atIndex ) { /* YOUR CODE HERE */ } // removeNumberAtIndex - removes the number at [atIndex] by shifing elements above it down one private static void removeNumberAtIndex( int arr[], int count, int atIndex ) { /* YOUR CODE HERE */ } } //END
Explanation / Answer
Method 1
private static void insertNumberAtIndex(int arr[],int count,int newNumber,int atIndex){
for(int i = (count-1); i >= (atIndex-1); i--)
{
arr[i+1] = arr[i];
}
arr[atIndex-1] = newNumber;
}
Method 2:
private static void removeNumberAtIndex(int arr[],int count,int atIndex){
for(int i = atIndex; i < count -1; i++){
arr[i] = arr[i + 1];
}
arr[count-1]=0; as we cannot shrink the array.
}
if you like the answer please provide a thumbs up.