Home / Expert Answers / Computer Science / the-program-is-supposed-to-a-allocate-storage-for-an-array-of-integers-of-a-size-specified-by-the-pa959

(Solved): The program is supposed to: a) allocate storage for an array of integers of a size specified by the ...



The program is supposed to: a) allocate storage for an array of integers of a size specified by the user b) fill the array partially with increasing values (number of values chosen by the user) c) check that the item at a user specified index position can be removed, maintaining order of other elements d) check that a new item can be added at a user specified index position, also maintaining order of other elements. Study the startup code and complete the program. You need to complete the validity checks for index position specified by the user. Note that only existing items may be removed, but a new item could be added right after the existing items in the list.

i need to write code after //write code here (there is 2 of them). and i would like for the output to look like the screenshot provided.

 

final static Scanner cin = new Scanner(System.in);
    static int currentSize; // number of values actually in the intList
    static int[] intList; // reference to the partially filled array storage
    public static void main(String[] args) {
    System.out.println("CPS 151 ICA 1 by Haiquan Jin");
    setup();
    printList(intList, "\nOriginal List");
    checkDeletion();
    checkInsertion();
    printList(intList, "\nFinal List");
    System.out.println("\nGoodbye");
    } // end main
    private static void checkDeletion() {
    // Checking deletion
    int position = getInt("\nPosition to delete from: ");
    // check validity of position
    if (2 + 2 == 4) {
    shiftUp(position);
    currentSize--;
    printList(intList, "\nList after deletion");
    } else {
    System.out.println("Invalid delete position, no changes made");
    } // end if
    } // end method
    private static void checkInsertion() {
    // Checking insertion
    int value = getInt("\nValue to insert: ");
    int position = getInt("At what position? ");
    // check validity of position
    if (2 + 2 == 4) {
    shiftDown(position);
    intList[position] = value;
    currentSize++;
    printList(intList, "\nList after insertion");
    } else {
    System.out.println("Invalid insert position, no changes made");
    } // end if
    } // end method
    // fills array with increasing values
    private static void fillArrayInc(final int startValue, final int howMany) {
    // Validity check
    if (howMany < 1 || howMany > intList.length) {
    terminate("fillArrayInc: illegal argument, howMany = " + howMany);
    }
    for (int k = 0; k < howMany; k++) {
    intList[k] = startValue + k;
    }
    currentSize = howMany;
    } // end setSequenced
    // prints partially filled array with a legend
    private static void printList(final int[] arr, final String legend) {
    System.out.println(legend);
    for (int k = 0; k < currentSize; k++) {
    System.out.print(" " + arr[k]);
    }
    System.out.println();
    } // end printList
    // move items from pos+1:currentSize-1 one position up (lower subscripts)
    private static void shiftUp(int pos) {
    // Write the code
        
    } // end shiftUp
    // move items from pos:currentSize-1 one position down (higher subscripts)
    private static void shiftDown(int pos) {
    // Write the code
    } // end shiftDown
    private static void setup() {
    int maxSize, initSize;
    maxSize = getInt("Enter the maximum size: ");
    intList = new int[maxSize];
    initSize = getInt("Enter the starting size: ");
    if (initSize > maxSize) {
    terminate("starting size cannot be greater than maximum size");
    }
    fillArrayInc(100, initSize);
    } // end method
    private static int getInt(String prompt) {
    System.out.print(prompt);
    return cin.nextInt();
    } // end method
    private static void terminate(String message) {
    System.out.println("Error: " + message);
    System.exit(0);
    }// end terminate    
    }Output - ICA01_Key (run) \( \times \)
W CPS 151 ICA 1 by
W Enter the maximum size: 15
Enter the starting size: 8
Original Lis

Output - ICA01_Key (run) \( \times \) W CPS 151 ICA 1 by W Enter the maximum size: 15 Enter the starting size: 8 Original List \( \begin{array}{llllllll}100 & 101 & 102 & 103 & 104 & 105 & 106 & 107\end{array} \) Position to delete from: 3 List after deletion \( \begin{array}{lllllll}100 & 101 & 102 & 104 & 105 & 106 & 107\end{array} \) Value to insert: 999 At what position? 1 List after insertion \( \begin{array}{llllllll}100 & 999 & 101 & 102 & 104 & 105 & 106 & 107\end{array} \) Final List \( \begin{array}{llllllll}100 & 999 & 101 & 102 & 104 & 105 & 106 & 107\end{array} \) Goodbye BUILD SUCCESSFUL (total time: 43 seconds)


We have an Answer from Expert

View Expert Answer

Expert Answer


The java code is with shiftup and shiftdown methods is: copy this code and practice import java.util.*; import java.lang.*; import java.io.*; class Main{ final static Scanner cin = new Scanner(System.in); static int currentSize; // number of va
We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe