Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Да се имплемнтира генеричка класа Triple (тројка) од нумерички вредности (три броја). За класата да се имплементираат:
- конструктор со 3 аргументи,
- double max() - го враќа најголемиот од трите броја
- double average() - кој враќа просек на трите броја
- void sort() - кој ги сортира елементите во растечки редослед
- да се преоптовари методот toString() кој враќа форматиран стринг со две децимални места за секој елемент и празно место помеѓу нив.
- import java.util.*;
- class Triple<T extends Number>{
- private T a;
- private T b;
- private T c;
- private List<T> list;
- public Triple(T a, T b, T c){
- this.a = a;
- this.b = b;
- this.c = c;
- this.list = new ArrayList<>();
- list.add(a);
- list.add(b);
- list.add(c);
- }
- double max(){
- return list.stream().mapToDouble(Number::doubleValue).max().orElse(0);
- }
- double avarage(){
- return list.stream().mapToDouble(Number::doubleValue).average().orElse(0);
- }
- void sort(){
- Collections.sort(list,Comparator.comparing(Number::doubleValue));
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- for(T t : list){
- sb.append(String.format("%.2f ",t.doubleValue()));
- }
- return sb.substring(0,sb.length()-1);
- }
- }
- public class TripleTest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int a = scanner.nextInt();
- int b = scanner.nextInt();
- int c = scanner.nextInt();
- Triple<Integer> tInt = new Triple<Integer>(a, b, c);
- System.out.printf("%.2f\n", tInt.max());
- System.out.printf("%.2f\n", tInt.avarage());
- tInt.sort();
- System.out.println(tInt);
- float fa = scanner.nextFloat();
- float fb = scanner.nextFloat();
- float fc = scanner.nextFloat();
- Triple<Float> tFloat = new Triple<Float>(fa, fb, fc);
- System.out.printf("%.2f\n", tFloat.max());
- System.out.printf("%.2f\n", tFloat.avarage());
- tFloat.sort();
- System.out.println(tFloat);
- double da = scanner.nextDouble();
- double db = scanner.nextDouble();
- double dc = scanner.nextDouble();
- Triple<Double> tDouble = new Triple<Double>(da, db, dc);
- System.out.printf("%.2f\n", tDouble.max());
- System.out.printf("%.2f\n", tDouble.avarage());
- tDouble.sort();
- System.out.println(tDouble);
- }
- }
- Sample input
- 10 10 15
- 5.5 4.4 3.3
- 12 24 12.1
- Sample output
- 15.00
- 11.67
- 10.00 10.00 15.00
- 5.50
- 4.40
- 3.30 4.40 5.50
- 24.00
- 16.03
- 12.00 12.10 24.00
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement