Actually, I am trying to upload the picture but it dosen\'t work as well. In thi
ID: 3877972 • Letter: A
Question
Actually, I am trying to upload the picture but it dosen't work as well.
In this programm i should open a file name lab2data.txt and it contains : first_name, last_name, salary, gender.
This is the explanation for this programm:
We are given a data file from a company containing all their employee names, salary, and gender. We are to process the data and display gender and salary statistics.
File format (each field separated by a tab):
• First row is a header row
• All other rows:
• first_name last_name salary gender
Safe assumptions to make No spaces will be in the file, only tabs. (No last names have more than one word)
Gender will be only Female or Male
You do not need to STORE the entire file in memory, process each line and forget the details that do not matter, such as employee’s name.
Your code should work for any data that follows the formatting rules File may contain MORE then 1000 records, perhaps millions
File name may be hard coded, however, do not add any path info After reading in and processing the data:
• Output the number of employees found, the number of females, and the number of males. • Output the average total salary, average female salary, and the average male salary.
Finally, the output would be:
Total Persons: 1000
Total Females: 486
Total Males: 514
Average Salary for all: $105230
Average Salary for Females: $108097
Average Salary for Males: $102519
Explanation / Answer
I have explained the entire code by giving comment on appropriate place
To run this code first you have to create one "lab2data.txt" text file in particular location where you can save your employee records.
I have given one sample of lab2data.txt file:
lab2data.txt
first_name last_name salary gender
anjelina jolly 100000 female
Johnny Depp 200000 male
Russell Crowe 500000 male
Brad Pitt 300000 male
Tom Cruise 400000 male
Sylvester Stallone 300000 male
Kate Winslet 200000 female
Bruce Willis 100000 male
Charlize Theron 150000 female
Keira Knightley 90000 female
Vin Diesel 1000000 male
Java program for this application:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Records {
public static void main(String args[]) {
//Initializing the count of the required parameters
int totalPersons = 0, totalFemales = 0, totalMales = 0, totalSalary = 0, totalMaleSalary = 0, totalFemaleSalary = 0;
//Pass the path to the file as a parameter
File file = new File("D:\Chegg\lab2data.txt");
Scanner sc;
try {
sc = new Scanner(file);
//Checking for the data in file
if(sc.hasNextLine()){
//Avoiding header line of file
sc.nextLine();
//Checking for the records in the file
if(sc.hasNextLine()){
while (sc.hasNextLine()) {
//Incrementing the count for the total person entries
totalPersons++;
//Splitting line by tab. and creating string array with size 4
String[] entries = sc.nextLine().split("\s+");
//Fetching salary of particular person and adding it to the total salary
totalSalary += Integer.valueOf(entries[2]);
//Check for male and female employee
if (entries[3].equalsIgnoreCase("male")) {
totalMales++; //Incrementing the count of male employees
totalMaleSalary += Integer.valueOf(entries[2]); //Adding the salary of that male employee to the total salary of male employees
} else {
totalFemales++; //Incrementing the count of female employees
totalFemaleSalary += Integer.valueOf(entries[2]); //Adding the salary of that female employee to the total salary of female employees
}
}
// Printing count of total employee
System.out.println("Total Persons:" + totalPersons);
// Printing count of female employee
System.out.println("Total Females:" + totalFemales);
// Printing count of male employee
System.out.println("Total Males:" + totalMales);
// Printing Average Salary for all employees by dividing totalSalary by totalPersons
System.out.println("Average Salary for all: $"
+ ((float) totalSalary / totalPersons));
// Printing Average Salary for Female employees by dividing totalFemaleSalary by totalFemales
System.out.println("Average Salary for Females: $"
+ ((float) totalFemaleSalary / totalFemales));
// Printing Average Salary for Male employees by dividing totalMaleSalary by totalMales
System.out.println("Average Salary for Males: $"
+ ((float) totalMaleSalary / totalMales));
} else {
System.out.println("File has only headers but there are no any records.");
}
} else {
System.out.println("File has no data");
}
} catch (FileNotFoundException e) {
//If file will be not present on directory then it will show error message here
e.printStackTrace();
}
}
}
Output:
Total Persons:11
Total Females:4
Total Males:7
Average Salary for all: $303636.38
Average Salary for Females: $135000.0
Average Salary for Males: $400000.0
[Note: If you want average salary as Integer value only then you just need to remove "(float)" from print
statement.]
I have used eclipse juno as tool to run this code. You can use any tool based on your convenient.
If you have any further query regarding this problem then please feel free to ask in comment.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.