Answer:
See explaination
Step-by-step explanation:
//SortFile.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class SortFile {
// array list of integers to store the numbers
private ArrayList<Integer> list;
// default constructor
public SortFile() {
// initializing list
list = new ArrayList<Integer>();
// reading file name using a scanner
Scanner sc = new Scanner(System.in);
System.out.print("Enter file name: ");
String filename = sc.nextLine();
// opening file inside a try catch block. if you want to throw exception
// instead, just remove try-catch block and add throws
// FileNotFoundException to the method signature.
try {
// opening file
sc = new Scanner(new File(filename));
// looping and reading each integer from file to list
while (sc.hasNextInt()) {
list.add(sc.nextInt());
}
// closing file
sc.close();
} catch (FileNotFoundException e) {
// exception occurred.
System.err.println(e);
}
}
// constructor taking a file name
public SortFile(String filename) {
// initializing list
list = new ArrayList<Integer>();
// opening file inside a try catch block. if you want to throw exception
// instead, just remove try-catch block and add throws
// FileNotFoundException to the method signature.
try {
Scanner sc = new Scanner(new File(filename));
// looping and reading each integer from file to list
while (sc.hasNextInt()) {
list.add(sc.nextInt());
}
sc.close();
} catch (FileNotFoundException e) {
System.err.println(e);
}
}
// method to return a sorted array of integers containing unique elements
// from list
public int[] sort() {
// creating an array of list.size() size
int arr[] = new int[list.size()];
// count of unique (non duplicate) numbers
int count = 0;
// looping through the list
for (int i = 0; i < list.size(); i++) {
// fetching element
int element = list.get(i);
// if this element does not appear before on list, adding to arr at
// index=count and incrementing count
if (!list.subList(0, i).contains(element)) {
arr[count++] = element;
}
}
// now reducing the size of arr to count, so that there are no blank
// locations
arr = Arrays.copyOf(arr, count);
// using selection sort algorithm to sort the array
int index = 0;
// loops until index is equal to the array length. i.e the whole array
// is sorted
while (index < arr.length) {
// finding the index of biggest element in unsorted array
// initially assuming index as the index of biggest element
int index_max = index;
// looping through the unsorted part of array
for (int i = index + 1; i < arr.length; i++) {
// if current element is bigger than element at index_max,
// updating index_max
if (arr[i] > arr[index_max]) {
index_max = i;
}
}
// now we simply swap elements at index_max and index
int temp = arr[index_max];
arr[index_max] = arr[index];
arr[index] = temp;
// updating index
index++;
// now elements from 0 to index-1 are sorted. this will continue
// until whole array is sorted
}
//returning sorted array
return arr;
}
//code for test-run
public static void main(String[] args) {
//initializing SortFile using default constructor
SortFile sortFile = new SortFile();
//displaying sorted array
System.out.println(Arrays.toString(sortFile.sort()));
}