Skoči na vsebino

P2 - 2021/22 - DN10 Obseg likov - resitev Aljaz Starc

import java.util.ArrayList;

interface Lik {
    public double obseg();
}


class Kvadrat implements Lik {
    int l;
    public Kvadrat (int l) { this.l = l; }
    public double obseg () { return this.l * 4; }
}

class Pravokotnik implements Lik {
    int l;
    int n;
    public Pravokotnik (int l, int n) {
        this.l = l;
        this.n = n;
    }
    public double obseg () { return 2 * this.l + 2 * this.n; }
}

class Nkotnik implements Lik {
    int l, n;
    public Nkotnik (int l, int n) {
        this.l = l;
        this.n = n;
    }
    public double obseg () { return this.l * this.n; }
}


class DN10 {

    private static ArrayList<Lik> liki = new ArrayList<Lik>();

    public static void main(String[] args) {
        for (int i = 0; i < args.length; i++) {
            String[] parts = args[i].split(":");
            
            switch (parts[0]) {
                case "kvadrat":
                    liki.add(new Kvadrat(Integer.parseInt(parts[1])));
                    break;
                case "pravokotnik":
                    liki.add(new Pravokotnik(Integer.parseInt(parts[1]), Integer.parseInt(parts[2])));
                    break;
                case "nkotnik":
                    liki.add(new Nkotnik(Integer.parseInt(parts[1]), Integer.parseInt(parts[2])));
                    break;
                default:
                    break;
            }
        }

        System.out.println((int) DN10.skupniObseg());
    }

    public static double skupniObseg () { return DN10.liki.stream().mapToDouble((Lik l) -> l.obseg()).reduce(0, Double::sum); }
}

Zadnja posodobitev: May 7, 2022