prometheus800

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

Nov 25th, 2020
1,167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.70 KB | None | 0 0
  1. //Генеричка тројка Problem 5 (0 / 0)
  2. //        Да се имплемнтира генеричка класа Triple (тројка) од нумерички вредности (три броја). За класата да се имплементираат:
  3. //
  4. //        конструктор со 3 аргументи,
  5. //        double max() - го враќа најголемиот од трите броја
  6. //        double average() - кој враќа просек на трите броја
  7. //        void sort() - кој ги сортира елементите во растечки редослед
  8. //        да се преоптовари методот toString() кој враќа форматиран стринг со две децимални места за секој елемент и празно место
  9. //        помеѓу нив.
  10. //
  11. // Sample input           |  Sample output
  12. //        10 10 15        |  15.00
  13. //        5.5 4.4 3.3     |  11.67
  14. //        12 24 12.1      |  10.00 10.00 15.00
  15. //                        |  5.50
  16. //                        |  4.40
  17. //                        |  3.30 4.40 5.50
  18. //                        |  24.00
  19. //                        |  16.03
  20. //                        |  12.00 12.10 24.00
  21.  
  22. import java.text.DecimalFormat;
  23. import java.util.ArrayList;
  24. import java.util.Collections;
  25. import java.util.Scanner;
  26.  
  27. public class TripleTest {
  28.     public static void main(String[] args) {
  29.         Scanner scanner = new Scanner(System.in);
  30.         int a = scanner.nextInt();
  31.         int b = scanner.nextInt();
  32.         int c = scanner.nextInt();
  33.         Triple<Integer> tInt = new Triple<>(a, b, c);
  34.         System.out.printf("%.2f\n", tInt.max());
  35.         System.out.printf("%.2f\n", tInt.average());
  36.         tInt.sort();
  37.         System.out.println(tInt);
  38.         float fa = scanner.nextFloat();
  39.         float fb = scanner.nextFloat();
  40.         float fc = scanner.nextFloat();
  41.         Triple<Float> tFloat = new Triple<>(fa, fb, fc);
  42.         System.out.printf("%.2f\n", tFloat.max());
  43.         System.out.printf("%.2f\n", tFloat.average());
  44.         tFloat.sort();
  45.         System.out.println(tFloat);
  46.         double da = scanner.nextDouble();
  47.         double db = scanner.nextDouble();
  48.         double dc = scanner.nextDouble();
  49.         Triple<Double> tDouble = new Triple<>(da, db, dc);
  50.         System.out.printf("%.2f\n", tDouble.max());
  51.         System.out.printf("%.2f\n", tDouble.average());
  52.         tDouble.sort();
  53.         System.out.println(tDouble);
  54.     }
  55. }
  56. // vasiot kod ovde
  57. class Triple<T extends Number>{
  58.     private final ArrayList<T> arrayList;
  59.  
  60.     public Triple(T num1, T num2, T num3){
  61.         ArrayList<T> arrayList = new ArrayList<>();
  62.         arrayList.add(num1);
  63.         arrayList.add(num2);
  64.         arrayList.add(num3);
  65.         this.arrayList = arrayList;
  66.     }
  67.  
  68.     public double max() {
  69.         return arrayList.stream()
  70.                 .mapToDouble(Number::doubleValue)
  71.                 .max()
  72.                 .orElse(0.00);
  73.     }
  74.  
  75.     public double average() {
  76.         return arrayList.stream()
  77.                 .mapToDouble(Number::doubleValue)
  78.                 .average()
  79.                 .orElse(0.00);
  80.     }
  81.  
  82.     public void sort() {
  83.         this.arrayList.sort(Collections.reverseOrder().reversed());
  84.     }
  85.  
  86.     @Override
  87.     public String toString(){
  88.         StringBuilder stringBuilder = new StringBuilder();
  89.         DecimalFormat decimalFormat = new DecimalFormat("0.00");
  90.         arrayList.forEach(n -> stringBuilder.append(decimalFormat.format(n.doubleValue())).append(" "));
  91.         stringBuilder.delete(stringBuilder.length()-1, stringBuilder.length());
  92.         return stringBuilder.toString();
  93.     }
  94. }
Add Comment
Please, Sign In to add comment