1. Engineering
  2. Computer Science
  3. final static scanner cin new scannersystemin final static random...

Question: final static scanner cin new scannersystemin final static random...

Question details

This assignment has 2 parts. The main method calls the methods partA ) and then calls partB ). The skeleton code has the call to partB ) commented out. The intention is that you concentrate on the first part Once that part is completed correctly, uncomment the call to partB ) in the main method Part A The method code for partA) is complete. You need to complete the methods shiftUp ) and shiftDown (). Once these methods are written the program will allow you to delete a value from a list maintained using a partially filled array. It will also allow you to insert a value in a specific position within the list. For convenience of the programmer, we assume that the positions specified will be zero-based (i.e subscripts). You can consult sections 6.3.6 and 6.3.7 in the textbook. Note that we want the relative order of existing elements to be preserved in both of these operations Question: What are the valid value range for the variable position (in partA(0) for insertion and deletion (in terms of the variable size? [For this lab you do not need to implement the validity check] Part B Uncomment the call to partB () in main. The code for partB () Is complete. You need to complete the method findPos ). The program then will sort a random array the insertion sort technique. Note that this part reuses the shiftDown function from part A After you have completed both parts of the assignment submit the source code and screen shot as described in the document How to submit assignments through Isidore. The name of the project should be ICA01 and the name of the source code file should be ICA01.java Design of findPos method The findPos method is used to find the right place to insert a new value in a partially filled array. The partially filled array is assumed to be sorted in increasing order and if the new value is inserted at the index position (as calculated by findPos) then the resulting array will remain sorted. This is the method specification given for findPos // find the right place for item to be inserted within arr [0:size-1] private static int findPos (int[ arr, int size, int item) f Suppose size-5 and arr = { 12, 23, 45 , 67, 88). Note that there may be more elements in arr but as a partially filled array, only 5 values are relevant. These are the values returned by the method for various possible values of item. item return value from findPos (arr, 5, item) 0 18 35 45 56 3 (new 45 goes after old 45s) 4 91 You can start a position variable at 0 and keep incrementing it as long as the position issize and item is The final value is the return value from the method. Note: It is also possible to write the loop backward arr[position]

    final static Scanner cin = new Scanner(System.in);
    final static Random rand = new Random();
    final static int MAX_SIZE = 10;     // amount of storage for the intList
    static int size = 0;                        // number of values actually in the intList
    static int[] intList = new int[MAX_SIZE];

     * @param args the command line arguments
    public static void main(String[] args) {
        out.println("CPS 151 In-class assignment 1 by _________________________");
//        partB();
    } // end main

    private static void partA() {
        int position, value;
Part A: Maintain a partially filled array");
        setSequenced(intList, 100, 8);
        size = 8;
        printList(intList, size, "Initial contents");
        // Add code to do insert and delete
Position to delete from: ");
        position = cin.nextInt();
        shiftUp(intList, size, position);
        printList(intList, size, "
List after deletion");

Value to insert: ");
        value = cin.nextInt();
        out.print("At what position? ");
        position = cin.nextInt();
        shiftDown(intList, size, position);
        intList[position] = value;
        printList(intList, size, "
List after insertion");
    } // end partA

    private static void partB() {
Part B: Insertion sort");
        setRandom(intList, 100);
        printList(intList, MAX_SIZE, "
Array before sorting");
        printList(intList, MAX_SIZE, "
Array after sorting");
    } // end partB

    // loads sequenced values, caller responsible for mainitaining the size for
    // the partially filled intList
    private static void setSequenced(int[] intList, final int startValue,
            final int howMany) {
        // Validity check 
        if (howMany < 1 || howMany > intList.length)
            terminate("setSequenced: illegal argument, howMany = " + howMany);
        for (int k = 0; k < howMany; k++) {
            intList[k] = startValue + k;
    } //end setSequenced

    // prints partially filled array with a legend
    private static void printList(final int[] arr, final int size,
            final String legend) {
        for (int k = 0; k < size; k++) {
            out.print(" " + arr[k]);
    } // end printList

    // move items from pos+1:size-1 one position up (lower subscripts)
    private static void shiftUp(int[] arr, int size, int pos) {
        // Write the code

    } // end shiftUp

    // move items from pos:size-1 one position down (higher subscripts)
    private static void shiftDown(int[] arr, int size, int pos) {
        // Write the code

    } // end shiftDown

    private static void setRandom(int[] arr, final int range) {
        for (int k = 0; k < arr.length; k++) {
            arr[k] = rand.nextInt(range);
    } // end setRandom

    private static void sort(int[] arr) {
        for (int k = 1; k < arr.length; k++) {
            // assume arr[0:k-1] sorted
            int save = arr[k]; // arr[k] may be overwritten later, so save that value
            int pos = findPos(arr, k, save); // find where to insert save in arr[0:k-1]
            shiftDown(arr, k, pos);
            arr[pos] = save;
            // arr[0:k] is now sorted with k+1 values
            printList(arr, MAX_SIZE, "
After pass " + k);
        } // end for

    } // end sort

    // find the right place for item to be inserted within arr[0:size-1]
    private static int findPos(int[] arr, int size, int item) {
        // write the code
        return 0; // stub, keeps compiler happy
    } // end findPos

    private static void terminate(String message) {
        out.println("Error: " + message);
    } // end terminate
Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution