Write a JAVA program that reads a file name from the keyboard. The file contains
ID: 3910535 • Letter: W
Question
Write a JAVA 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.
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks.
// PlayWithNumbers.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class PlayWithNumbers {
/**
* 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 int[] inputData() {
int[] array = null;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter input file name: ");
String filename = scanner.nextLine();
try {
Scanner fileScanner = new Scanner(new File(filename));
while (fileScanner.hasNext()) {
int n = Integer.parseInt(fileScanner.nextLine());
array = new int[n];
int index = 0;
while (fileScanner.hasNext()) {
int number = Integer.parseInt(fileScanner.nextLine());
array[index] = number;
index++;
}
}
} catch (FileNotFoundException e) {
System.out.println(e);
System.exit(1);
} catch (Exception e) {
System.out.println("Corrupted file");
System.exit(1);
}
return 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 printArray(int[] array) {
System.out.println("Array: ");
int counter = 0;
for (int i = 0; i < array.length; i++) {
counter++;
System.out.printf("%4d ", array[i]);
if (counter == 10) {
counter = 0;
System.out.println();
}
}
System.out.println();
}
/**
* This method will reverse the elements of the array
*/
public static void reverseArray(int[] array) {
int temp[] = new int[array.length];
int index = 0;
for (int i = array.length - 1; i >= 0; i--) {
temp[index] = array[i];
index++;
}
for (int i = 0; i < array.length; i++) {
array[i] = temp[i];
}
}
/**
* his method should compute and return the sum of all elements in the
* array.
*/
public static int sum(int[] array) {
int total = 0;
for (int i = 0; i < array.length; i++) {
total += array[i];
}
return total;
}
/**
* This method should compute and return the average of all elements in the
* array.
*/
public static double average(int[] array) {
int total = sum(array);
double avg = (double) total / array.length;
return avg;
}
/**
* This method should find and return the largest value in the array.
*/
public static int max(int[] array) {
int max = 0;
for (int i = 0; i < array.length; i++) {
if (i == 0) {
// first entry
max = array[i];
} else if (array[i] > max) {
max = array[i];
}
}
return max;
}
/**
* this method should find and return the smallest value in the array
*/
public static int min(int[] array) {
int min = 0;
for (int i = 0; i < array.length; i++) {
if (i == 0) {
// first entry
min = array[i];
} else if (array[i] < min) {
min = array[i];
}
}
return min;
}
/**
* This method will use Selection Sort to sort (in ascending order) the
* elements of the array
*/
public static void ascendingSelectionSortArray(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
int index = i;
/**
* finding index of smallest element in the unsorted part
*/
for (int j = i + 1; j < array.length; j++)
if (array[j] < array[index])
index = j;
/**
* Swapping elements
*/
int temp = array[index];
array[index] = array[i];
array[i] = temp;
}
}
/**
* This method will Bubble Sort to sort (in descending order) the elements
* of the array
*/
public static void desendingBubbleSortArray(int[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length - 1; j++) {
if (array[j] < array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
/**
* 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.
*/
public static void outputData(int[] array) {
try {
PrintWriter oddWriter = new PrintWriter(new File("odd.out"));
PrintWriter evenWriter = new PrintWriter(new File("even.out"));
for (int i = 0; i < array.length; i++) {
if (array[i] % 2 == 0) {
evenWriter.println(array[i]);
} else {
oddWriter.println(array[i]);
}
}
oddWriter.close();
evenWriter.close();
System.out
.println("Even,Odd elements are written to respective files!");
} catch (IOException e) {
System.out.println(e);
}
}
public static void main(String[] args) {
/**
* performing required operations in proper order as mentioned
*/
int[] array = inputData();
printArray(array);
System.out.println("Reversing array");
reverseArray(array);
printArray(array);
int sum = sum(array);
System.out.println("Sum: " + sum);
double avg = average(array);
System.out.println("Average: " + avg);
int max = max(array);
System.out.println("Largest: " + max);
int min = min(array);
System.out.println("Smallest: " + min);
ascendingSelectionSortArray(array);
System.out.println("Sorted in ascending order:");
printArray(array);
desendingBubbleSortArray(array);
System.out.println("Sorted in descending order:");
printArray(array);
outputData(array);
}
}
/*OUTPUT*/
Enter input file name: numbers.txt
Array:
11 27 32 44 53 65 74 82 93 10
Reversing array
Array:
10 93 82 74 65 53 44 32 27 11
Sum: 491
Average: 49.1
Largest: 93
Smallest: 10
Sorted in ascending order:
Array:
10 11 27 32 44 53 65 74 82 93
Sorted in descending order:
Array:
93 82 74 65 53 44 32 27 11 10
Even,Odd elements are written to respective files!
//numbers.txt
10
11
27
32
44
53
65
74
82
93
10
//odd.out
93
65
53
27
11
//even.out
82
74
44
32
10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.