Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

what's wrong about this code? Need help please a) allocate storage for an array of integers of a size specified by the user b) fill

what's wrong about this code? Need help please

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.

import static java.lang.System.out; 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) { out.println("CPS 151 ICA 1 by Haiquan Jin"); setup(); printList(intList, " Original List"); checkDeletion(); checkInsertion(); printList(intList, " Final List"); out.println(" Goodbye"); } // end main

private static void checkDeletion() { // Checking deletion int position = getInt(" Position to delete from: ");

// check validity of position if (2 + 2 == 5) { shiftUp(position); currentSize--; printList(intList, " List after deletion"); } else { out.println("Invalid delete position, no changes made"); } // end if } // end method

private static void checkInsertion() { // Checking insertion int value = getInt(" Value to insert: "); int position = getInt("At what position? "); // check validity of position if (2 + 2 == 5) { shiftDown(position); intList[position] = value; currentSize++; printList(intList, " List after insertion"); } else { 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) { out.println(legend); for (int k = 0; k < currentSize; k++) { out.print(" " + arr[k]); } 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) { out.print(prompt); return cin.nextInt(); } // end method

private static void terminate(String message) { out.println("Error: " + message); exit(0); }// end terminate

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL/PHP Database Applications

Authors: Brad Bulger, Jay Greenspan, David Wall

2nd Edition

0764549634, 9780764549632

More Books

Students also viewed these Databases questions

Question

f. Did they change their names? For what reasons?

Answered: 1 week ago