In: Computer Science
Following is hw14. Save the demo filename as lastname_hw14.java and the class name as lastname_people.java.
Following are three arrays each containing 50 values. Store all three arrays in the main method. Write an object to transfer the three arrays.
int []age[] = {70, 68, 52, 69, 40, 59, 61, 34, 45, 50, 43, 22, 35, 50, 67, 33, 36, 22, 63, 65, 56, 31, 55, 28, 30, 24, 55, 35, 39, 59, 68, 50, 33, 45, 26, 54, 44, 56, 58, 24, 69, 56, 65, 52, 20, 23, 37, 27, 69, 35};
int []num_children = {2, 4, 3, 3, 4, 2, 3, 5, 5, 4, 4, 1, 5, 1, 4, 5, 4, 2, 2, 1, 2, 4, 3, 2, 3, 0, 5, 0, 0, 1, 3, 1, 4, 1, 0, 4, 0, 2, 2, 1, 3, 5, 1, 0, 0, 1, 1, 2, 1, 4};
int []weekly_pay = {1600, 950, 400, 1450, 1250, 1350, 2450, 750, 1700, 700, 1200, 1550, 1450, 450, 2600, 1550, 400, 850, 400, 600, 700, 1300, 2050, 900, 2250, 450, 1900, 800, 2500, 1500, 1200, 2900, 1100, 1700, 900, 1000, 750, 2200, 2600, 1300, 2150, 450, 1250, 1750, 800, 2850, 2150, 750, 2350, 1100};
1) In the class file, write a method called method1 to compute the average age.
2) In the class file, write a method called method2 to compute the average age of those who have exactly 3 children.
3) In the class file, write a method called metho3 to compute the lowest age of those making $1501 to $2500 per week.
Print the answers to all three parts from within the main method (lastname_hw14.java).
java
Please find your solution below and if any doubt comment and do upvote.
CODE:
public class lastname_people
{
//method1 to compute the average age.
public static double method1(int age[]){
int sum=0;
for(int i=0;i<50;i++)
{
sum+=age[i];
}
return sum/50;
}
//method2 to compute the average age of those who have exactly 3 children.
public static double method2(int age[],int num_children[]){
int sum=0;
int count=0;
for(int i=0;i<50;i++)
{
if(num_children[i]==3)
{
sum+=age[i];
count++;
}
}
return sum/count;
}
//method3 to compute the lowest age of those making $1501 to $2500 per week.
public static int method3(int []age,int weekly_pay[]){
int lowestAge=100000;
for(int i=0;i<50;i++)
{
if(weekly_pay[i]>=1501&&weekly_pay[i]<=2500){
if(lowestAge>age[i])
{
lowestAge=age[i];
}
}
}
return lowestAge;
}
public static void main(String[] args) {
int []age = {70, 68, 52, 69, 40, 59, 61, 34, 45, 50, 43, 22, 35, 50, 67, 33, 36, 22, 63, 65, 56, 31, 55, 28, 30, 24, 55, 35, 39, 59, 68, 50, 33, 45, 26, 54, 44, 56, 58, 24, 69, 56, 65, 52, 20, 23, 37, 27, 69, 35};
int []num_children = {2, 4, 3, 3, 4, 2, 3, 5, 5, 4, 4, 1, 5, 1, 4, 5, 4, 2, 2, 1, 2, 4, 3, 2, 3, 0, 5, 0, 0, 1, 3, 1, 4, 1, 0, 4, 0, 2, 2, 1, 3, 5, 1, 0, 0, 1, 1, 2, 1, 4};
int []weekly_pay = {1600, 950, 400, 1450, 1250, 1350, 2450, 750, 1700, 700, 1200, 1550, 1450, 450, 2600, 1550, 400, 850, 400, 600, 700, 1300, 2050, 900, 2250, 450, 1900, 800, 2500, 1500, 1200, 2900, 1100, 1700, 900, 1000, 750, 2200, 2600, 1300, 2150, 450, 1250, 1750, 800, 2850, 2150, 750, 2350, 1100};
//prints the output return from the functions
double average=method1(age);
System.out.println("The average age is "+ average);
double avgOf3Child=method2(age,num_children);
System.out.println("The average age of exactly 3 children is "+avgOf3Child);
int lowest=method3(age,weekly_pay);
System.out.println("The lowest age is "+ lowest);
}
}
OUTPUT: