Как создать параметризированный тест для метода расчета среднего возраста avgAge(), используя аннотацию @CsvSource?

Main.java
public static void main(String[] args) {
// s1, s2, s3 - объекты класса Student
Student s1 = new Student();
s1.setFirstName("Alexander");
s1.setLastName("kriza");
s1.setAge(36);

Student s2 = new Student("Ivan", "nIkulin", 87);

Student s3 = new Student("kONStantin","petrov", 33);

System.out.println(s1.getFullName() + ", " + s1.getAge());
System.out.println(s2.getFullName() + ", " + s2.getAge());
System.out.println(s3.getFullName() + ", " + s3.getAge());


Student[] students = { s1,s2,s3};
Student[] students2 = {new Student("Alexandr", "Ivanov", 20), new Student("Vladimir", "Petrov", 18),
new Student("Aleksey", "Alekseev", 35)};

System.out.println("AVG= "+Student.avgAge(students2));
System.out.println("MAX= "+Student.maxAge(students));
System.out.println("MIN= "+Student.minAge(students));



}

Student.java
class Student {
//поля класса
private String firstName;
private String lastName;
private int age;

//конструктор с аргументами
public Student(String firstName, String lastName, int age) {
setFirstName(firstName);
setLastName(lastName);
setAge(age);
}

//конструктор по умолчанию
public Student(String name, double student) {
}

public Student() {
// TODO Автоматически созданная заглушка конструктора
}

//геттеры и сеттеры
public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
if (firstName.length() > 1) {
this.firstName = firstName.substring(0, 1).toUpperCase() + firstName.substring(1).toLowerCase();
} else {
this.firstName = firstName.toUpperCase();
}

}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
if (lastName.length() > 1) {
this.lastName = Character.toUpperCase(lastName.charAt(0)) + lastName.substring(1).toLowerCase();
} else {
this.lastName = lastName;
}
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age > 18 && age < 50 ? age : 18;
}

public String getFullName() {
return getLastName() + " " + getFirstName();
}

//метод для расчета среднего возраста
public static int avgAge(Student [] array) {
int avg = 0;
int ageSum=0;

for (int i = 0; i < array.length; i++) {
ageSum += array[i].age;
}
avg= ageSum / array.length;

return avg;
}

//метод для расчета минимального возраста
public static int minAge(Student [] array) {

int min=100; // текущий минимальный возраст

for (int i=0; i < array.length; i++){
if (array[i].getAge() < min) {
min = array[i].getAge();
}

}
return min;
}

//метод для расчета максимального возраста
public static int maxAge(Student [] array) {

int max=0; // текущий максимальный возраст

for (int i=0; i < array.length; i++){
if (array[i].getAge() > max) {
max = array[i].getAge();
}

}
return max;
}

public static String avgAge(double student) {
// TODO Автоматически созданная заглушка метода
return null;
}

}
  • Вопрос задан
  • 356 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы