Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have this program. Works fine in this condition. It sorts a given ARRAY OF INT

ID: 3641279 • Letter: I

Question

I have this program. Works fine in this condition. It sorts a given ARRAY OF INT, and stores it into grades.txt. But i need a little help with it. How do i change this program so that:

reads an array of unsorted integers from a file and sorts them. (Prompt for and read the file's name from the user.)

Output the list of the sort integers into a file
whose name is given by the user.

Also a sample output'll be much appreciated.....



import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.*;

//////////////////////////////////////////////////////////////
//
//This module contains a method that carries out the
//bubble sort on an array "a" of integers, where "n"
//is the number of data items (integers) currently in
//the array.
//
//Assumption: n > 1
//
//
//
//////////////////////////////////////////////////////////////

public class Arrayutil {

public static void sort(int[] a, int n)

{
boolean exchange_made;
int num_items_to_visit;
int last_marker;
int i;
int temp;

// if the number of items is 0 or 1, then there's
// nothing to do; otherwise, do the sorting

if (n > 1) {
// set the number of values (elmts) to visit on the first pass.
// Note that it is actually one less than the total #
// of data values since the comparison of the next-to-last
// and last values occurs while visiting the next-to-last

num_items_to_visit = n - 1;

do {

// no exchange has occurred yet on the current pass.
// (We haven't scanned any values yet!)
// Indicate this by lowering the "exchange_made" flag

exchange_made = false;

// the index of the last item to visit on this pass is
// one less than the # to visit

last_marker = num_items_to_visit - 1;

// scan the values

for (i = 0; i <= last_marker; i++)

if (a[i] > a[i + 1]) {
// swap them

temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;

// indicate that a swap has been made

exchange_made = true;

}

// number of items to visit is now one less

num_items_to_visit--;

} while (exchange_made && (num_items_to_visit > 0));

}

} // end sort
/**
* Writes the sorted array into the grade.txt file
* @param a
* @param outputFileName
*/
public static void writeData(int[] a,String outputFileName) {
try {
// Create file
FileWriter fstream = new FileWriter(outputFileName);
BufferedWriter out = new BufferedWriter(fstream);
int k = 0;
while(kout.write(String.valueOf(a[k]));
out.newLine();
k++;
}
// Close the output stream
out.close();
} catch (Exception e) {// Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
public static void main(String args[]) {
int[] grade = new int[10];
grade[0] = 12;
grade[1] = 89;
grade[2] = 65;
grade[3] = 74;
grade[4] = 34;
grade[5] = 28;
grade[6] = 96;
grade[7] = 21;
grade[8] = 44;
grade[9] = 52;
int i=0,j=0,sum = 0;
System.out.println("Intial array is");
while(iSystem.out.println(grade[i]);
i++;
}
Arrayutil.sort(grade, 10);
System.out.println("Sorted array is");
while(jint temp = grade[j];
System.out.println(temp);
sum= sum+temp;
j++;
}
String outputFileName = "grade.txt";
Arrayutil.writeData(grade,outputFileName);
int minGrade = grade[0];
int maxGrade = grade[grade.length-1];
int avgGrade = sum/10;
System.out.println("high grade is "+maxGrade);
System.out.println("low grade is "+minGrade);
System.out.println("spread is "+(maxGrade-minGrade));
System.out.println("median grade is "+avgGrade);
}
} // end Arrayutil

Explanation / Answer

Here is the code ////////////////////////////////////////////////////////////// // //This module contains a method that carries out the //bubble sort on an array "a" of integers, where "n" //is the number of data items (integers) currently in //the array. // //Assumption: n > 1 // // // ////////////////////////////////////////////////////////////// public class Arrayutil { public static void sort(int[] a, int n) { boolean exchange_made; int num_items_to_visit; int last_marker; int i; int temp; // if the number of items is 0 or 1, then there's // nothing to do; otherwise, do the sorting if (n > 1) { // set the number of values (elmts) to visit on the first pass. // Note that it is actually one less than the total # // of data values since the comparison of the next-to-last // and last values occurs while visiting the next-to-last num_items_to_visit = n - 1; do { // no exchange has occurred yet on the current pass. // (We haven't scanned any values yet!) // Indicate this by lowering the "exchange_made" flag exchange_made = false; // the index of the last item to visit on this pass is // one less than the # to visit last_marker = num_items_to_visit - 1; // scan the values for (i = 0; i a[i + 1]) { // swap them temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; // indicate that a swap has been made exchange_made = true; } } // number of items to visit is now one less num_items_to_visit--; } while (exchange_made && (num_items_to_visit > 0)); } } // end sort /** * Writes the sorted array into the grade.txt file * * @param a * @param outputFileName */ public static void writeData(int[] a, String outputFileName) { try { // Create file FileWriter fstream = new FileWriter(outputFileName); BufferedWriter out = new BufferedWriter(fstream); for (int k = 0; k