You have a text file that generated 1000 lines with a faculty member\'s first na
ID: 3549561 • 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
In above only three types are defined i.e. associate , assistant and full but in question a professor is also introduce so i do coding according to your question in which professor is also added... and if any question related to this then plz let me know
public class Text {
public class Member {
String firstName;
String lastName;
String rank;
Double salary;
public Member(String firstName, String lastName, String rank,
Double salary) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.rank = rank;
this.salary = salary;
}
}
ArrayList<Member> result = new ArrayList<Member>();
public static void main(String[] args) throws MalformedURLException {
Test u = new Test();
u.read();
u.printAverages();
}
private void printAverages() {
Iterator<Member> iterator = result.iterator();
double assistantHolder = 0, assistantCounter = 0,
assosiatedHolder = 0, assosiatedCounter = 0,
fullHolder = 0, fullCounter = 0;
while (iterator.hasNext()) {
Member member = iterator.next();
if (member.rank.equals("associate")) {
assosiatedHolder += member.salary;
assosiatedCounter++;
} else if (member.rank.equals("assistant")) {
assistantHolder += member.salary;
assistantCounter++;
} else if (member.rank.equals("full")) {
fullHolder += member.salary;
fullCounter++;
} else if (member.rank.equals("Professor")) {
ProfessorHolder += member.salary;
ProfessorCounter++;
}
}
System.out.println("Number of Associate :"+(assosiatedCounter));
System.out.println("Associate average:"+(assosiatedHolder/assosiatedCounter));
System.out.println("Number of Assistant:"+(assistantCounter));
System.out.println("Assistant average:"+(assistantHolder/assistantCounter));
System.out.println("Number Of Full:"+(FullCounter));
System.out.println("Full average:"+(fullHolder/fullCounter));
System.out.println("Number Of Professor:"+(ProfessorCounter));
System.out.println("Associate average:"+(ProfessorHolder/ProfessorCounter));
}
// Now you have to definrd your text file, from where the program get the information. if program get the information from a text file at website whose URL is given then your code will be.//
private ArrayList<Member> read() throws MalformedURLException {
URL data = new URL("URL OF Text File");
try {
Scanner scan = new Scanner(data.openStream());
while (scan.hasNext()) {
String line = scan.nextLine();
String[] tokens = line.split("\s+",4);
String firstName = tokens[0], lastName = tokens[1], rank = tokens[2];
Double salary = Double.valueOf(tokens[3]);
result.add(new Teacher(firstName, lastName, rank, salary));
}
scan.close();
return result;
} catch (IOException e) {
e.getStackTrace();
}
return result;
}
}
I think this program helps u alot if u need further more help then plz let me know.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.