In: Computer Science
please write a java code, one for method and another for the Demo 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}
PROGRAM :
I have run and compiled the program using Eclipse IDE . I am attaching the code along with the input and output screenshot.
PROGRAM CODE
import java.util.Scanner;
public class Demo
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
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};
//function is being called here.
averageAge(gender,age,credits_completed);
}
private static void averageAge(String[] gender, int[] age, int[] credits_completed)
{
// TODO Auto-generated method stub
// Here we are calculating the average age of all female students.
int sum=0 ,count=0;
for(int i=0;i<gender.length;i++)
{
//checking the condition of geneder is Female or not.
if(gender[i].equals("F"))
{
sum = sum+age[i];
count++;
}
}
float averageAge_female;
averageAge_female=sum/count;
System.out.println("THE AVERAGE AGE OF ALL FEMALE STUDENTS IS :"+averageAge_female);
//Here we are calculating the least credits completed by male.
int flag = 0;
int least = 0;
for(int i=0;i<gender.length;i++)
{
if(flag==0 && gender[i].equals("M"))
{
flag = 1;
least = credits_completed[i];
}
else if(gender[i].equals("M") && least>credits_completed[i])
{
least = credits_completed[i];
}
}
System.out.println("THE LEAST AMOUNT OF CREDITS COMPLETED AMONG MALE IS : "+least);
}
}
PROGRAM INPUT SNAPS
PROGRAM OUTPUT SNAPS
I have made seperately two classes one for printing the value only and other for calculation.
CODES ARE FOLLOWING :
package Demo;
import java.util.Scanner;
import student.Student;
public class DemoMain
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
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};
DemoMethod d = new DemoMethod();
//function is being called here for calculating the average of females.
float avg = d.averageAge(gender,age,credits_completed);
System.out.println("THE AVERAGE AGE OF ALL FEMALE STUDENTS IS :"+avg);
//functions is being called for finding the least.
int leastvalue = d.leastValue(gender,age,credits_completed);
System.out.println("THE LEAST AMOUNT OF CREDITS COMPLETED AMONG MALE IS : "+leastvalue);
}
}
package Demo;
public class DemoMethod
{
// Here we are calculating the average age of all female students.
public float averageAge(String[] gender, int[] age, int[] credits_completed)
{
// TODO Auto-generated method stub
int sum=0 ,count=0;
for(int i=0;i<gender.length;i++)
{
//checking the condition of geneder is Female or not.
if(gender[i].equals("F"))
{
sum = sum+age[i];
count++;
}
}
float averageAge_female;
averageAge_female=sum/count;
return averageAge_female;
}
//Here we are calculating the least credits completed by male.
public int leastValue(String[] gender, int[] age, int[] credits_completed) {
// TODO Auto-generated method stub
int flag = 0;
int least = 0;
for(int i=0;i<gender.length;i++)
{
if(flag==0 && gender[i].equals("M"))
{
flag = 1;
least = credits_completed[i];
}
else if(gender[i].equals("M") && least>credits_completed[i])
{
least = credits_completed[i];
}
}
return least;
}
}
ATTACHING THE SCREENSHOT
OUTPUT