Write a program that will determine the letter grade of students using understan
ID: 3846867 • Letter: W
Question
Write a program that will determine the letter grade of students using understanding of java class, objects, exception handling, and collection in java Your program will need to accept two command line arguments. The first argument withe name of a disk file that contains the names of students, and their test scores, separated by commas follows by one or more spaces. Each line in the file will contain scores for one student. The second argument argument will be the name of an output disk file. Your program will create a new output disk file using that name. The output file will have grade information of all students whose information was read from input file, in a sorted order. You will be writing one line in the output file for one student. You will write name of one student and the letter grade he/she got in the class in each line (you should format the texts in the output file). Calculation: The test scores are weighed. There are four quizzes, 40% total, midterm I is 20%, midterm II is 15% and the final is 25%. All scores in the input file are recorded out of 100. You need to apply the weight for each score in the program to calculate the final score. The final score is tabulated as follows: Final Score = quiz1*.10 + quiz2*.10 + quiz3*.10 + quiz4*.10 + quiz1*.10 + mid1*.20 + mid2*.15 + Final*.25. Final Score >= 90% letter grade is A, 80%--89% B, 70%--79% C, 60--69%D, <= 59% F
Sample input.txt file : name, quiz1, quiz2, quiz3, quiz4, mid1, mid2, final
Rock Smith, 100, 90, 80, 100, 89, 99, 88
Baker Adam, 90, 90, 100, 100, 99, 100, 95
John Khuu, 83, 77, 88, 76, 79, 72, 76
..
<<more if there are more students>>
Sample output file : output.txt (the output format is : name, letter grade, sorted by name) e.g Letter grade for three students given in input.txt file is:
Rock Smith: A
Baker Adam: A
John Khuu: C
..
<<more if there are more students>>
Sample Run of the program (name of the application is letterGrader): Remember there are two sets of outputs. Letter grade is written in the output disk file (which is not shown in the screen), and score averages are displayed on the console (and are not written in the disk file).
Here is an example of run in the command line
C:>java letterGrader input.txt output.txt
Letter grade has been calculated for students listed in input file input.txt and written to output file output.txt
Here is the class averages:
Q1 Q2 Q3 Q4 Mid1 Mid2 Final
Average 82.25 80.38 82.25 83.88 81.38 84.13 78.63
Minimum 62.25 60.38 62.25 63.88 61.38 64.13 68.63
Maximum 99.25 98.38 97.25 96.88 95.38 94.13 98.63
Press ENTER to continue . .
C:>_
An simplified pseudo code for the driver class (letterGrader.java), the application, may look like this:
public class letterGrader { public static void main (String args[]) {
Grader grader = new Grader(args[0], args[1]);
grader.readScore(); // reads score and stores the data in member variables
grader.calcGrade();// determines letter grade and stores information
grader.printGrade();//writes the grade in output file
grader.displayAverages();//displays the averages in console
grader.doCleanup(); //use it to close files and other resources
}
}
Explanation / Answer
// TestLetterGrader.java
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
class LetterGrader {
private String inputFileName;
private String outputFileName;
private ArrayList<String> studentName;
private ArrayList<Double> quiz1;
private ArrayList<Double> quiz2;
private ArrayList<Double> quiz3;
private ArrayList<Double> quiz4;
private ArrayList<Double> midterm1;
private ArrayList<Double> midterm2;
private ArrayList<Double> finalExam;
private ArrayList<String> grade;
LetterGrader(String inputFileName, String outputFileName) {
this.inputFileName = inputFileName;
this.outputFileName = outputFileName;
studentName = new ArrayList<String>();
quiz1 = new ArrayList<Double>();
quiz2 = new ArrayList<Double>();
quiz3 = new ArrayList<Double>();
quiz4 = new ArrayList<Double>();
midterm1 = new ArrayList<Double>();
midterm2 = new ArrayList<Double>();
finalExam = new ArrayList<Double>();
grade = new ArrayList<String>();
}
//reed
public void readScore() {
//we have created text input file in the final Project folder manually
File inputFile = new File(inputFileName);
if (!inputFile.exists()) {
System.out.println("Input file " + inputFileName + " does not exist");
System.exit(2);
}
try {
Scanner input = new Scanner(inputFile);
while (input.hasNextLine()) {
String line = input.nextLine();
// display the whole line on screen
//System.out.println("Student #: " + i + " " + line);
// this line has one students data, use split
String[] studentInfo = line.split(",");
studentName.add(studentInfo[0]);
quiz1.add(Double.parseDouble(studentInfo[1]));
quiz2.add(Double.parseDouble(studentInfo[2]));
quiz3.add(Double.parseDouble(studentInfo[3]));
quiz4.add(Double.parseDouble(studentInfo[4]));
midterm1.add(Double.parseDouble(studentInfo[5]));
midterm2.add(Double.parseDouble(studentInfo[6]));
finalExam.add(Double.parseDouble(studentInfo[7]));
}
input.close();
} catch (IOException e) {
System.out.println("Error reading from input file: " + e);
}
}
//calculate
public void calcLetterGrade() {
for (int i = 0; i < studentName.size(); i++) {
double totalScore = quiz1.get(i) * 0.10 + quiz2.get(i) * 0.10 +
quiz3.get(i) * 0.10 + quiz4.get(i) * 0.10 +
midterm1.get(i) * 0.20 + midterm2.get(i) * 0.15 +
finalExam.get(i) * 0.25;
if (totalScore >= 90) {
grade.add("A");
} else if (totalScore >= 80) {
grade.add("B");
} else if (totalScore >= 70) {
grade.add("C");
} else if (totalScore >= 60) {
grade.add("D");
} else {
grade.add("F");
}
}
}
//sort by student name
private void sortByNames(){
for (int i = 0; i < studentName.size(); i++)
{
for (int j = i + 1; j < studentName.size(); j++)
{
if (studentName.get(i).compareTo(studentName.get(j))>0)
{
String temp = studentName.get(i);
studentName.set(i,studentName.get(j));
studentName.set(j,temp);
//swap scores
temp = grade.get(i);
grade.set(i,grade.get(j));
grade.set(j,temp);
}
}
}
}
//The output file will have grade information of all students whose information was read from
//input file, in a sorted order
public void printGrade() {
sortByNames();
File outputFile = new File(outputFileName);
try {
// output file is created and data is written on it
PrintWriter output = new PrintWriter(outputFile);
output.printf("Letter grade for %d students given in '%s' file is: ",
studentName.size(), inputFileName);
output.println("");
for (int i = 0; i < studentName.size(); i++) {
output.println(studentName.get(i) + ": "+ grade.get(i));
}
output.close(); // close o/p file
}catch (IOException e) {
System.out.println("Error writing on outputfile"+ e);
}
}
//calculate average minimum and maximum value
private double[] calcListStatistic(ArrayList<Double> list) {
double sum = 0;
double minimum = list.get(0);
double maximum = list.get(0);
double[] ret = new double[3];
for (int i = 0; i < list.size(); i++) {
//find Average
sum += list.get(i);
// FIND MINIMUM
if(list.get(i)< minimum){
minimum = list.get(i);
}
// FIND MAXIMUM
if(list.get(0) > maximum){
maximum = list.get(i);
}
}
double average = sum/list.size();
//return average, minimum, maximum
ret[0] = average;
ret[1] = minimum;
ret[2] = maximum;
return ret;
}
//display average minimum and maximum
public void displayAverages() {
double[] quiz1Stats = calcListStatistic(quiz1);
double[] quiz2Stats = calcListStatistic(quiz2);
double[] quiz3Stats = calcListStatistic(quiz3);
double[] quiz4Stats = calcListStatistic(quiz4);
double[] midterm1Stats = calcListStatistic(midterm1);
double[] midterm2Stats = calcListStatistic(midterm2);
double[] finalExamStats = calcListStatistic(finalExam);
System.out.println(" Letter grade has been calculated for students listed in"
+ " input file "+inputFileName+" and written to output file "+outputFileName);
System.out.println(" Here is the class Statistic:");
System.out.println(" quiz1 quiz2 quiz3 quiz4 midt1 midt2 final ");
System.out.println("Average: "+quiz1Stats[0]+" "+quiz2Stats[0]+" "+quiz3Stats[0]+" "+quiz4Stats[0]+
" "+midterm1Stats[0]+" "+midterm2Stats[0]+" "+finalExamStats[0]);
System.out.println("Minimum: "+quiz1Stats[1]+" "+quiz2Stats[1]+" "+quiz3Stats[1]+" "+quiz4Stats[1]+
" "+midterm1Stats[1]+" "+midterm2Stats[1]+" "+finalExamStats[1]);
System.out.println("Maximum: "+quiz1Stats[2]+" "+quiz2Stats[2]+" "+quiz3Stats[2]+" "+quiz4Stats[2]+
" "+midterm1Stats[2]+" "+midterm2Stats[2]+" "+finalExamStats[2]);
}
public void doCleanup() {
//for exiting after enter key is pressed(according to Example Run1)
//exception are handled in there respective functions
Scanner input = new Scanner(System.in);
System.out.println(" Press enter key to continue...");
input.nextLine();
input.close();
}
}
public class TestLetterGrader {
public static void main (String args[]) {
if (args.length < 2) {
System.out.println("USAGE: java TestLetterGrader <input_file_name> <output_file_name>");
System.exit(1);
}
LetterGrader letterGrader = new LetterGrader(args[0], args[1]);
letterGrader.readScore(); //reads score and stores the data in member variables
letterGrader.calcLetterGrade(); //determines letter grade and stores information
letterGrader.printGrade(); //writes the grade in output file
letterGrader.displayAverages();//displays the averages in console
letterGrader.doCleanup(); //use it to close files and other resources
}
}
================================================================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.