223k views
2 votes
Create the class named SortFile. The class will have a single attribute, which is an ArrayList of integers. The class will have the following methods: 1. SortFile() : the null constructor will ask the user for a file name. It will then open that file, reading each and every integer value in the file. Each integer will be placed into the ArrayList attribute. Note that the file may contain multiple integers on each line, and the file may contain many lines. 2. SortFile(String file) : this constructor will receive a file name as a parameter. It will operate in the same fashion as the null constructor described above. 3. int[ ] sort() : the sort() function will take the values in the ArrayList attribute and return a sorted array containing all of the values, from highest to lowest (descending order). Duplicate values will not be included.Use the Selection sort algorithm to perform this effort. Note that the order of items in the ArrayList attribute will be UNCHANGED during the execution of this method.

1 Answer

0 votes

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

}

User Eranki
by
3.8k points