Aleks_Tor

Min Max Sum Average [first one]

Apr 27th, 2021
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.39 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.     public static void main(String[] args) throws Exception {
  6.        
  7.         List<Double> integerList = getIntegerList();
  8.         System.out.println("min="+getMinimum(integerList));
  9.         System.out.println("max="+getMaximum(integerList));
  10.         System.out.println("sum="+getSum(integerList));
  11.         System.out.println("avg="+String.format("%.2f",getAvg(integerList)));
  12.     }
  13.  
  14.     public static double getMinimum(List<Double> array){
  15.         return Collections.min(array);
  16.     }
  17.  
  18.     public static double getMaximum(List<Double> array){
  19.         return Collections.max(array);
  20.     }
  21.  
  22.     public static double getSum(List<Double> array){
  23.         return array.stream().mapToInt(Double::intValue).sum();
  24.     }
  25.  
  26.     public static double getAvg(List<Double> array){
  27.         double sum = 0;
  28.         if (!array.isEmpty()){
  29.             for (Double arr : array){
  30.                 sum+=arr;
  31.             }
  32.             return sum/array.size();
  33.         }
  34.         return sum;
  35.     }
  36.  
  37.     public static List<Double> getIntegerList() throws IOException{
  38.         Scanner scanner = new Scanner(System.in);
  39.         double N = scanner.nextDouble();
  40.  
  41.         List<Double> newList = new ArrayList<>();
  42.         for (int i=0; i<N; i++){
  43.             newList.add(scanner.nextDouble());
  44.         }
  45.         return newList;
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment