/* Craig Persiko - CS 111A
DaysOut.java - Solution to Days Out in-class exercise
Days Out program
This program calculates average days out of work by a number of employees
*/
import java.util.Scanner;
public class DaysOut
{
static Scanner keyIn = new Scanner(System.in);
public static void main(String args[])
{
int employees, // To hold the number of company employees.
daysAbsent; // To hold the total days absent of all employees.
employees = getNumEmps();
daysAbsent = getDaysMissed(employees);
showAvgDaysMissed(employees, daysAbsent);
} // End of main function.
//*******************************************************************
// Function getNumEmps. This function asks the user to enter *
// the number of employees in the company. Validation is performed *
// to verify at least one employee, the value entered is *
// then returned to function main. *
//*******************************************************************
static int getNumEmps()
{
int emps;
// Get the number of employees.
System.out.print("How many employees does the company have? ");
emps = keyIn.nextInt();
// Validate the input.
while (emps < 1)
{
System.out.print("The number of employees must be one or greater. ");
System.out.print("Please re-enter: ");
emps = keyIn.nextInt();
}
return emps;
} // End of function getNumEmps
//*******************************************************************
// Function daysMissed. Accepts as an argument the number of *
// employees in the company. User is asked to enter the *
// number of days missed by each employee. Validation performed to *
// confirm negative number is not entered. The total days missed *
// by all employees is returned. *
//*******************************************************************
static int getDaysMissed(int numEmps)
{
int days, // To hold the days missed by a given employee
totalMissed = 0; // Accumulates total days missed
for (int emp = 1; emp <= numEmps; emp++)
{
// Get number of days missed...
System.out.print("Days missed by employee #" + emp + " : ");
days = keyIn.nextInt();
// Validate the input...
while (days < 0)
{
System.out.print("Days missed must be zero or greater. Please re-enter: ");
days = keyIn.nextInt();
}
totalMissed += days; // Accumulate the number of days missed.
}
return totalMissed;
} // End of function getDaysMissed
//*******************************************************************
// Function showAveDaysMissed. This function accepts the number of *
// employees and the total number of days missed by all employees *
// as arguments. The average number of days missed is calculated *
// and output. *
//*******************************************************************
static void showAvgDaysMissed(int emps, int days)
{
float daysPerEmp = (float)days / emps;
System.out.print("The average number of days missed per ");
System.out.printf("employee is %.1f \n", daysPerEmp);
} // End of function avgDaysMissed
}
/* Sample Output:
bash-2.04$ java PP5
How many employees does the company have? -3
The number of employees must be one or greater. Please re-enter: 0
The number of employees must be one or greater. Please re-enter: 3
Days missed by employee # 1 :6
Days missed by employee # 2 :10
Days missed by employee # 3 :1
The average number of days missed per employee is 5.7
bash-2.04$ java PP5
How many employees does the company have? 5
Days missed by employee # 1 :-5
Days missed must be zero or greater. Please re-enter: 0
Days missed by employee # 2 :-1
Days missed must be zero or greater. Please re-enter: -10
Days missed must be zero or greater. Please re-enter: 10
Days missed by employee # 3 :20
Days missed by employee # 4 :0
Days missed by employee # 5 :5
The average number of days missed per employee is 7.0
*/
syntax highlighted by Code2HTML, v. 0.9