Advertisement
Dimitrija

Генеричка тројка

Feb 13th, 2022
834
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Comparator;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. import java.util.stream.Collectors;
  6.  
  7. class Triple<T extends Number>{
  8.  
  9.     private List<T> threeList;
  10.  
  11.     public Triple(T a, T b, T c) {
  12.         threeList = Arrays.asList(a,b,c);
  13.     }
  14.  
  15.     public double max(){
  16.         return threeList.stream().max(Comparator.comparingDouble(Number::doubleValue)).orElseGet(null).doubleValue();
  17.     }
  18.     public double avarage(){
  19.         return threeList.stream().mapToDouble(Number::doubleValue).average().orElseGet(null);
  20.     }
  21.     public void sort(){
  22.         threeList = threeList.stream().sorted().collect(Collectors.toList());
  23.     }
  24.  
  25.     @Override
  26.     public String toString() {
  27.         return String.format("%.2f %.2f %.2f",threeList.get(0).doubleValue(),threeList.get(1).doubleValue(),threeList.get(2).doubleValue());
  28.     }
  29. }
  30.  
  31.  
  32.  
  33. public class TripleTest {
  34.     public static void main(String[] args) {
  35.         Scanner scanner = new Scanner(System.in);
  36.         int a = scanner.nextInt();
  37.         int b = scanner.nextInt();
  38.         int c = scanner.nextInt();
  39.         Triple<Integer> tInt = new Triple<Integer>(a, b, c);
  40.         System.out.printf("%.2f\n", tInt.max());
  41.         System.out.printf("%.2f\n", tInt.avarage());
  42.         tInt.sort();
  43.         System.out.println(tInt);
  44.         float fa = scanner.nextFloat();
  45.         float fb = scanner.nextFloat();
  46.         float fc = scanner.nextFloat();
  47.         Triple<Float> tFloat = new Triple<Float>(fa, fb, fc);
  48.         System.out.printf("%.2f\n", tFloat.max());
  49.         System.out.printf("%.2f\n", tFloat.avarage());
  50.         tFloat.sort();
  51.         System.out.println(tFloat);
  52.         double da = scanner.nextDouble();
  53.         double db = scanner.nextDouble();
  54.         double dc = scanner.nextDouble();
  55.         Triple<Double> tDouble = new Triple<Double>(da, db, dc);
  56.         System.out.printf("%.2f\n", tDouble.max());
  57.         System.out.printf("%.2f\n", tDouble.avarage());
  58.         tDouble.sort();
  59.         System.out.println(tDouble);
  60.     }
  61. }
  62. // vasiot kod ovde
  63. // class Triple
  64.  
  65.  
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement