If you were to get these questions on a test and could not use a computer to ans
ID: 3850244 • Letter: I
Question
If you were to get these questions on a test and could not use a computer to answer this question, only pen and paper.
How would you answer them? Please explain in detail and with step-by-step on how to approach questions like these :)
The class Person represents a person: class Person the person's name and year of birth private String name private int birth Year; public Person (String name int birth Year this name name this birth Year birth Year public String to String return "s" name birth Year public int age java time .LocalDate Current Date java time. Local Date now int current Year current Date.getYear o, return current year birth Year; a A static method, averageAge, receives a non-empty array of persons (objects of type Person) and returns the average age for these persons. Create that method. b) A static method, youngest Person, receives a non-empty array of persons (objects of type Person) and returns the youngest of these persons. Create that method. c Create an array of persons (objects of type Person), and call the methods averageAge and youngestPerson with this arrayExplanation / Answer
It is very easy to answer these type of questions. As you have said you need to implement in a test without running it, I'll just provide the methods: -
a) Here we need to create a method to calculate averageAge: -
public static double averageAge(Person[] persons) { // definition method having array of type Person as parameter
// Here we need to iterate the array to get sum of the age divided by length of array i.e. number of elements in // array.
double sum = 0; // variable to get total sum
for(int i = 0; i < persons.length;i++) { // for loop to iterate array of persons
sum = sum + persons[i].age(); // adding age by getting it by age method defined in person class
}
return sum/(double)persons.length; // returning average
}
b) method to return minimum aged person i,e, youngest person: -
public static int youngestPerson(Person[] persons) {
// definition method having array of type Person as parameter
// Here we need to iterate the array to get smallest age
int minAge = persons[0]; // variable to get index of minimum aged persons, initialized with 0 as first member
for(int i = 1; i < persons.length;i++) { // for loop to iterate array of persons
if(persons[i].age() < persons[minAge]) { // To compare if present person's age is less then initial minimum
minAge = i; // updating the minimum index
}
}
return minAge; // returning the minimum element Note it starts from 0, so 0 means 1st person
}
c. main method to call the above two functions: -
public static void main(String[] args) { // main method for third part to call the above two created methods
// creating person object looking into constructor defined in the question
Person p1 = new Person("David",1992);
Person p2 = new Person("Ramsey",1990);
Person persons[] = new Person[2]; // creating array of persons
//adding array elements
persons[0] = p1;
persons[1] = p2;
System.out.println("Average age of persons: "+averageAge(persons)); // printing the average age of array
// printing youngest element in the array. Adding one here as array index starts from 0
System.out.println("Youngest person is # "+ (youngestPerson(persons)+1));
}
Note that for these type of questions, we just need to see the class definition given in question and use the methods and structure of the class that is defined.