Need help creating this java program Write a program that reads a file name from
ID: 3904614 • Letter: N
Question
Need help creating this java program
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 program should call the following methods in order, output the intermediate results and count statistics 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) – This method will display the content of the array on screen. Print 10 integers per line and use printf method to align columns of numbers.
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 sum(int[] array) – This method should compute and return the sum of all elements in the array.
* public static double average(int[] array) – This method should compute and return the average of all elements in the array.
* 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 ascendingSelectionSortArray(int[] array) – This method will use Selection Sort to sort (in ascending order) the elements of the array so that the 1st element becomes the smallest, the 2nd element becomes the 2nd smallest, and so on.
* public static void desendingBubbleSortArray(int[] array) – This method will Bubble Sort to sort (in descending order) the elements of the array so that the 1st element becomes the largest, the 2nd element becomes the 2nd largest, and so on.
* public static void outputData(int[] array) – This method will create two output files called even.out and odd.out. Scan through the entire array, if an element is even, print it to even.out. If it is odd, print the element to odd.out.
Input file: in16.dat
16
-12
13
-2
4
11
15
-22
8
-7
1
21
7
4
2
6
-14
The following are sample interactions that occur when running the program:
$ java ArrayProcessing
enter input filename: in16.dat
original array:
-12 13 -2 5 11 15 -28 18 -7 1
21 7 4 2 6 -14
reversed array:
-14 6 2 4 7 21 1 -7 18 -28
15 11 5 -2 13 -12
The sum of all elements: 40
The average of all elements: 2.5
Max: 21
Min: -28
array in ascending order(by Selection Sort):
-28 -14 -12 -7 -2 1 2 4 5 6
7 11 13 15 18 21
array in descending order(by Bubble Sort):
21 18 15 13 11 7 6 5 4 2
1 -2 -7 -12 -14 -28
Create two output files:
Done, please check files 'even.out' and 'odd.out'.
Output files:
even.out
18
6
4
2
-2
-12
-14
-28
odd.out
21
15
13
11
7
5
1
-7
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
NOTE: Please make sure you place your input file in the correct folder. If using eclipse, the file should be in the project directly and NOT INSIDE src folder.
Also after running the program, refresh the project to see the output files.
input file: in16.dat
==============
16
-12
13
-2
4
11
15
-28
18
-7
1
21
7
4
2
6
-14
NumberStats.java
============
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class NumberStats {
public static void main(String[] args) {
try {
int[] array = inputData();
System.out.print("Original Array: ");
printArray(array);
reverseArray(array);
System.out.print("Reversed Array: ");
printArray(array);
System.out.println("The sum of all elements: " + sum(array));
System.out.printf("The average of all elements: %.1f ", average(array));
System.out.println("Max: " + max(array));
System.out.println("Min: " + min(array));
ascendingSelectionSortArray(array);
System.out.println("Array in ascending order(by Selection Sort): ");
printArray(array);
descendingBubbleSortArray(array);
System.out.println("Array in descending order(by Bubble Sort): ");
printArray(array);
outputData(array);
} catch (FileNotFoundException e) {
e.getMessage();
}
}
public static int[] inputData() throws FileNotFoundException
{
String filename;
Scanner keybaord = new Scanner(System.in);
System.out.print("Enter input filename: ");
filename = keybaord.nextLine();
int[] array = null;
Scanner infile = new Scanner(new File(filename));
int size = infile.nextInt();
array = new int[size];
for(int i = 0; i < size; i++)
array[i] = infile.nextInt();
infile.close();
return array;
}
public static void printArray(int[] array)
{
for(int i = 0; i < array.length; i++)
{
if(i % 10 == 0) // print newline after 10 numbers
System.out.println();
System.out.print(array[i] + " ");
}
System.out.println();
}
public static void reverseArray(int[] array)
{
int i = 0, j = array.length - 1;
while(i < j)
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
}
public static int sum(int[] array)
{
int s = 0;
for(int i = 0; i < array.length; i++)
{
s = s + array[i];
}
return s;
}
public static double average(int[] array)
{
double s = sum(array);
return s / array.length;
}
public static int max(int[] array)
{
int maximum = array[0];
for(int i = 1; i < array.length; i++)
{
if(array[i] > maximum)
maximum = array[i];
}
return maximum;
}
public static int min(int[] array)
{
int minimum = array[0];
for(int i = 1; i < array.length; i++)
{
if(array[i] < minimum)
minimum = array[i];
}
return minimum;
}
public static void ascendingSelectionSortArray(int[] array)
{
for(int i = 0; i < array.length; i++)
{
int minIdx = i;
for(int j = i+1; j < array.length; j++)
{
if(array[j] < array[minIdx])
minIdx = j;
}
if(minIdx != i)
{
int temp = array[i];
array[i] = array[minIdx];
array[minIdx] = temp;
}
}
}
public static void descendingBubbleSortArray(int[] array)
{
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array.length - i - 1; j++)
{
if(array[j] < array[j+1])
{
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
public static void outputData(int[] array) throws FileNotFoundException
{
PrintWriter odd = new PrintWriter("odd.out");
PrintWriter even = new PrintWriter("even.out");
System.out.println("Create two output files:");
for(int i = 0; i < array.length; i++)
{
if(array[i] % 2 == 0) //even number
even.println(array[i]);
else
odd.println(array[i]);
}
odd.close();
even.close();
System.out.println("Done, please check files 'even.out' and 'odd.out'.");
}
}
output
====
Enter input filename: in16.dat
Original Array:
-12 13 -2 4 11 15 -28 18 -7 1
21 7 4 2 6 -14
Reversed Array:
-14 6 2 4 7 21 1 -7 18 -28
15 11 4 -2 13 -12
The sum of all elements: 39
The average of all elements: 2.4
Max: 21
Min: -28
Array in ascending order(by Selection Sort):
-28 -14 -12 -7 -2 1 2 4 4 6
7 11 13 15 18 21
Array in descending order(by Bubble Sort):
21 18 15 13 11 7 6 4 4 2
1 -2 -7 -12 -14 -28
Create two output files:
Done, please check files 'even.out' and 'odd.out'.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.