You have a text file that generated 1000 lines with a faculty member\'s first na
ID: 3549568 • Letter: Y
Question
You have a text file that generated 1000 lines with a faculty member's first name, last name, rank and salary. The ranks are randomly generated as assistant, associate, or full. The salary is randomly generated for an assistant from $40000 to 50000, for associate from 50000 to 100000 and for full from 60000 to 110000. The output looks like this:
FirstName1 LastName1 assistant $25133.42
FirstName2 LastName2 full $100123.44
...
FirstName1000 LastName1000 associate $52344.11
Write a program that will process the data file to read the file and calculate
a) The number of assistants and the average salary
b) The number of associates and the average salary
c) The number of professors and the average salary
d) The total number of professors and their average salary
e) The Lowest Salary and the person who earns it
This should be displayd in a JFrame, and an ArrayList should be useful.
Explanation / Answer
I've made the changes and added file creator method too.. Pls check the file path before executing the program else it will give an exception. There are three classes.
package com.chegg;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.text.DecimalFormat;
import java.util.Random;
public class CreateFile {
Random random = new Random();
String firstName = "FirstName";
String lastName = "LastName";
String[] grade = {"assistant","assocaite","full"};
public void createFile(String filePath) throws Exception{
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(filePath)));
String lineData;
for(int i=1; i<=1000; i++){
String grade = getGrade();
lineData = firstName+i+" "+lastName+i+" "+grade+" $"+getSalary(grade);
writer.write(lineData);
writer.newLine();
writer.flush();
}
writer.close();
}
String getSalary(String grade){
double lowerLimit=0;
double upperLimit=0;
if(grade.equalsIgnoreCase("assistant")){
lowerLimit = 40000;
upperLimit = 50000;
}
else if(grade.equalsIgnoreCase("assocaite")){
lowerLimit = 50000;
upperLimit = 100000;
}
else if(grade.equalsIgnoreCase("full")){
lowerLimit = 60000;
upperLimit = 110000;
}
double sal = (lowerLimit)+((random.nextDouble())*(upperLimit-lowerLimit));
return new DecimalFormat("#.00").format(sal);
}
String getGrade(){
int i = random.nextInt(3);
return grade[i];
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.