Question: How do I modify my code below with the following steps. Step 0 . First set up the given code in your new program and

How do I modify my code below with the following steps.

Step 0. First set up the given code in your new program and make sure it works properly. Take a screenshot of its execution and include it in your assignment report.

Step 1. Add a private helper method:

// return a partially filled int array with all elements from 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]

Step 3. Add a private helper method to sort this partially filled array following the given method prolog comment. You must come up with a new sorting code and use selection sort or insertion sort.

// 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 it's negative 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.

// insert into a partially filled array ([0~numOfElements-1]) and

// maintain sorted order (ascending)

// This method returns number of stored elements at the end.

// If insert failed (such as arr is null, numOfElements is invalid,

// or arr is already full), do not modify arr content and return

// 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);

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]

My Code:

// classes used for file i/o and i/o exception import java.io.IOException; import java.nio.file.Path;

import java.util.Scanner; // I/O methods

import java.util.ArrayList;

public class AO12TestNumFile {

// call method to load data from file and prepare for processing public static void main(String[] args) { ArrayList numList = null; // declare an ArrayList var and initialize to null

// relative path and file name of data file String fileName = "data/records.txt"; // This "records.txt" file should already exist in a folder named "data". // If using an IDE and source code file is put in a default "src" folder (or similar), // folder "data" should be at the same location as the "src" folder; // otherwise, "data" folder should be at the same location as your .java file

numList = loadFromFile(fileName); // load data

System.out.println("Records: " + numList); // ... more processing ...

} // end main

// read data from a specified file // data will be saved in ArrayList and returned private static ArrayList loadFromFile(String fileName) { Scanner fileIn = null; // scanner object to connect to file ArrayList list = new ArrayList<>(); // to store data from file

try { // try-catch-finally used for any exception during file open/read/close // open input file fileIn = new Scanner( Path.of(fileName) );

// loop through multiple records while (fileIn.hasNextInt()) { // still have numbers to be read? // 1. read one record (here each record is just one num) int num = fileIn.nextInt();

// 2. add the record to ArrayList obj list.add(num);

// end one record }// end while: reading all records

} catch (IOException ioe) { System.out.println("Error reading \"" + fileName + "\" file: " + ioe); } finally { // close file if ( fileIn != null) { // close if was connected to a file fileIn.close(); } } // end file input

return list; }// end loadFromFile

} // end class TestNumFile

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!