Advertisement
SashkoKlincharov

[Java][НП] - Генеричка тројка

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