/* MeanFraction.java ** ** Author: P. M. J. ** Course: CMPS 134 ** Semester: Fall 2018 ** ** Last Modified: November 5, 2018 ** Collaboration: R. W. M. ** ** Java application that reads a text file containing a sequence of fractions, ** one per line, determines the mean of these values and prints that result. ** */ import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class MeanFraction { public static void main(String[] args) throws FileNotFoundException { // Use the first run-value argument as the name of the file to be read String fileID = args[0]; Scanner input = new Scanner(new File(fileID)); Fraction total = new Fraction(0,1); int count = 0; // Loop to read and process each value value while(input.hasNextLine()) { count = count + 1; // Read the next line from the input as a string String line = input.nextLine().trim(); // Construct a new object to represent this value Fraction value = new Fraction(line); // Add to the total and print the running value total.addTo(value); System.out.println(count + ") \"" + line + "\" aka " + value.toString() + " accumulates to " + total.toString()); } // Determine and print the result System.out.print("The mean value is "); if(count > 0) { Fraction countAsFraction = new Fraction(count,1); total.divideBy(countAsFraction); System.out.println(total); } else { System.out.println("unknown!"); } System.out.println(); } }