import java.io.*;
/**
 * provide information about lists of numbers in a file
 */
public class NumberInfo {
  
  /**
   * Read a sequence of doubles from file f (one per line), and return
   * the one with a fractional part that is closest to zero.
   * Precondition: f != null and the file is not empty
   * @param f Name of file containing doubles.
   * @return Double value whose fractional part is closest to zero.
   */
  public static double getClosestFraction(String f) throws IOException {
    BufferedReader br= new BufferedReader(new FileReader(f));
    double currentDouble;
    double smallestFractionalPartDouble= 
      Double.parseDouble(br.readLine());
    for (String input= br.readLine(); input != null; input= br.readLine()) {
      currentDouble= Double.parseDouble(input);
      if (Math.abs(currentDouble - (int) currentDouble) < 
          Math.abs(smallestFractionalPartDouble - 
                   (int) smallestFractionalPartDouble)) {
        smallestFractionalPartDouble= currentDouble;
      }
    }
    br.close(); // tidy up
    return smallestFractionalPartDouble;
  }
  
  /**
   * Read a sequence of doubles from a file f (one per line), and return
   * the average of the largest and smallest entries.
   * Precondition: f != null and the file is not empty
   * @param f Name of file containing doubles
   * @return average of the largest and smallest entries
   */
  public static double midPoint(String f) throws IOException{
    double largest, smallest;
    BufferedReader br= new BufferedReader (new FileReader(f));
    String input= br.readLine();
    largest= Double.parseDouble(input);
    smallest= largest; // now no need to initialize for loop
    for (; input != null; input= br.readLine()) {
      if (Double.parseDouble(input) > largest) {
        largest= Double.parseDouble(input);
      }
      if (Double.parseDouble(input ) < smallest) {
        smallest= Double.parseDouble(input);
      }
    }
    br.close(); // tidy up
    return (largest + smallest) / 2;
  }
  
  /**
   * Read a sequence of ints from file f, and return the sum of the
   * numbers that are multiples of 3.
   * precondition: f != null
   * @param f Name of file containing integers
   * @return Sum of the multiples of 3 in f
   */
  public static int getThreeSum(String f) throws IOException {
    int threeSum= 0;
    BufferedReader br= new BufferedReader(new FileReader(f));
    for (String input= br.readLine(); input != null; input= br.readLine()) {
      if (Integer.parseInt(input) % 3 == 0) {
        threeSum += Integer.parseInt(input);
      }
    }
    br.close(); // tidy up
    return threeSum;
  }
  
  /**
   * Read a sequence of ints from file f, and return the sum of the
   * numbers that are multiples of 3, minus the sum of those
   * that are not.
   * Precondition: f != null
   * @param f Name of file containing integers
   * @return Difference of multiples of 3 minus non-multiples of 3
   */
  public static int getThreeSumDifference(String f) throws IOException{
    int threeSum= 0, nonThreeMultSum= 0;
    BufferedReader br= new BufferedReader(new FileReader(f));
    for (String input= br.readLine(); input != null; input= br.readLine()) {
      if (Integer.parseInt(input) % 3 == 0) {
        threeSum += Integer.parseInt(input);;
      }
      else {
        nonThreeMultSum += Integer.parseInt(input);
      }
    }
    br.close(); // tidy up
    return threeSum - nonThreeMultSum;
  }  
  
  /**
   * Read a sequence of ints from file f, and return whether
   * numbers that are multiples of 3 occur more than half
   * as often as those that are not.
   * Precondition: f != null
   * @param f Name of file containing integers
   * @return Whether multiples of 3 occur more than half as often 
   * as non-multiples of 3
   */
  public static boolean hasManyThreeProducts(String f) throws IOException {
    int threeMultCount= 0, nonThreeMultCount= 0;
    BufferedReader br= new BufferedReader(new FileReader(f));
    for (String input= br.readLine(); input != null; input= br.readLine()) {
      if (Integer.parseInt(input) % 3 == 0) {
        threeMultCount++;
      }
      else {
        nonThreeMultCount++;
      }
    }
    br.close(); // tidy up
    return 2 * threeMultCount > nonThreeMultCount;
  }
  
  public static void main (String[] args) throws IOException{
    System.out.println(hasManyThreeProducts("threeStuff"));
  }
}
