0

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);
    }
}
efemoney
  • 3,149
  • 1
  • 12
  • 16

2 Answers2

4

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).

KevinO
  • 4,111
  • 4
  • 23
  • 34
1

Alright here is concise code for your project; however, copying my code is going to teach you nothing. You need to go through this line by line and teach yourself what it all does so you learn something. Now this is NOT the way I would program this (I'd put Person as a whole separate class away from the main method) but I'm not sure what your teacher is ok with so here is what you posted just slightly modified.

package Fall15Test3;
import java.util.*;

public class Person {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Person[] personArray = new Person[10];

        for (int i = 0; i < personArray.length; i++) {
            System.out.print("Please input name: ");
            String inputName = input.nextLine();
            System.out.print("Please input age: ");
            int inputAge = input.nextInt();
            personArray[i] = new Person(inputName, inputAge);
        }

        int ageTotal = 0;
        for (int i = 0; i < personArray.length; i++) {
            ageTotal += personArray[i].getAge();
        }
        System.out.println(ageTotal);
    }
}
Philip Vaughn
  • 596
  • 3
  • 17