fork download
/* package whatever; // don't place package name! */
public class Main {
    public static void main(String[] args) {
        Icecream[] icecreams = new Icecream[4];

        icecreams[0] = new Icecream("Шоколадное", true, 5);
        icecreams[1] = new Icecream("Сливочное", false);
        icecreams[2] = new Icecream();
        icecreams[3] = new Icecream(icecreams[0]);

        for (Icecream icecream : icecreams) {
            icecream.print();
        }

        System.out.println("Средний процент жирности: " + Icecream.averageFatContent(icecreams));
        System.out.println("Количество мороженых с шоколадом: " + Icecream.countChocolateIcecream(icecreams));
    }
}

class Icecream {
    private String name;
    private boolean hasChocolate;
    private int fatContent; // процент жирности

    public Icecream() {
        this("", false, 0);
    }

    public Icecream(String name, boolean hasChocolate) {
        this(name, hasChocolate, 0);
    }

    public Icecream(String name, boolean hasChocolate, int fatContent) {
        this.name = name;
        this.hasChocolate = hasChocolate;
        this.fatContent = fatContent;
    }

    public Icecream(Icecream icecream) {
        this.name = icecream.name;
        this.hasChocolate = icecream.hasChocolate;
        this.fatContent = icecream.fatContent;
    }

    public String getName() {
        return name;
    }

    public boolean hasChocolate() {
        return hasChocolate;
    }

    public int getFatContent() {
        return fatContent;
    }

    public static double averageFatContent(Icecream[] icecreams) {
        int totalFatContent = 0;
        int count = 0;
        for (Icecream icecream : icecreams) {
            if (icecream.getFatContent() != 0) {
                totalFatContent += icecream.getFatContent();
                count++;
            }
        }
        return (double) totalFatContent / count;
    }

    public static int countChocolateIcecream(Icecream[] icecreams) {
        int count = 0;
        for (Icecream icecream : icecreams) {
            if (icecream.hasChocolate()) {
                count++;
            }
        }
        return count;
    }

    public void print() {
        System.out.println("Название: " + name);
        if (hasChocolate) {
            System.out.println("Содержит шоколад.");
        } else {
            System.out.println("Не содержит шоколад.");
        }
        if (fatContent != 0) {
            System.out.println("Процент жирности: " + fatContent);
        }
        System.out.println();
    }
}
Success #stdin #stdout 0.12s 57828KB
stdin
Standard input is empty
stdout
Название: Шоколадное
Содержит шоколад.
Процент жирности: 5

Название: Сливочное
Не содержит шоколад.

Название: 
Не содержит шоколад.

Название: Шоколадное
Содержит шоколад.
Процент жирности: 5

Средний процент жирности: 5.0
Количество мороженых с шоколадом: 2