fork download
  1.  
  2. public class Main {
  3. public static void main(String[] args) {
  4. Icecream[] icecreams = new Icecream[4];
  5. icecreams[0] = new Icecream("Шоколадное", true, 5);
  6. icecreams[1] = new Icecream("Сливочное", false);
  7. icecreams[2] = new Icecream();
  8. icecreams[3] = new Icecream(icecreams[0]);
  9.  
  10. for (Icecream icecream : icecreams) {
  11. icecream.print();
  12. }
  13.  
  14. System.out.println("Средний процент жирности: " + Icecream.averageFatContent(icecreams));
  15. System.out.println("Количество мороженых с шоколадом: " + Icecream.countChocolateIcecream(icecreams));
  16. }
  17. }
  18.  
  19. class Icecream {
  20. private String name;
  21. private boolean hasChocolate;
  22. private int fatContent;
  23.  
  24. public Icecream() {
  25. this("", false, 0);
  26. }
  27.  
  28. public Icecream(String name, boolean hasChocolate) {
  29. this(name, hasChocolate, 0);
  30. }
  31.  
  32. public Icecream(String name, boolean hasChocolate, int fatContent) {
  33. this.name = name;
  34. this.hasChocolate = hasChocolate;
  35. this.fatContent = fatContent;
  36. }
  37.  
  38. public Icecream(Icecream icecream) {
  39. this.name = icecream.name;
  40. this.hasChocolate = icecream.hasChocolate;
  41. this.fatContent = icecream.fatContent;
  42. }
  43.  
  44. public String getName() {
  45. return name;
  46. }
  47.  
  48. public boolean hasChocolate() {
  49. return hasChocolate;
  50. }
  51.  
  52. public int getFatContent() {
  53. return fatContent;
  54. }
  55.  
  56. public static double averageFatContent(Icecream[] icecreams) {
  57. int totalFatContent = 0;
  58. int count = 0;
  59. for (Icecream icecream : icecreams) {
  60. if (icecream.getFatContent() != 0) {
  61. totalFatContent += icecream.getFatContent();
  62. count++;
  63. }
  64. }
  65. return (double) totalFatContent / count;
  66. }
  67.  
  68. public static int countChocolateIcecream(Icecream[] icecreams) {
  69. int count = 0;
  70. for (Icecream icecream : icecreams) {
  71. if (icecream.hasChocolate()) {
  72. count++;
  73. }
  74. }
  75. return count;
  76. }
  77.  
  78. public void print() {
  79. System.out.println("Название: " + name);
  80. if (hasChocolate) {
  81. System.out.println("Содержит шоколад.");
  82. } else {
  83. System.out.println("Не содержит шоколад.");
  84. }
  85. if (fatContent != 0) {
  86. System.out.println("Процент жирности: " + fatContent);
  87. }
  88. System.out.println();
  89. }
  90. }
  91.  
Success #stdin #stdout 0.15s 57764KB
stdin
Standard input is empty
stdout
Название: Шоколадное
Содержит шоколад.
Процент жирности: 5

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

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

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

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