I have the program here. Just fix it so that it asks the user to enter the array
ID: 3641076 • Letter: I
Question
I have the program here. Just fix it so that it asks the user to enter the array instead of typing it in the program, and also asks the user which file to save the array into instead of just saving it into grade.txt...A sample output would be apreciated....Thank You!!!
package string;
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(k<a.length){
out.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(i<grade.length-1){
System.out.println(grade[i]);
i++;
}
Arrayutil.sort(grade, 10);
System.out.println("Sorted array is");
while(j<grade.length-1){
int 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