Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Генеричка тројка Problem 5 (0 / 0)
- // Да се имплемнтира генеричка класа Triple (тројка) од нумерички вредности (три броја). За класата да се имплементираат:
- //
- // конструктор со 3 аргументи,
- // double max() - го враќа најголемиот од трите броја
- // double average() - кој враќа просек на трите броја
- // void sort() - кој ги сортира елементите во растечки редослед
- // да се преоптовари методот toString() кој враќа форматиран стринг со две децимални места за секој елемент и празно место
- // помеѓу нив.
- //
- // Sample input | Sample output
- // 10 10 15 | 15.00
- // 5.5 4.4 3.3 | 11.67
- // 12 24 12.1 | 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
- import java.text.DecimalFormat;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Scanner;
- 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<>(a, b, c);
- System.out.printf("%.2f\n", tInt.max());
- System.out.printf("%.2f\n", tInt.average());
- tInt.sort();
- System.out.println(tInt);
- float fa = scanner.nextFloat();
- float fb = scanner.nextFloat();
- float fc = scanner.nextFloat();
- Triple<Float> tFloat = new Triple<>(fa, fb, fc);
- System.out.printf("%.2f\n", tFloat.max());
- System.out.printf("%.2f\n", tFloat.average());
- tFloat.sort();
- System.out.println(tFloat);
- double da = scanner.nextDouble();
- double db = scanner.nextDouble();
- double dc = scanner.nextDouble();
- Triple<Double> tDouble = new Triple<>(da, db, dc);
- System.out.printf("%.2f\n", tDouble.max());
- System.out.printf("%.2f\n", tDouble.average());
- tDouble.sort();
- System.out.println(tDouble);
- }
- }
- // vasiot kod ovde
- class Triple<T extends Number>{
- private final ArrayList<T> arrayList;
- public Triple(T num1, T num2, T num3){
- ArrayList<T> arrayList = new ArrayList<>();
- arrayList.add(num1);
- arrayList.add(num2);
- arrayList.add(num3);
- this.arrayList = arrayList;
- }
- public double max() {
- return arrayList.stream()
- .mapToDouble(Number::doubleValue)
- .max()
- .orElse(0.00);
- }
- public double average() {
- return arrayList.stream()
- .mapToDouble(Number::doubleValue)
- .average()
- .orElse(0.00);
- }
- public void sort() {
- this.arrayList.sort(Collections.reverseOrder().reversed());
- }
- @Override
- public String toString(){
- StringBuilder stringBuilder = new StringBuilder();
- DecimalFormat decimalFormat = new DecimalFormat("0.00");
- arrayList.forEach(n -> stringBuilder.append(decimalFormat.format(n.doubleValue())).append(" "));
- stringBuilder.delete(stringBuilder.length()-1, stringBuilder.length());
- return stringBuilder.toString();
- }
- }
Add Comment
Please, Sign In to add comment