Create a data file with 1,000 lines. Each line in the file consists of a faculty
ID: 3565024 • Letter: C
Question
Create a data file with 1,000 lines. Each line in the file consists of a faculty member's first name, last name, rank, and salary. The faculty member's first name and last name for the ith line are FirstNamei and LastNamei. The rank is randomly generated as assistant, associate, and full. The salary is randomly generated as a number with two digits after the decimal point. The salary for an assistant professor should be in the range from 50,000 to 80,000, for associate professer from 60,000 to 110,000, and for full professor from 75,000 to 130,000. Save the file in Salary.txt. Here are some sample data.
FirstName1 LastName1 assistant 60055.95
FirstName2 LastName2 associate 81112.45
...
FirstName1000 LastName1000 full 92255.21
Explanation / Answer
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
public class Faculty {
public static void main(String... args){
int total = 1000,i,num;
String str,fname="FirstName",lname="LastName",assist="assistant",assoc="associate",full="full";
Random rand = new Random();
try{
BufferedWriter br = new BufferedWriter(new FileWriter("D:\Salary.txt"));
for(i=1;i<=total;i++){
str = fname.concat(Integer.toString(i)) + " " + lname.concat(Integer.toString(i));
num = rand.nextInt(3);
switch(num){
case 0:
str = str + " " + assist + " " + String.format("%.2f",(rand.nextInt(30001) + 50000 +rand.nextDouble()));
break;
case 1:
str = str + " " + assoc + " " + String.format("%.2f",(rand.nextInt(50001) + 60000 + rand.nextDouble()));
break;
case 2:
str = str + " " + full + " " + String.format("%.2f",(rand.nextInt(55001) + 75000 + rand.nextDouble()));
break;
default:
System.out.println("Failure");
break;
}
br.write(str);
br.newLine();
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.