Reference no: EM13164836
Write a program that reads a file name from the keyboard. The file contains integers, each on a separate line. The first line of the input file will contain the number of integers in the file. You then create a corresponding array and fill the array with integers from the remaining lines. If the input file does not exist, give an appropriate error message and terminate the program. After integers are stored in an array, your main program should call the following methods in order, output the intermediate results on screen, and at end output even integers and odd integers to two different files called even.out and odd.out.
Implement the following methods in the program:
public static int[] inputData() - This method will ask user for a file name, create an array, and store the integers read from the file into the array. If input file does not exist, give an appropriate error message and terminate the program.
public static void printArray(int[] array, int counter) - This method will display the content of the array on screen. The second parameter counter is to make sure that you only process the array elements that contain valid data items. Print 10 integers per line and use printf method to align columns of numbers.
public static int outOfOrder(int[] array) - This method will test the array for being out of order. The method returns -1 if the elements are not out of order; otherwise, it returns the index of the first element of the array that is out of order. For example, consider a = {1, 2, 3, 2, 4, 7, 5, 8, 9, 1}, a[2] and a[3] are the first pair out of order, and a[3] is the first element out of order, so the method returns 3. If the array were sorted, the method would return -1.
public static void reverseArray(int[] array) - This method will reverse the elements of the array so that the 1st element becomes the last, the 2nd element becomes the 2nd to the last, and so on.
public static int max(int[] array) - This method should find and return the largest value in the array.
public static int min(int[] array) - This method should find and return the smallest value in the array.
Public static void selectionSort (int[] array) - This method should implement Selection Sort and sort the array of integers into descending order.
Public static int deleteRepeats (int[] array) - This method should delete all repeated integers from the sorted array. When an integer is deleted, the remaining integers are moved one position to fill in the gap. This will create empty positions at the end of the array, so the method should return the value that represents the actual number of array positions used.
For the array
4 4 3 3 3 2 2 1 1 1 1 -1 -12 -12 -12 -12
After this method is executed, the array will be
4 3 2 1 -1 -12
public static void outputData(int[] array, int counter) - This method will create two output files called even.out and odd.out. Scan through valid data items in the array, if an element is even, print it to even.out. If it is odd, print the element to odd.out.