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

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

Название: Фруктовое
Не содержит шоколад.
Процент жирности: 3

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

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