Advertisement
joxaren

TaskOne

Jun 1st, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.83 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.Objects;
  6.  
  7. public class TaskOne {
  8.  
  9.     private long[] sequence;
  10.  
  11.     public static void main(String[] args) {
  12.         new TaskOne().start();
  13.     }
  14.  
  15.     private void start() {
  16.         System.out.println("введите имя файла в формате имя.txt. для выхода введите stop.");
  17.         String input = getInput();
  18.         if (checkIfNull(input)) {
  19.             start();
  20.         } else if ((input.equals("stop")) || input.equals("stop.")) {
  21.             System.out.println("до свидания.");
  22.         } else {
  23.             readFromFile(input);
  24.             Arrays.sort(sequence);
  25.             try {
  26.                 System.out.println("90th percentile: " + percentile(sequence, 90));
  27.                 System.out.println("median: " + median(sequence));
  28.                 System.out.println("average: " + average(sequence));
  29.                 System.out.println("max: " + max(sequence));
  30.                 System.out.println("min: " + min(sequence));
  31.                 start();
  32.             } catch (ArrayIndexOutOfBoundsException ex) {
  33.  
  34.             }
  35.         }
  36.     }
  37.  
  38.     private boolean checkIfNull(String input) {
  39.         boolean isEmpty = input == null || input.trim().length() == 0;
  40.         if (isEmpty) {
  41.             return true;
  42.         } else return false;
  43.     }
  44.  
  45.     private void readFromFile(String fileName) {
  46.         List<Integer> inputList = new ArrayList<>();
  47.         try {
  48.             BufferedReader reader = new BufferedReader(new FileReader(fileName));
  49.             String number;
  50.             if (reader.readLine() == null) {
  51.                 System.out.println("файл не должен быть пустым.");
  52.                 start();
  53.             } else {
  54.                 while ((number = reader.readLine()) != null) {
  55.                     inputList.add(Integer.valueOf(number));
  56.                 }
  57.             }
  58.             reader.close();
  59.         } catch (FileNotFoundException ex) {
  60.             System.out.println("нет такого файла.");
  61.             start();
  62.         } catch (IOException ex) {
  63.             ex.printStackTrace();
  64.         }
  65.         sequence = inputList.stream().filter(Objects::nonNull).mapToLong(i -> i).toArray();
  66.     }
  67.  
  68.     private static String getInput() {
  69.         String fileName = null;
  70.         try {
  71.             BufferedReader reader = new BufferedReader(
  72.                     new InputStreamReader(System.in));
  73.             fileName = reader.readLine();
  74.             if (fileName.length() == 0) return null;
  75.         } catch (IOException ex) {
  76.             ex.printStackTrace();
  77.         }
  78.         return fileName;
  79.     }
  80.  
  81.     private static long[] makeRandomArray(int length, int maxNum) {
  82.         long[] array = new long[length];
  83.         for (int i = 0; i < length; i++) {
  84.             array[i] = (long) (Math.random() * (maxNum + 1));
  85.         }
  86.         return array;
  87.     }
  88.  
  89.     private static long average(long[] sequence) {
  90.         long sum = 0;
  91.         for (long number : sequence)
  92.             sum += number;
  93.         return sum / sequence.length;
  94.     }
  95.  
  96.     private static long max(long[] sequence) {
  97.         return sequence[sequence.length - 1];
  98.     }
  99.  
  100.     private static double median(long[] sequence) {
  101.         int middle = sequence.length / 2;
  102.         if (sequence.length % 2 == 1) {
  103.             return sequence[middle];
  104.         } else {
  105.             return ((sequence[middle - 1] / 2) + (sequence[middle]) / 2);
  106.         }
  107.     }
  108.  
  109.     private static long min(long[] sequence) {
  110.         return sequence[0];
  111.     }
  112.  
  113.     private static long percentile(long[] sequence, int rank) {
  114.         double x = (double) rank / 100;
  115.         int index = (int) Math.round((sequence.length) * x);
  116.         return sequence[index - 1];
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement