fork download
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. class Icecream {
  5. private String name;
  6. private boolean hasChocolate;
  7. private double fatPercentage;
  8.  
  9. public Icecream(String name, boolean hasChocolate, double fatPercentage) {
  10. this.name = name;
  11. this.hasChocolate = hasChocolate;
  12. this.fatPercentage = fatPercentage;
  13. }
  14.  
  15. public String getName() {
  16. return name;
  17. }
  18.  
  19. public boolean hasChocolate() {
  20. return hasChocolate;
  21. }
  22.  
  23. public double getFatPercentage() {
  24. return fatPercentage;
  25. }
  26.  
  27. public static void main(String[] args) {
  28. // Создаем список мороженого
  29. List<Icecream> icecreams = new ArrayList<>();
  30. icecreams.add(new Icecream("Vanilla", false, 10.0));
  31. icecreams.add(new Icecream("Chocolate", true, 15.0));
  32. icecreams.add(new Icecream("Strawberry", false, 8.0));
  33. icecreams.add(new Icecream("Mint", false, 12.0));
  34. icecreams.add(new Icecream("Chocolate Chip", true, 18.0));
  35.  
  36. // Подсчитываем средний процент жирности и количество мороженого с шоколадом
  37. double totalFatPercentage = 0;
  38. int chocolateCount = 0;
  39.  
  40. for (Icecream icecream : icecreams) {
  41. totalFatPercentage += icecream.getFatPercentage();
  42. if (icecream.hasChocolate()) {
  43. chocolateCount++;
  44. }
  45. }
  46.  
  47. double averageFatPercentage = totalFatPercentage / icecreams.size();
  48.  
  49. System.out.println("Средний процент жирности: " + averageFatPercentage);
  50. System.out.println("Количество мороженого с шоколадом: " + chocolateCount);
  51. }
  52. }
  53.  
Success #stdin #stdout 0.12s 55700KB
stdin
Standard input is empty
stdout
Средний процент жирности: 12.6
Количество мороженого с шоколадом: 2