Question: X754: Null Pointer Exception Practice 3 For the question below, assume the following implementation of a Person class: 1 public class Person { 2 2


X754: Null Pointer Exception Practice 3 For the question below, assume the following implementation of a Person class: 1 public class Person { 2 2 private int age; 3 private String name; private String grade; 7 8 public Person(int a, String n, String gr) { age = a; namen; ; grade = gr; } 9 10 11 12 13 public int getAge() { return age; ; } 14 15 16 17 18 19 public String getName() { return name; } 20 21 22 23 public String getGrade() { return grade; } } 24 25 26 27) Your coworker now wants to create a method that will take in 3 person objects as parameters and return oldest Person. However, this code is failing if one or more) of the parameters equals null. If one of the parameters is null, the program should return the oldest all non-null people. If all three are null, the method should return null. . For example, if the following people were passed as parameters: Anne, age 15, grade B null Ben, age 10, grade F then the method would return Anne. Fix the issue with the code think about how you would explain the problem to your coworker. Your Answer: 0 - 0 W H K N H 6 8 1 public Person getoldest(Person persona, Person personB, Person person) { 2 if(personA.getAge() > personB.getAge()) { if (personA.getAge() > person.getAge()) { return personA; } else { return personc; } 9 } 10 else if (personB.getAge() > personc.getAge()) { return personB; 12 } 13 else { 14 return personC; 15 } 16} 17 11
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
