Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Objects;
- public class TaskOne {
- private long[] sequence;
- public static void main(String[] args) {
- new TaskOne().start();
- }
- private void start() {
- System.out.println("введите имя файла в формате имя.txt. для выхода введите stop.");
- String input = getInput();
- if (checkIfNull(input)) {
- start();
- } else if ((input.equals("stop")) || input.equals("stop.")) {
- System.out.println("до свидания.");
- } else {
- readFromFile(input);
- Arrays.sort(sequence);
- try {
- System.out.println("90th percentile: " + percentile(sequence, 90));
- System.out.println("median: " + median(sequence));
- System.out.println("average: " + average(sequence));
- System.out.println("max: " + max(sequence));
- System.out.println("min: " + min(sequence));
- start();
- } catch (ArrayIndexOutOfBoundsException ex) {
- }
- }
- }
- private boolean checkIfNull(String input) {
- boolean isEmpty = input == null || input.trim().length() == 0;
- if (isEmpty) {
- return true;
- } else return false;
- }
- private void readFromFile(String fileName) {
- List<Integer> inputList = new ArrayList<>();
- try {
- BufferedReader reader = new BufferedReader(new FileReader(fileName));
- String number;
- if (reader.readLine() == null) {
- System.out.println("файл не должен быть пустым.");
- start();
- } else {
- while ((number = reader.readLine()) != null) {
- inputList.add(Integer.valueOf(number));
- }
- }
- reader.close();
- } catch (FileNotFoundException ex) {
- System.out.println("нет такого файла.");
- start();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- sequence = inputList.stream().filter(Objects::nonNull).mapToLong(i -> i).toArray();
- }
- private static String getInput() {
- String fileName = null;
- try {
- BufferedReader reader = new BufferedReader(
- new InputStreamReader(System.in));
- fileName = reader.readLine();
- if (fileName.length() == 0) return null;
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- return fileName;
- }
- private static long[] makeRandomArray(int length, int maxNum) {
- long[] array = new long[length];
- for (int i = 0; i < length; i++) {
- array[i] = (long) (Math.random() * (maxNum + 1));
- }
- return array;
- }
- private static long average(long[] sequence) {
- long sum = 0;
- for (long number : sequence)
- sum += number;
- return sum / sequence.length;
- }
- private static long max(long[] sequence) {
- return sequence[sequence.length - 1];
- }
- private static double median(long[] sequence) {
- int middle = sequence.length / 2;
- if (sequence.length % 2 == 1) {
- return sequence[middle];
- } else {
- return ((sequence[middle - 1] / 2) + (sequence[middle]) / 2);
- }
- }
- private static long min(long[] sequence) {
- return sequence[0];
- }
- private static long percentile(long[] sequence, int rank) {
- double x = (double) rank / 100;
- int index = (int) Math.round((sequence.length) * x);
- return sequence[index - 1];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement