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

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

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

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

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