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

Имя: Шабаева
Нет долгов.

Имя: 
Нет долгов.

Имя: Кожедуб
Возраст: 19
Имеет долги.

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