Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What I need help on is the findMatchingPerson method and the testPerson method.

ID: 3628491 • Letter: W

Question

What I need help on is the findMatchingPerson method and the testPerson method. Also, I get an error at the line sum = sum + people[i]; in the averageAge method that I'm not sure how to fix. I know this is a lot of stuff, but I really need the help. i've been playing around with this for hours now and I just don't understand. LifeSaver rating is guaranteed.

/**
* Static Methods:
* (1) averageAge: averages the ages of an array of Person objects.
* (2) findMatchingPerson: Returns any element of the Person array that has the same name and age as the parameters.
* NOTE1 that this should not be a NEW Person object, but one that already exists in the array.
* NOTE2 If no match is found, the method should return null.
* (3) testAverageAge: Tests that the averageAge method works.
* (4) testPerson: Tests that the constructor and get methods of Person work.
*/
public class PersonMethods {

public static double averageAge(Person[] people) {
double sum = 0;
for (int i=0; i < people.length; i++)
sum = sum + people[i];
double average = (sum / people.length);

return average;

}

public static Person findMatchingPerson(Person[] people, String name, int age) {
return null;
}




public static boolean testAverageAge(Person[] people, double expected) {
double result = averageAge(people);
if (result == expected) {
System.out.println("The average of this array is " + result);
return true;
}
else {
System.out.println("expected " + expected + " not" + result);
return false;
}

}

public static boolean testPerson(String n, int a) {

return false;
}

public static boolean testFindMatchingPerson(Person[] people, String name, int age, Person expected) {
Person result = findMatchingPerson(people, name, age);
if (result != expected) {
System.out.println("findMatchingPerson failed! expected " + expected + " not " + result);
return false;
} else {
System.out.println("findMatchingPerson passed.");
return true;
}
}

public static boolean testSetAge(Person p, int newAge, int expected) {
p.setAge(newAge);
if (p.getAge() != expected) {
System.out.println("setAge failed. Expected " + expected + " not " + p.getAge());
return false;
} else {
System.out.println("age passed.");
return true;
}
}

public static void main(String[] args) {
Person[] people = new Person[2];
people[0] = new Person("Joe ", 21);
people[1] = new Person("Jess ", 26);
testPerson("Joe", 21);
testFindMatchingPerson(people, "Jess ", 26, people[1]);
testAverageAge(people, 23.5);
testSetAge(new Person("Jimmie", 22), -2, 22);
testSetAge(new Person("Jimmie", 22), 20, 20);
}
}



The methods in the Person work are here and they should be right:
public class Person {
//Instance Variables
String name;
private int age;

//constructor
public Person(String n, int a){
name = n;
age = a;
}

//Instance Methods
//a
public String getName(){
return name;
}
public int getAge(){
return age;
}
public void setName(String n){
name = n;
}
public int setAge(int a){
if (a < 0){
age = a;
}
return a;

}
//(b) a method named toString with no parameters and a String return type. This should return a
//* String representation of this Person (for example, their name and age).

public String toString(){
String Person = (name + age);
return Person;
}
//(c) a method named equals with a single Person parameter and a boolean return type.
// This method returns true if the Person parameter has the same name and age as this object.
public boolean equals(Person p){
if ((p.name == name) && (p.age == age)){
return true;
}
else{
return false;
}
}
}

Explanation / Answer

// please change following line to

sum = sum + people[i].getAge();

// now your program will work...

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote