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

Фамилия: Филипп
Пол: Женский

Пол: Женский

Фамилия: Шторк
Возраст: 19
Пол: Мужской

Средний возраст индивидуума: 19.0
Количество мужчин: 2