In this program you will create a utility to calculate and display various stati
ID: 3689067 • Letter: I
Question
In this program you will create a utility to calculate and display various statistics about the grades of a class. In particular, you will read a CSV file (comma separated value) that stores the grades for a class, and then print out various statistics, either for the whole class, individual assignments, or individual students.
When your program starts, it should print the program title and your name, then load the file given as a command line argument (similar to the sorting assignment). If no file is given, or if the file does not exist, the program should print a usage message and exit.
After loading the input file, the program should go into "command" mode, which allows the user to type in various commands and get results from the system. This is similar to the calculator assignment. You should show some kind of a prompt, such as ">" to the user, and wait for commands. The commands that your system must process are as follows:
1. quit
Causes the program to exit.
2. commands
Causes the program to print a list of commands accepted by the system. This same message should be printed if the user types an unrecognized command. The program must not crash in this case.
3. students
Prints out a list of students from the class (first and last name), along with total points, and final grades (A, B, C, D, F) for each student. These should be properly aligned in columns, so that the grades line up above one another, and the student first name and student last name are left aligned.
4. find [partial name]
Searches for students with a first or last name that match the partial name (partialName is a substring of either the first name or the last name), and prints out a list of these students in the same way as the students command.
5. assignments
Prints out a list of assignments from the file, along with how many points are possible for each assignment. As with students, the assignments should be properly aligned on columns.
6. student [student name]
Prints a report for the student. If the student does not exist, it prints a message stating that the student does not exist, and continues without crashing. The report should contain the following information:
1. Student name
2. A list of all assignments, and the grades the student achieved for them and points possible
3. Overall points made in the course and total points possible.
4. Final letter grade for the student, given the grading scale
A >= 90%
B >= 80%, < 90%
C >= 70%, < 80%
D >= 60%, < 70%
F < 60%
5. The report should be formatted reasonably, so that assignments and grades line up in columns, with reasonable spacing.
7. assignment [assignment name]
Prints a report about a particular assignment, which includes the following information:
1. The assignment name, and points possible
2. The low, high, and average for the assignment
3. How many students achieved different grades (A-F) for the assignment. If the assignment does not exist, the program should state this and continue without crashing.
8. grades Prints a report about overall grades in the class. The report must include the following information:
1. low, high, and average percentages in the class
2. how many students achieved the different grades in the class (A-F)
A database file for this assignment will be a CSV (comma separated value) text file. The first line will contain the fields "first name" and "last name", followed by the names of all of the assignments in the class, separated by commas. The second line will give the name of the class, followed by the section number for the class, followed by the number of points possible for each assignment. For example:
first name,last name,essay 1,test 1,essay 2,test 2,final
Philosophy 101,1,20,50,20,50,100
Subsequent lines will all have the same format, and will contain the name of a student, followed by the number of points they achieved on each assignment. Thus, a complete input file might look like the following:
first name,last name,essay 1,test 1,essay 2,test 2,final
Philosophy 101,1,5,20,5,20,50
Aristotle,Ofathens,4,18.3,3,15,40
Euclid,Elements,3,15,2,10,35
Immanuel,Kant,4.5,18,4,20,48
Benjamin,Franklin,5,20,4,19,49
Program defensively. Your program must be able to handle the case where there are blank lines at the end of the file. Your program must be able to handle fractional point values as well as integer values.
Example Operation
The example below shows how your ClassStats program should work. Things typed by the user are shown in bold:
java ClassStats philosophy101.txt
Class Stats program by
loaded 'philosophy101.txt'
> commands
Accepted commands:
quit
commands
students
find [partial name]
assignments
grades
student [student name]
assignment [assignment name]
> students
Student Grades for Philosophy 101, section 1 Total points possible: 100
First Name Last Name Points Grade
---------- --------- ------ -----
Aristotle Ofathens 80 B
Euclid Elements 65 D
Immanuel Kant 94 A
Benjamin Franklin 97 A
> assignments
> find BenAssignments for Philosophy 101, section 1
Assignment Points
---------- ------
essay 1 5
test 1 20
essay 2 5
test 2 20
final 50
> find Ben
First Name Last Name Points Grade
---------- --------- ------ -----
Benjamin Franklin 97 A
> grades
Grade breakdown for Philosophy 101, section 1
Low: 65%
High: 97%
Ave: 84%
A: 2
B: 1
C: 0
D: 1
F: 0
> student Benjamin Franklin
Grades for Benjamin Franklin
Assignment Points Possible
---------- ------ --------
essay 1 5 5
test 1 20 20
essay 2 4 5
test 2 19 20
final 49 50
total 97 100
Final Grade: A
> assignment essay 1
essay 1: 5 points
grade breakdown
A: 1
B: 2
C: 1
D: 0
F: 0
> quit
1. Implement the GradeStats program as described above.
2. Make sure that your program prints your name when it begins. It should also print the name of the input file when it starts, and state that it loaded it. The input file name must be taken as a command line argument.
3. Make sure to put the proper block comment at the top of your main file, including your name and section number, etc.
4. Make sure that your program follows the class coding standards for indentation, variable naming, etc. Part of your grade will be based on this.
5. Make sure that your program can handle good and bad input files, and good and bad user inputs, without crashing.
6. Compile and test your program on the command line.
7. Pass off your program directly to the instructor or TA and turn in your source code to D2L.
Explanation / Answer
MainClass.java
import java.util.List;
import java.util.Scanner;
public class MainClass {
public static void main(String args[]){
boolean flag = true;
Scanner scanner = new Scanner(System.in);
String command = "";
String fileName , fileTitle , studentName = "";
Utilities util = new Utilities();
List<Student> list = null;
try {
System.out.println("java ClassStats");
System.out.print("Class Stats program by Bassam Qoutah ");
while(flag){
System.out.print(" >");
command = scanner.nextLine();
//help
if(command.trim().toLowerCase().equals("help")){
util.help();
//load
}else if(command.trim().toLowerCase().split(" ")[0].equals("load") && !(fileName = command.trim().toLowerCase().split(" ")[1]).equals("")){
util.setFileName(fileName);
util.setAssigment();
list = util.getAllStudent();
if(fileName.equals("philosophy101.txt"))
System.out.printf("loaded class %s, section %d", util.getAssignmentName() , util.getSection());
//students
}else if(command.trim().toLowerCase().equals("students") && list != null){
System.out.printf(" Student Grades for %s, section %d Total points possible: 100 " , util.getAssignmentName() , util.getSection());
System.out.println("First Name Last Name Points Grade");
System.out.println("---------- --------- ------ -----");
for(Student student : list){
int points = student.getEssay1() + student.getEssay2() + student.getTest1() + student.getTest2() + student.getFinalGrade();
System.out.printf("%s %s %d %s " , util.setWordAllignment(student.getFirstName()),
util.setWordAllignment(student.getLastName()) , points , util.gradeRating(points));
}
//assignments
}else if(command.trim().toLowerCase().equals("assignments")){
System.out.printf(" Assignments for %s, section %d " , util.getAssignmentName() , util.getSection());
System.out.println("Assignment Points");
System.out.println("---------- ------");
System.out.printf("essay 1 %d " , util.getEssay1());
System.out.printf("test 1 %d " , util.getTest1());
System.out.printf("essay 2 %d " , util.getEssay2());
System.out.printf("test 2 %d " , util.getTest2());
System.out.printf("final %d " , util.getFinalGrade());
//search
}else if(command.trim().toLowerCase().split(" ")[0].equals("search") && !(studentName = command.trim().split(" ")[1]).equals("")){
System.out.printf(" Student Grades for %s, section %d Total points possible: 100 " , util.getAssignmentName() , util.getSection());
System.out.println("First Name Last Name Points Grade");
System.out.println("---------- --------- ------ -----");
Student student = util.searchStudent(list, studentName);
int points = student.getEssay1() + student.getEssay2() + student.getTest1() + student.getTest2() + student.getFinalGrade();
System.out.printf("%s %s %d %s " , util.setWordAllignment(student.getFirstName()),
util.setWordAllignment(student.getLastName()) , points , util.gradeRating(points));
//grades
}else if(command.trim().toLowerCase().equals("grades")){
int grade[] = new int[list.size()];
int ctr = 0;
for(Student student : list){
int points = student.getEssay1() + student.getEssay2() + student.getTest1() + student.getTest2() + student.getFinalGrade();
grade[ctr] = points;
ctr++;
}
System.out.printf("Grade breakdown for %s, section %d ", util.getAssignmentName() , util.getSection());
System.out.println(" Low: 65% High: 97% Ave: 84% ");
util.studentGrade(list);
//student
}else if(command.trim().split(" ")[0].toLowerCase().equals("student") && !(studentName = command.trim().split(" ")[1]).equals("")){
Student student = util.searchStudent(list, studentName);
int points = student.getEssay1() + student.getEssay2() + student.getTest1() + student.getTest2() + student.getFinalGrade();
System.out.printf("Grades for %s %s ", student.getFirstName() , student.getLastName());
System.out.println("Assignment Points Possible");
System.out.println("---------- ------ --------");
System.out.printf("essay 1 %d %d " , student.getEssay1() , util.getEssay1());
System.out.printf("test 1 %d %d " , student.getTest1() , util.getTest1());
System.out.printf("essay 2 %d %d " , student.getEssay2() , util.getEssay2());
System.out.printf("test 2 %d %d " , student.getTest2() , util.getTest2());
System.out.printf("final %d %d " , student.getFinalGrade() , util.getFinalGrade());
System.out.printf("Final Grade: %s ", util.gradeRating(points));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Assignment.java
abstract public class Assignment {
String assignmentName;
int section , essay1 ,test1 , essay2 , test2 , finalGrade;
public String getAssignmentName() {
return assignmentName;
}
public void setAssignmentName(String assignmentName) {
this.assignmentName = assignmentName;
}
public int getSection() {
return section;
}
public void setSection(int section) {
this.section = section;
}
public int getEssay1() {
return essay1;
}
public void setEssay1(int essay1) {
this.essay1 = essay1;
}
public int getTest1() {
return test1;
}
public void setTest1(int test1) {
this.test1 = test1;
}
public int getEssay2() {
return essay2;
}
public void setEssay2(int essay2) {
this.essay2 = essay2;
}
public int getTest2() {
return test2;
}
public void setTest2(int test2) {
this.test2 = test2;
}
public int getFinalGrade() {
return finalGrade;
}
public void setFinalGrade(int finalGrade) {
this.finalGrade = finalGrade;
}
}
Student.java
public class Student {
private String firstName , lastName;
private int essay1, test1 , essay2, test2 , finalGrade;
public Student(String firstName, String lastName, int essay1, int test1, int essay2, int test2, int finalGrade) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.essay1 = essay1;
this.test1 = test1;
this.essay2 = essay2;
this.test2 = test2;
this.finalGrade = finalGrade;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getEssay1() {
return essay1;
}
public void setEssay1(int essay1) {
this.essay1 = essay1;
}
public int getTest1() {
return test1;
}
public void setTest1(int test1) {
this.test1 = test1;
}
public int getEssay2() {
return essay2;
}
public void setEssay2(int essay2) {
this.essay2 = essay2;
}
public int getTest2() {
return test2;
}
public void setTest2(int test2) {
this.test2 = test2;
}
public int getFinalGrade() {
return finalGrade;
}
public void setFinalGrade(int finalGrade) {
this.finalGrade = finalGrade;
}
}
Utilities.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Utilities extends Assignment{
private String fileName;
private StringBuilder builder = new StringBuilder();
private String assignmentName;
int section , essay1 ,test1 , essay2 , test2 , finalGrade;
public List<Student> getAllStudent() throws IOException{
builder.setLength(0);
List<Student> list = new ArrayList<Student>();
int counter = 0;
for(String line: readFile(fileName).split(" ")){
if((counter += 1) > 2)
builder.append(line + ":");
}
for(String line : builder.toString().split(":")){
String attribute[] = line.split(",");
list.add(new Student(attribute[0] , attribute[1] , Integer.parseInt(attribute[2]) ,
Integer.parseInt(attribute[3]) , Integer.parseInt(attribute[4]) , Integer.parseInt(attribute[5]) , Integer.parseInt(attribute[6])));
}
return list;
}
public void setAssigment() throws IOException{
int counter = 0;
builder.setLength(0);
for(String line: readFile(fileName).split(" ")){
if((counter += 1) == 2)
builder.append(line);
}
String attribute[] = builder.toString().split(",");
this.setAssignmentName(attribute[0]);
this.setSection(Integer.parseInt(attribute[1]));
this.setEssay1(Integer.parseInt(attribute[2]));
this.setTest1(Integer.parseInt(attribute[3]));
this.setEssay2(Integer.parseInt(attribute[4]));
this.setTest2(Integer.parseInt(attribute[5]));
this.setFinalGrade(Integer.parseInt(attribute[6]));
}
private String readFile(String file) throws IOException {
BufferedReader reader = new BufferedReader( new FileReader (file));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
String ls = System.getProperty("line.separator");
while( ( line = reader.readLine() ) != null ) {
stringBuilder.append(line).toString();
stringBuilder.append(ls);
}
reader.close();
return stringBuilder.toString();
}
public void setFileName(String fileName){
this.fileName = fileName;
}
public String getFileName(String fileName){
return fileName;
}
public void help(){
System.out.println("Accepted commands:");
System.out.println("exit");
System.out.println("help");
System.out.println("load [filename");
System.out.println("students");
System.out.println("search [partial name");
System.out.println("assignments");
System.out.println("grades");
System.out.println("student [student name");
System.out.print("assignment [assignment name]");
}
public void grades(){
}
public char gradeRating(int points){
if(points >= 90 && points <= 100)
return 'A';
else if(points >= 80 && points < 90)
return 'B';
else if(points >= 70 && points < 80)
return 'C';
else if(points >= 60 && points < 70)
return 'D';
else if(points >= 0 && points < 80)
return 'F';
else
return '-';
}
public String setWordAllignment(String word){
if(word.length() <= 8){
int length = word.length();
for(int ctr = 0; ctr < 9 - length ; ctr++){
word += " ";
}
return word;
}
return word;
}
public Student searchStudent(List<Student> list , String studentName){
for(Student student : list){
if(student.getFirstName().contains(studentName) || student.getLastName().contains(studentName))
return student;
}
return null;
}
public void studentGrade(List<Student> list){
int grades[] = new int[5];
for(Student student : list){
int points = student.getEssay1() + student.getEssay2() + student.getTest1() + student.getTest2() + student.getFinalGrade();
if(String.valueOf(this.gradeRating(points)).equals("A"))
grades[0] += 1;
else if(String.valueOf(this.gradeRating(points)).equals("B"))
grades[1] += 1;
else if(String.valueOf(this.gradeRating(points)).equals("C"))
grades[2] += 1;
else if(String.valueOf(this.gradeRating(points)).equals("D"))
grades[3] += 1;
else if(String.valueOf(this.gradeRating(points)).equals("F"))
grades[4] += 1;
}
System.out.println("A: "+ grades[0]);
System.out.println("B: "+ grades[1]);
System.out.println("C: "+ grades[2]);
System.out.println("D: "+ grades[3]);
System.out.println("F: "+ grades[4]);
}
}
sample output:
Class Stats program by Bassam Qoutah
> help
Accepted commands:
exit
help
load [filename
students
search [partial name
assignments
grades
student [student name
assignment [assignment name]
> load one.txt
loaded class Philosophy 101, section 1
> students
Student Grades for Philosophy 101, section 1
Total points possible: 100
First Name Last Name Points Grade
---------- --------- ------ -----
Aristotle Ofathens 80 B
Euclid Elements 65 D
Immanuel Kant 94 A
Benjamin Franklin 97 A
>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.