Answered step by step
Verified Expert Solution
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 Add a private helper method:
return a partially filled int array with all elements from the
ArrayList param in the same sequence and extra spots at the end
private static int toIntArrayArrayList 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 list.size
Step Add the following code to the end of main:
int numArr null; to hold data
int size ; track actual # of elements.
numArr toIntArraynumList;
size numList.size;
System.out.printlnNow in arr: Arrays.toStringnumArr;
need import
This segment of code should print the same data plus zeros at the end:
Now in arr:
After this step, all work should be done with arrays. Do not use any ArrayList objectsmethods otherwise zero credit.
Step 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 lecture on working with a partially filled array.
sort a partially filled array ~ numOfElements into
ascending order
Will return and not modify the arr if arr is null or
numOfElements is invalid
private static void sortint arr, int numOfElements
ADD code
numOfElements is invalid if its negative, or equal to or larger than arr.length.
Step Add the following code to main to test the sorting method continue from step work:
sortnumArr size;
System.out.printlnAfter sorting: Arrays.toStringnumArr;
This segment of code should print the data after sorting. Be aware that the zeros at the end should not be touched by the sort method.
After sorting:
Step 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 ~numOfElements 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 insertint arr, int numOfElements, int newItem
ADD code
Step Add the following code to main to test the insert method continue from step work:
Scanner stdIn new ScannerSystemin;
int num;
for int i; i; i
System.out.printEnter a number: ;
num stdIn.nextInt;
size insertnumArr size, num;
if the insert is successful, returned value should be old size
plus ;
otherwise, returned value should match the old size
System.out.printfAfter inserting d: s
num, Arrays.toStringnumArr;
stdIn.close; Eclipse requires closing a Scanner object
This segment of code will generate this output with two inputs and :
Enter a number:
After inserting :
Enter a number:
After inserting :
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started