Determine Letter Grades - JAVA Summary: Write a program that will determine the
ID: 3738284 • Letter: D
Question
Determine Letter Grades - JAVA
Summary: Write a program that will determine the letter grade of students of a class (university class, not java class) using your understanding of class (java class), objects, exception handling, and collection in Java.
Description: Your program will need to accept two command line arguments. The first argument will be the name of a disk file that contains the names of students, and their test scores, separated by commas followed by one or more spaces. Each line in the file will contain scores for one student. The second 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). The format of the data in the input file is fixed, however the number of students in the input file is unknown during compile time. The name of input and output files could be anything and only known during run time. Besides writing to the output file, you will also need to display the averages of scores along with minimum and maximum scores for each test in the screen/console.
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 + midi * .20 + midii * .15 + final * .25 Determination of letter grade is according to the following logic:
Final Score >= 90% then letter grade is A, 80%-89% B, 70%-79% C, 60-69% D, <= 59% F
Sample input data file: input_data.txt (the input data format is: name, quiz1, quiz2, quiz3, quiz4, midi, midii, final,e.g.:
..
<>
Thui Bhu, 100, 90, 80, 100, 89, 99, 88
Ariana B. Smith, 90, 90, 100, 100, 99, 100, 95
Emily Gonzales, 100, 90, 100, 70, 78, 78, 80
Jennifer L, 80, 90, 90, 100, 89, 99, 85
Maria Jones, Bill Gates, Escobar Morris, Anne Latner,
65, 72, 77, 68, 62, 70, 65 60, 54, 38, 62, 65, 60, 50 83, 77, 88, 76, 79, 72, 76 80, 80, 85, 95, 90, 95, 90Thui Bhu, 100, 90, 80, 100, 89, 99, 88
==================================================================
Ariana B. Smith, 90, 90, 100, 100, 99, 100, 95
Emily Gonzales, 100, 90, 100, 70, 78, 78, 80
Jennifer L, 80, 90, 90, 100, 89, 99, 85
Maria Jones, 65, 72, 77, 68, 62, 70, 65
Bill Gates, 60, 54, 38, 62, 65, 60, 50
Escobar Morris, 83, 77, 88, 76, 79, 72, 76
Anne Latner, 80, 80, 85, 95, 90, 95, 90
==============================================================================
(The input file may have extra white space or have ".", after the middle intital. You may have to parse the data or how you see fit. )
Note about input file: You can use the above data as template to create your input file. You can use any text editor of your choice including IDE. You can assume the data will be correctly formatted as described above when I test your program, however, I will have my own input file, with different name and different number of students. You should keep the input file in your project’s default folder when testing using IDE. Refer to the separate document provided which shows you how to set the command line arguments to pass the input and output file names to your program.
Sample output file: output_data.txt (the output format is: Name: letter grade, sorted by name), e.g. Letter grade for 8 students given in input_data.txt file is:
...
<>
Sample Run of the program (name of the application is TestLetterGrader): Remember that 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 you could also run this from your IDE (e.g. eclipse). You just need to provide these arguments in the class properties.
Example Run 1:
C:>java TestLetterGrader input_data.txt output_data.txt
Letter grade has been calculated for students listed in input file input_data.txt and written to output file output_data.txt
Anne Latner: B Ariana B. Smith: A Bill Gates: F Emily Gonzales: B Escobar Morris: C Jennifer L: B Maria Jones: D Thui Bhu: A
Here is the class averages:
Q1 Q2
Q3 Q4 82.25 83.88 38 62
100 100
MidI MidII
81.38 84.13 78.63
62 60 50 99 100 95
Average:
Minimum: 60 Maximum: 100
Press ENTER to continue . . .
C:>_
82.25 80.38 54 90
Final
Sample Run 2:
C:>java TestLetterGrader data1.txt data2.txt
Letter grade has been calculated for students listed in input file data1.txt and written to output file data2.txt
Here is the class averages: Q1
Q2 Q3 Q4 80.38 82.25 83.88 54 38 62
90 100 100
Mid1 Mid2
81.38 84.13 78.63
62 60 50 99 100 95
Average:
Minimum: 60 Maximum: 100
Press ENTER to continue . . .
C:>_
82.25
Score: Maximum score you can receive for this final project is 105 and it is divided into the following categories:
1) 2) 3)
Program compiles, runs, and provides the correct answers in the format as shown on screen and on disk. (70%)
The application design is modular and has good choices of classes and methods with proper data encapsulation. (20%)
Proper error checking, exception handling and descriptive comments are used (10%)
Design Suggestion: You need to model this problem into a software application – TestLetterGrader (driver class). Your final program should be implemented as Object Oriented and are instantiated using new. The driver class, TestLetterGrader, uses your main class, LetterGrader , which really determines the grade – meaning LetterGrader has all the code to determine the grader, TestLetterGrader simply uses the LetterGrader.
You need to identify classes, their hierarchies (inheritance and/or interface) to be used. The driver class will simply parse the command line arguments and use the main class to read the student’s scores, do the calculation, output the result and close the files. Only TestLetterGrader and LetterGrader class are needed to be public classes. You will need only one main method in the application and that should be in the TestLetterGrader class only. You can have a main in LetterGrader for testing purposes, but, it will not be used when your application, the driver class, TestLettergrader is run.
Final
An simplified pseudo code for the driver class (TestLettergrader.java), your application, may look like this:
public class TestLetterGrader {
public static void main (String args[]) {
//test if there are two valid arguments then, create the object //if not give right message and exit
LetterGrader letterGrader = new LetterGrader(args[0], args[1]);
//LetterGrader is your main class,
letterGrader.readScore(); letterGrader.calcLetterGrade(); letterGrader.printGrade(); letterGrader.displayAverages(); letterGrader.doCleanup();
//args[0] has input file name, and
//args[1] has output file name
//reads score and stores the data in member variables
//determines letter grade and stores information //writes the grade in output file
//displays the averages in console
//use it to close files and other resources
//remember you need to take care of any errors or exceptions // in any of these activities must be taken care of
} }
Submission requirement: Submit your source code, all java files (*.java), You DO NOT need to submit anything else. Naming requirements, especially for public items:
There will be no more that 100 students. Last name will only not have more than 60 characters. First name will not have more than 40 characters.
Important Class: The public class, which does all the work, reading input data, calculating the grade averages and printing data in disk file and console – LetterGrader.java.
Interface (is optional): IGrader.java
Package (is optional): utility
Private Items: You can choose any names you want for private classes which does not affect file names. You can add all private classes in your main class, LetterGrader.java (yes you can have multiple classes in one .java file as long as there is only one public class in that file). If you are planning to inherit from a class of your own to your public class, in that case, leave the access modifier of parent class to nothing (that makes it a package access as you can’t inherit from a private class).
Driver Class (your application): The public class which acts like a driver program is really the application name – TestLetterGrader.java.
This criteria compiles, runs, and provides the correct answers in the format as shown on screen program compiles, runs, and provides the correct answers
1. Program compiles without errors, runs without run-time errors, provides correct output on screen and in disk file as well. The output is well formatted as shown in the requirements document.
2. Classes are designed following what was covered in lectures with the use of classes, inheritance, and/or interface. All instance variables are declared private, methods are private as well with only few declared public.
3. Code is well formatted, commented. Understanding code is easy. It has good error trapping and proper error checking, exception handling and descriptive comments are used.
Explanation / Answer
import java.io.PrintWriter;
public class TestLetterGrader {
public static void main(String[] args) {
LetterGrader letterGrader = new LetterGrader(args[0], args[1]);
String [] newArray= letterGrader.readScore(args[0]);
// Get the number of rows in the array
final int COUNT = newArray.length;
String [] s = letterGrader.calcLetterGrade(newArray, COUNT, args[0], args[1]);
PrintWriter textPrintStream= letterGrader.displayAverages(s, COUNT);
letterGrader.doCleanup(textPrintStream, args[1]);
}
}
------------------------------------------------------------------------------------------------------------
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class LetterGrader implements IGrader {
// Since we know there are 8 columns ...
int length = 8;
PrintWriter textPrintStream = null;
public LetterGrader(String string, String string2) {
}
public String[] readScore(String string) {
// read names and scores from input file
FileReader fileReader = null;
try {
fileReader = new FileReader(string);
} catch (FileNotFoundException e1) {
System.out.println("The file name you entered: " + string + " was not found");
System.exit(1);
}
BufferedReader finalInStream = new BufferedReader(fileReader);
// read each line of the file and store each line in newArray
String [] newArray = new String[length];
String s;
int stringLength = 0;
int i = 0;
try {
while ((s=finalInStream.readLine()) != null ) {
stringLength = stringLength + 1;
newArray[i] = s;
i = i+1;
}
} catch (IOException e) {
System.out.println("Error reading from file: " + string + e.getMessage());
e.printStackTrace();
}
try {
finalInStream.close();
} catch (IOException e) {
System.out.println("Error reading from file: " + string + e.getMessage());
e.printStackTrace();
}
return newArray;
}
public String[] calcLetterGrade(String[] s, int count, String string, String string2) {
// create arrays for student and letter.
String [] stud = new String [count];
char [] letter = new char [count];
// loop over array and calculate weighted final score.
for (int k = 0; k<= count -1; k++) {
String y[] = s[k].split(",");
double final_score = (Double.parseDouble(y[1]) * .10) + (Double.parseDouble(y[2]) * .10) + (Double.parseDouble(y[3]) * .10) + (Double.parseDouble(y[4]) * .10) + (Double.parseDouble(y[5]) * .20) + (Double.parseDouble(y[6]) * .15) + (Double.parseDouble(y[7]) * .25);
final_score = (double)Math.round(final_score * 100.00d)/100.00d;
String student = y[0];
// assign a letter grade to the score
char letterGrade = getLetterGrade(final_score);
// fill student and letter grade arrays
stud[k] = student;
letter[k] = letterGrade;
}
// print the grade
printGrade(string, string2, count, stud, letter);
// return the string array
return s;
}
private char getLetterGrade(double score) {
// take value of "score" and get the letter grade via if/else
if (score >=90.0)
return 'A';
else if (score >=80.0)
return 'B';
else if (score >= 70)
return 'C';
else if (score >= 60)
return 'D';
else
return 'F';
}
private void printGrade(String string, String string2, int count, String[] name, char [] letter) {
this.textPrintStream = null;
// find the longest name in array. We'll need it to calculate formating
int spacing = getLongestName(name);
// create a PrintWriter object from a FileOutputStream
try {
this.textPrintStream = new PrintWriter(new FileOutputStream(string2));
this.textPrintStream.append("Letter grade for " + count + " students given in input file " + string + " is: ");
// print the name and grade
for (int i = name.length-1; i >=0 ; i--) {
this.textPrintStream.print(String.format("%-" + spacing + "s%-" + spacing + "s ", name[i], letter[i]));
}
//String newLine = System.getProperty("line.separator");
//textPrintStream.write("This is new line " + newLine + "What is this");
} catch (FileNotFoundException e) {
System.out.println("Error opening the file " + string2 + " " + e.getMessage());
System.exit(0);
}
this.textPrintStream.close();
System.out.println("Letter grade has been calculated for " + name.length + " students listed in input file " + string + " and written to output file " + string2 + " ");
}
private int getLongestName(String[] nameArray) {
int spacingSeparation = 10;
String currentMax = nameArray[0];
int currentMaxIndex = 0;
for (int j = 0; j < nameArray.length - 1; j++) {
currentMax = nameArray[j];
currentMaxIndex = j;
for (int k = 1; k < nameArray.length; k++) {
if (currentMax.length() < nameArray[k].length()) {
currentMaxIndex = k;
}
}
}
int spacing = nameArray[currentMaxIndex].length() + spacingSeparation;
return spacing;
}
public PrintWriter displayAverages(String[] s, int COUNT) {
ArrayList<String[]> token_Array = new ArrayList<String[]>();
System.out.println("Here are the class averages: ");
System.out.println(" Q1 Q2 Q3 Q4 MidI MidII Final");
token_Array= getTokens(s);
getColumns(token_Array);
return textPrintStream;
}
private ArrayList<String[]> getTokens(String[] t) {
// tokenize the elements in each string row
int rows = t.length;
ArrayList<String[]> scores = new ArrayList<String[]>();
for (int j = 0; j< rows; j++) {
StringTokenizer parsewords = new StringTokenizer(t[j], ",");
String [] tokens = new String[parsewords.countTokens()];
int k = 0;
while(parsewords.hasMoreTokens()) {
tokens[k++] = parsewords.nextToken().trim();
}
// add rows as tokens to array scores
scores.add(tokens);
}
// return tokenized array
return scores;
}
private void getColumns(ArrayList<String[]> values) {
// for each type (average, min, max), get a column, get average, get max, get min
// create arraylist for each
ArrayList<Double> averages = new ArrayList<Double>();
ArrayList<Integer> mins = new ArrayList<Integer>();
ArrayList<Integer> maxs = new ArrayList<Integer>();
// for each column i,
for (int i = 1; i< values.get(0).length; i++ ) {
// collect row values for the column
ArrayList <Integer> temp = new ArrayList<Integer>();
for (int j = 0; j< values.size() ; j++ ) {
temp.add(Integer.valueOf(values.get(j)[i]));
}
// call get functions for averages, mins, maxes, and add values to arraylists
averages.add(getAverage(temp));
mins.add(getMin(temp));
maxs.add(getMax(temp));
}
// print averages, mins, maxes
System.out.print("Average:");
for (int k = 0; k < values.size()-1; k++) {
System.out.printf(" %5.2f", averages.get(k));
}
System.out.println();
System.out.print("Minimums:");
for (int k = 0; k < values.size()-1; k++) {
System.out.printf(" %d", mins.get(k));
}
System.out.println();
System.out.print("Maximums:");
for (int k = 0; k < values.size()-1; k++) {
System.out.printf(" %d", maxs.get(k));
}
System.out.println();
pressEnterKeyToContinue();
}
private double getAverage(ArrayList<Integer> colVals ) {
// calculate averages from column values (colVals) ArrayList
double temp = 0;
for (int i = 0; i < colVals.size(); i++) {
temp += colVals.get(i);
}
temp = temp/colVals.size();
return temp;
}
private int getMin(ArrayList<Integer> colVals) {
// calculate minimums from column values ArrayList
int minTemp = Collections.min(colVals);
return minTemp;
}
private int getMax(ArrayList<Integer> colVals) {
// calculate maximums from column values ArrayList
int maxTemp = Collections.max(colVals);
return maxTemp;
}
private void pressEnterKeyToContinue() {
System.out.println(" Press Enter key to continue... ");
try
{
System.in.read();
}
catch(Exception e)
{System.out.println("You must press Enter key to continue");}
}
public void doCleanup(PrintWriter textPrintStream, String outputFile) {
this.textPrintStream.close();
}
}
------------------------------------------------------------------------------------------------
import java.io.PrintWriter;
public interface IGrader {
public String[] readScore(String string);
public String[] calcLetterGrade(String[] s, int count, String string, String string2);
public PrintWriter displayAverages(String[] s, int COUNT);
public void doCleanup(PrintWriter textPrintStream, String outputFile);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.