Write a program that calculates the grades for a course. Your program should pro
ID: 3876845 • Letter: W
Question
Write a program that calculates the grades for a course. Your program should prompt the user for the name of a file that stores exam scores. Each line of the file has the following format: LastName:FirstName:Exam1:Exam2:Exam3
The exams are to be weighted 25% for the first exam, 30% for the second exam, and 45% for the third exam. Based on that, a final grade is to be assigned: A if the total is at least 90, B if it is at last 80, C if it is at least 70, D if it is at least 60, and F otherwise. The highest grade based on the total points is always assigned, so a 75 gets a C.
Your program should output to the terminal a list of students with the letter grade, as follows:
LastName FirstName LetterGrade
It should also output to a file, whose name is provided by the user, lines of the form
LastName FirstName Exam1 Exam2 Exam3 TotalPoints LetterGrade
After it is done, it will output the grade distribution. If the input is:
Doe:John:100:100:100
Pantz:Smartee:80:90:80
Then the terminal output is:
Doe John A
Pantz Smartee B
And the output file will contain:
Doe John 100 100 100 100 A
Pantz Smartee 80 90 80 83 B
A 1
B 1
C 0
D 0
F 0
Explanation / Answer
MarkReader.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class MarkReader {
public static void main(String[] args) {
BufferedReader bReader = null;
FileReader fReader = null;
BufferedWriter bwriter = null;
FileWriter fWriter= null;
String [] names= new String[2];
int [] mark1 =new int[2];
int [] mark2 =new int[2];
int [] mark3 =new int[2];
String [] grades= new String[2];
//Reading the data from marksInput.txt
try {
fReader = new FileReader("marksInput.txt");
bReader = new BufferedReader(fReader);
String [] strArray =new String[5];
int counter =0;
String cLineStr;
while ((cLineStr = bReader.readLine()) != null) {
strArray = cLineStr.split(":");
names[counter] = strArray[0]+" "+strArray[1];
mark1[counter] = Integer.valueOf(strArray[2]);
mark2[counter] = Integer.valueOf(strArray[3]);
mark3[counter] = Integer.valueOf(strArray[4]);
counter ++;
}
} catch (IOException e) {
System.out.println("Exception");
}
//Finding the grade
double gradeMark = 0;
String grade = "F";
int Acount =0;
int Bcount =0;
int Ccount =0;
int Dcount =0;
int Fcount =0;
String out = "";
for(int i=0;i<names.length;i++){
grade = "F";
gradeMark = (.25 * mark1[i]) +(.3 *mark2[i]) + (.45 * mark3[i]);
if(gradeMark >= 90){
grade = "A";
Acount++;
}
else if( gradeMark >=80)
{
grade = "B";
Bcount++;
}
else if( gradeMark >=70)
{
grade = "C";
Ccount++;
}
else if( gradeMark >=60)
{
grade = "D";
Dcount++;
}
else if( gradeMark < 60){
grade = "F";
Fcount++;
}
grades[i]=grade;
String outTerminal = names[i]+" "+grades[i];
out = out+ names[i]+" "+mark1[i]+" "+mark2[i]+" "+mark3[i]+" "+Double.valueOf(gradeMark).intValue()+" "+grades[i]+" ";
System.out.println(outTerminal);
}
out = out +"A "+Acount+" ";
out = out +"B "+Bcount+" ";
out = out +"C "+Ccount+" ";
out = out +"D "+Dcount+" ";
out = out +"F "+Fcount+" ";
//Writing the output to gradeOutput.txt
try {
fWriter = new FileWriter("gradeOutput.txt");
bwriter = new BufferedWriter(fWriter);
bwriter.write(out);
bwriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
marksInput.txt
Doe:John:100:100:100
Pantz:Smartee:80:90:80
Console output:
Doe John A
Pantz Smartee B
gradeOutput.txt
Doe John 100 100 100 100 A
Pantz Smartee 80 90 80 83 B
A 1
B 1
C 0
D 0
F 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.