Filip_Markoski

[NP] MinMax

Nov 11th, 2017
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /* when it is inside the <> it means that we are giving it objects which are already comparable  */
  4. class MinMax<T extends Comparable<T>> {
  5.  
  6.     T min;
  7.     T max;
  8.     int countMin;
  9.     int countMax;
  10.     int total;
  11.  
  12.     public MinMax() {
  13.         /* Default Constructor */
  14.         this.countMin = 0;
  15.         this.countMax = 0;
  16.         this.total = 0;
  17.     }
  18.  
  19.     /*
  20. 12
  21. 8
  22. 8
  23. 5
  24. 55
  25. 13
  26. 55*/
  27.  
  28.     public void update(T element) {
  29.         /* Starting scenario*/
  30.         if (total == 0) {
  31.             max = element;
  32.             min = element;
  33.         }
  34.  
  35.         /* Midway scenario */
  36.  
  37.         if (min.compareTo(element) > 0) {
  38.             min = element;
  39.             countMin = 1;
  40.         } else if (min.compareTo(element) == 0) {
  41.             countMin++;
  42.         }
  43.  
  44.         if (max.compareTo(element) < 0) {
  45.             max = element;
  46.             countMax = 1;
  47.         } else if (max.compareTo(element) == 0) {
  48.             countMax++;
  49.         }
  50.  
  51.         /* Update the total */
  52.         total++;
  53.     }
  54.  
  55.     public T max() {
  56.         return max;
  57.     }
  58.  
  59.     public T min() {
  60.         return min;
  61.     }
  62.  
  63.     @Override
  64.     public String toString() {
  65.         return min + " " + max + " " + (total - countMin - countMax) + "\n";
  66.     }
  67. }
  68.  
  69. public class MinAndMax {
  70.     public static void main(String[] args) throws ClassNotFoundException {
  71.         Scanner scanner = new Scanner(System.in);
  72.         int n = scanner.nextInt();
  73.         MinMax<String> strings = new MinMax<String>();
  74.         for (int i = 0; i < n; ++i) {
  75.             String s = scanner.next();
  76.             strings.update(s);
  77.         }
  78.         System.out.println(strings);
  79.         MinMax<Integer> ints = new MinMax<Integer>();
  80.         for (int i = 0; i < n; ++i) {
  81.             int x = scanner.nextInt();
  82.             ints.update(x);
  83.         }
  84.         System.out.println(ints);
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment