Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Step 1 . Add a private helper method: / / return a partially filled int array with all elements from the / / ArrayList param

Step 1. Add a private helper method:
// return a partially filled int array with all elements from the
// ArrayList param in the same sequence and 5 extra spots at the end
private static int[] toIntArray(ArrayList list){
// ADD code
}
We want to copy the data from the ArrayList to an array. To allow space for additional operations, we also want this array to be a partially filled array. We accomplish this by creating an array whose capacity is 5+ list.size().
Step 2. Add the following code to the end of main():
int[] numArr = null; // to hold data
int size =0; // track actual # of elements.
numArr = toIntArray(numList);
size = numList.size();
System.out.println("Now in arr: "+ Arrays.toString(numArr));
// need import
This segment of code should print the same data plus 5 zeros at the end:
Now in arr: [15,6,74,28,90,34,0,0,0,0,0]
After this step, all work should be done with arrays. Do not use any ArrayList objects/methods, otherwise zero credit.
Step 3. Add a private helper method to sort this partially filled array following the given method prolog comment. You must write your own sorting code and use selection sort or insertion sort. If needed, review Unit 2_2 lecture on working with a partially filled array.
// sort a partially filled array ([0 ~ numOfElements-1]) into
// ascending order
// Will return and not modify the arr if arr is null or
// numOfElements is invalid
private static void sort(int[] arr, int numOfElements){
// ADD code
}
numOfElements is invalid if its negative, or equal to or larger than arr.length.
Step 4. Add the following code to main() to test the sorting method (continue from step 2 work):
sort(numArr, size);
System.out.println("After sorting: "+ Arrays.toString(numArr));
This segment of code should print the data after sorting. Be aware that the 5 zeros at the end should not be touched by the sort() method.
After sorting: [6,15,28,34,74,90,0,0,0,0,0]
Step 5. Add a private helper method to insert a new item into the partially filled array. Do not add the new item to the end of the filled area and then sort. Insert and maintain the sorted order. Review this weeks lecture if needed.
// insert into a partially filled array ([0~numOfElements-1]) and
// maintain sorted order (ascending)
// This method returns the number of stored elements after the insert.
// If insert failed (such as arr is null, numOfElements is invalid,
// or arr is already full), do not modify arr content and return original
// numOfElements
private static int insert(int[] arr, int numOfElements, int newItem){
// ADD code
}
Step 6. Add the following code to main() to test the insert method (continue from step 4 work):
Scanner stdIn = new Scanner(System.in);
int num;
for (int i=0; i<2; i++){
System.out.print("Enter a number: ");
num = stdIn.nextInt();
size = insert(numArr, size, num);
// if the insert is successful, returned value should be old size
// plus 1;
// otherwise, returned value should match the old size
System.out.printf("After inserting %d: %s
", num, Arrays.toString(numArr));
}
stdIn.close(); // Eclipse requires closing a Scanner object
This segment of code will generate this output with two inputs -5 and 60:
Enter a number: -5
After inserting -5: [-5,6,15,28,34,74,90,0,0,0,0]
Enter a number: 60
After inserting 60: [-5,6,15,28,34,60,74,90,0,0,0]
Now take a screenshot of your final work and include it in your assignment report.

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

Step: 3

blur-text-image

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

Implementing Ai And Machine Learning For Business Optimization

Authors: Robert K Wiley

1st Edition

B0CPQJW72N, 979-8870675855

More Books

Students also viewed these Databases questions