import java.io.*; import java.util.ArrayList; /** * Created by flo on 9/1/15. */ public class AssignScore { // Private data members private ArrayList numGrade; private ArrayList letterGrade; private Double mean; public AssignScore() { // initializing Data Members this.numGrade = new ArrayList(); this.letterGrade = new ArrayList(); this.mean = 0.00; } // reading from a file via redirection java Project1Driver < data.txt public void readFile() { String line; String[] scores = new String[0]; BufferedReader br = null; br = new BufferedReader(new InputStreamReader(System.in)); // read each line from file and split it based on a space try { while ((line = br.readLine()) != null) { scores = line.split(" "); // "\\s+" many spaces } } catch (IOException err) { System.out.println(err.getMessage()); } finally { if (br != null) { try { br.close(); } catch (IOException err) { System.out.println("Lets exit this program, this is not suppose to happen"); System.exit(1); } } } // add each scores to an arraylist for (String score : scores) { try { numGrade.add(Integer.parseInt(score)); } catch (NumberFormatException err) { System.out.println(err.getMessage()); } } } // Compute the mean of the arraylist public void computeMeanScore() { int sum = 0; for (Integer x : numGrade) { sum += x; } this.mean = (double) (sum / numGrade.size()); } // Assign letter grade to each of the scores from data input file public void assignLetterGrade() { // find out the 10 percent difference then add or sub it from ograde and ugrade double diff = this.mean * .10; double oGrade = this.mean + diff; double uGrade = this.mean - diff; for (int grade = 0; grade < numGrade.size(); grade++) { if (numGrade.get(grade) > oGrade) { letterGrade.add("O"); } else if (numGrade.get(grade) <= oGrade && numGrade.get(grade) >= uGrade) { letterGrade.add("S"); } else if (numGrade.get(grade) < uGrade) { letterGrade.add("U"); } } } // write the data or information stored on Arraylist to a file called output.txt public void writeFile() { OutputStreamWriter osWriter = null; OutputStream outStream = null; try { osWriter = new OutputStreamWriter(new FileOutputStream("output.txt")); } catch (FileNotFoundException err) { System.out.println("File does not exist" + err.getMessage()); } for (int x = 0; x < numGrade.size(); x++) { try { osWriter.write(numGrade.get(x) + "," + letterGrade.get(x)); } catch (IOException err) { System.out.println(err.getMessage()); } try { osWriter.write("\n"); } catch (IOException err) { System.out.println(err.getMessage()); } } try { osWriter.close(); } catch (IOException err) { System.out.println(err.getMessage()); } } }