Wednesday, January 1, 2014

The Person Class Averaging the Three Ages


Can you tell what I should put in my code so that my class testperson actually averages the three ages??!! I know that values must be declared somewhere in testperson or something like that. Here is my code so far:



Java Code:



public class TestPerson extends AverageThreeInts
{
public static void main(String[] args)
{



Person person1 = new Person("John", "Smith", 32);
Person person2 = new Person("Jane", "Doe", 25);
Person person3 = new Person("Tommy", "Banks", 42);
person1.printName();
person1.printAge();
person1.printAgeGroup();

person2.printName();
person2.printAge();
person2.printAgeGroup();

person3.printName();
person3.printAge();
person3.printAgeGroup();
}

public int getAverageAge(Person[] people)

{
int sum = 0;

for (int i : values) {
sum += i;
}

return (sum / values.length);


}




}


Java Code:



import java.util.Scanner;

public class AverageThreeInts{

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int inputs = 2;
int[] values = new int[3];

while (inputs > -1) {
// It's a good idea to prompt for input before trying to get the next input from the user
System.out.println("Please enter an integer.");
values[inputs] = scanner.nextInt();

inputs--;
}

// It's also a good idea to give a clear message about what the output is
System.out.println("The averge of the integers entered is: " + averageValue(values));
}

private static int averageValue(int[] values) {
int sum = 0;

for (int i : values) {
sum += i;
}

return (sum / values.length);
}

}


No comments:

Post a Comment