SHARE
TWEET

Untitled

a guest Nov 19th, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2.  
  3. import sun.rmi.server.InactiveGroupException;
  4.  
  5. import java.awt.*;
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8. import java.util.List;
  9.  
  10. public class Main {
  11.  
  12.     static class AvgCalculator {
  13.  
  14.         public double calculateAvg(List<Integer> grades) {
  15.             List<Integer> copy = new ArrayList<>(grades);
  16.             if(grades.size() > 3) {
  17.                 final Integer max = Collections.max(copy);
  18.                 final Integer min = Collections.min(copy);
  19.  
  20.                 copy.remove(max);
  21.                 copy.remove(min);
  22.             }
  23.  
  24.             return internalCalculateAvg(copy);
  25.         }
  26.  
  27.         private double internalCalculateAvg(List<Integer> grades) {
  28.             double sum = 0;
  29.             for (Integer grade : grades) {
  30.                 sum += grade;
  31.             }
  32.             return sum / grades.size();
  33.         }
  34.  
  35.     }
  36.  
  37.     public static void main(String[] args) {
  38.         List<Integer> grades = new ArrayList<>();
  39.         grades.add(1);
  40.         grades.add(2);
  41.         grades.add(3);
  42.         grades.add(4);
  43.         grades.add(6);
  44.  
  45.         AvgCalculator avgCalculator = new AvgCalculator();
  46.  
  47.         System.out.println("List before method: " + grades);
  48.         final double result = avgCalculator.calculateAvg(grades);
  49.         System.out.println("List after method: " + grades);
  50.         System.out.println("result: " + result);
  51.  
  52.  
  53.     }
  54. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top