write a java code, and add methods to
-Compute the average age of all female students.
-Compute the least amount of credits completed among males.
Store the three arrays in the demo file.
Print the results within the demo file.
String []gender ={"F", "F", "M", "F", "F", "M", "M", "M", "M", "F", "M", "F", "M", "F", "F", "M", "M", "F", "M", "F"};
int []age = {18, 19, 19, 21, 20, 18, 24, 19, 21, 21, 21, 23, 20, 20, 19, 18, 19, 21, 20, 22};
int []credits_completed = {45, 12, 54, 23, 26, 33,18, 16, 28, 34, 36, 33, 49, 58, 51, 40, 38, 48, 56, 10};
Note: - The screenshot of code and output is attached for your refrence .
Demo.java
import java.util.ArrayList;
import java.util.Collections;
public class Demo
{
//main method
public static void main(String[] args) {
//array defined for gender , age , credit completed
String []gender ={"F", "F", "M", "F", "F", "M", "M", "M", "M", "F", "M", "F", "M", "F", "F", "M", "M", "F", "M", "F"};
int []age = {18, 19, 19, 21, 20, 18, 24, 19, 21, 21, 21, 23, 20, 20, 19, 18, 19, 21, 20, 22};
int []credits_completed = {45, 12, 54, 23, 26, 33,18, 16, 28, 34, 36, 33, 49, 58, 51, 40, 38, 48, 56, 10};
//list to store male credit completed and age of females
ArrayList<Integer> male = new ArrayList<Integer>();
ArrayList<Integer> female = new ArrayList<Integer>();
for(int i=0 ; i < gender.length ; i++)
{
if(gender[i].equals("F"))
{
female.add(age[i]);
}
else
{
male.add(credits_completed[i]);
}
}
Collections.sort(male);
Collections.sort(female);
System.out.println("Average age of all female students -- "+averageAgeFemale(female));
System.out.println("Least amount of credits completed among males -- "+leastCreditByMale(male));
}
//method to calculate least amount of credits completed among males.
private static int leastCreditByMale(ArrayList<Integer> male) {
return male.get(0);
}
//Method to Calculate average age of all female students.
private static float averageAgeFemale(ArrayList<Integer> female) {
float sum =0;
for (Integer integer : female)
{
sum = sum + integer;
}
return sum/female.size();
}
}
Demo.java -
Output-
Get Answers For Free
Most questions answered within 1 hours.