A
A
Alexander K2021-01-04 15:20:44
Java
Alexander K, 2021-01-04 15:20:44

How to create a parameterized test for the avgAge() method for calculating the average age using the @CsvSource annotation?

Main.java
public static void main(String[] args) {
// s1, s2, s3 are objects of class 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("Alexander", "
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 {
//class fields
private String firstName;
private String lastName;
private int age;

//constructor with arguments
public Student(String firstName, String lastName, int age) {
setFirstName(firstName);
setLastName(lastName);
setage(age);
}

//default constructor
public Student(String name, double student) {
}

public Student() {
// TODO Auto-generated constructor stub
}

//getters and setters
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();
}

//method for calculating average age
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 aug;
}

// method for calculating the minimum age
public static int minAge(Student [] array) {

int min=100; // current minimum age

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

}
return min;
}

//method for calculating the maximum age
public static int maxAge(Student [] array) {

int max=0; // current maximum age

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 Auto-generated method stub
return null;
}

}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question