Write a program that creates a file called \"grades.txt\" with 1000 lines. Each
ID: 3549474 • Letter: W
Question
Write a program that creates a file called "grades.txt" with 1000 lines. Each line in the "grades.txt" file will consist of a student's first name, last name, graduating year(2013,2014,2015), and grade. the student's first name and last name for the ith line are FirstNamei and LastName.The graduating year is randomly generated as freshman, sophomore, or junior. The grade is randomly generated from an A+ to A- for 2013, B+ to B- for 2014, or C+ to C- for 2015.
The sample output of the "grades.txt" file should look like this:
FirstName1 LastName1 2013 A-
FirstName2 LastName2 2015 C+
....
FirstName1000 LastName1000 2014 B
Explanation / Answer
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.FileWriter;
import java.util.*;
import java.io.File;
public class Program {
public static void main(String[] args) {
String text = "";
File file = new File("grades.txt");
Random random = new Random(System.currentTimeMillis());
int i=1;
int[] years = {2013,2014,2015};
String[] As= {"A+","A","A-"};
String[] Bs= {"B+","B","B-"};
String[] Cs= {"C+","C","C-"};
try {
BufferedWriter output = new BufferedWriter(new FileWriter(file));
while(i<=1000)
{
int year=random.nextInt(3);
text="FirstName"+i+" LastName"+i+" "+years[year]+" " ;
if(year==0)
{
text=text+As[random.nextInt(3)];
}
else if(year==1)
{
text=text+Bs[random.nextInt(3)];
}
else if(year==2)
{
text=text+Cs[random.nextInt(3)];
}
text=text+" ";
output.write(text);
i++;
}
output.close();
}
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.