Find Average Age Program in java

Question:

One of the least Insurance agencies recruited employees for their collection department. Now the HR needs a report as the average age of all the employees working in that department. Write a code to calculate the average age.

Implement a method “calculateAverage(int[] age)” to calculate the average age and return the result to the caller function. 

Note:

  1. Age limit should be minimum of 28 years and maximum of 40 years. 
  2. Minimum of 2 employees is mandatory to calculate average age, if fails, the output should be “Please enter a valid employee count”
  3. If any of the age is invalid, terminate the process and display “Invalid age encountered!”

Refer the sample given for read and display the output.

Sample Input 1:

Enter total no.of employees:

3

Enter the age for 3 employees:

30

31

32

Sample Output 1:

The average age is 31.00

Sample Input 2:

Enter total no.of employees:

2

Enter the age for 2 employees:

29

36

Sample Output 2:

The average age is 32.50

Sample Input 3:

Enter total no.of employees:

1

Sample Output 3:

Please enter a valid employee count

CODE:

import java.util.*;
import java.text.DecimalFormat;
public class Test 
{   
    
    double calculateAverage(int[] age)
    {
        int len=age.length;
        double sum=0.0;
        for(int i=0;i<len;i++)
        {
            sum+=age[i];
        }
        double avg=sum/len;
        return avg;
    }
    
    public static void main (String[] args) {
        Scanner sc =new Scanner(System.in);
        Test obj=new Test();
        
        System.out.println("Enter total no.of employees:");
        int n=sc.nextInt();
        int flag=0;
        if(n>1)
        { int[] age=new int[n];
            System.out.println("Enter the age for "+n+" employees:");
            
            for(int i=0;i<n;i++)
            {
                int temp=sc.nextInt();
                if(temp>=28 && temp<=40)
                {
                    age[i]=temp;
                }
                else
                {
                    System.out.println("Invalid age encountered!");
                    flag++;
                    break;
                }
            }
            
            if(flag==0)
            {   DecimalFormat df=new DecimalFormat("####.00");
                System.out.println("The average age is "+df.format(obj.calculateAverage(age)));
            }
        }
        else
        {
            System.out.println("Please enter a valid employee count");
        }
    }
}
public class Student 
{
    private int id;
    private String name;
    private int[] marks;
    private float average;
    private char grade;
    
    public void setId(int id)
    {
        this.id=id;
    }
    
    public int getId()
    {
        return this.id;
    }
    
    public void setName(String name)
    {
        this.name=name;
    }
    
    public String getName()
    {
        return this.name;
    }
    
    public void setMarks(int[] marks)
    {
        this.marks=marks;
    }
    
    public int[] getMarks()
    {
        return this.marks;
    }
    
    public void calculateAvg()
    {   float sum=0;
        
        for(int i=0;i<this.marks.length;i++)
        {
            sum+=this.marks[i];
        }
        
        average=(float)(sum/(this.marks.length));
        setAverage(average);
    }
    
    public void findGrade()
    {int flag=0;
        for(int i=0;i<this.marks.length;i++)
        {
            if(this.marks[i]<50)
            {
                flag++;
            }
        }
        if(flag>0)
        {
            grade='F';
        }
        else
        {
            if(this.average>=80 && this.average<=100)
            {
                grade='O';
            }
            else if(this.average>=50 && this.average<=79)
            {
                grade='A';
            }
            else
            {
                grade='F';
            }
        }
        setGrade(grade);
        
    }
    
    public void setAverage(float average)
    {
        this.average=average;
    }
    
    public void setGrade(char grade)
    {
        this.grade=grade;
    }
    
    public float getAverage()
    {
        return this.average;
    }
    
    public char getGrade()
    {
        return this.grade;
    }
}
Next:
  1. Login

Leave a Comment

Scroll to Top