Q:

Access attribute of an object in an array

I am currently struggling with finishing this code. I am reviewing for my test tomorrow so any help is good!
The instructions on what to do are in the comments in the code.
I'm not sure if any of the stuff I have done in this code is valid honestly. My main issue currently though is figuring out how to calculate the sum of the 10 people. Could y'all help?
package Fall15Test3;
import java.util.*;

public class Person {
    String name;
    int age;

    public Person(String name, int age) {

    }

    public void setName(String name) {

    }

    public String getName() {
        return name;
    }

    public void setAge(int age) {

    }

    public int getAge() {
        return age;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        //Create the array of 10 Person objects.
        Person[] personArray = new Person[10];

        //Initialize the array of persons using user's inputs.
        for (int i = 0; i < personArray.length; i++) {
            System.out.print("Please input name: ");
            String name = input.nextLine();
            System.out.print("Please input age: ");
            int age = input.nextInt();
            personArray[i] = new Person(name, age);
        }

        //Calculate the sum of the ages of the 10 people.
        int ageTotal = 0;
        for (int i = 0; i < personArray.length; i++) {
            ageTotal += WHAT DO I PUT HERE;
        }
        System.out.println(ageTotal);
    }
}

A:

When looping over the array, the objects in the array are accessible at each index in the loop, and the methods of the object type may be invoked without the need to obtain an additional reference to the object. Therefore, you may do something like:
ageTotal += personArray[i].getAge();

This approach simply gets the object directly, since personArray is an array of Person objects.
You can also do something like the following, which will explicitly obtain a reference to the object at the specified index in the array:
for (int i = 0; i < personArray.length; ++i) {
  Person p = personArray[i]; // Make clear we are obtaining the object
  ageTotal += p.getAge();  // get the age set in the Person "p" object
}

EDIT: updates based upon a couple of comments
You can use a different form of the for loop:
for (Person p : personArray) {
  ageTotal += p.getAge();
}

You can also get the total using the stream approach:
int ageTotal = Arrays.stream(personArray).mapToInt(p -> p.age).sum();
System.out.println(ageTotal);

Given the particulars of your example, you could simply accumulate the ageTotal in the input loop. However, I am assuming that having a separate loop is a good learning example for other processing.
Also, your example class of Person does not set the name, age in many of the methods (e.g., the constructor).

