import java.io.*; //import everything from java.io
import java.util.*; //import everything from java.util
import java.text.*; //import everything from java.text
class Main {
public static void main(String[] args) {
//empty arrays for data from the file
String [] students = new String [10];
int [][] grades = new int [10][10];
System.out.println("Mr. Rillett's Class Report");
System.out.println("====================================");
System.out.println("Student Name: \tTest Marks: ");
//try and catch scenarios
try{
Scanner s = new Scanner(new File("StudentData.txt"));
while(s.hasNextLine()){
for(int i=0;i<6;i++){ //6 students = 6 repeats
students[i]=s.nextLine();
for(int g=0; g<4; g++){//4 grades = 4 repeat
grades[i][g] = Integer.parseInt(s.nextLine());
}
}
}
s.close();
}
catch (FileNotFoundException e){
System.out.println("No File Found!");
}
catch (NoSuchElementException n){
System.out.println("No Elements Found!");
}
listGrades(students, grades);
System.out.println("\n====================================");
System.out.println("Student Name: \tStudent Average: ");
studentAverage(students, grades);
System.out.print("\n====================================");
courseAverage(students, grades);
System.out.print("\n====================================");
addStudent(students, grades);
}
public static void listGrades(String[]nameList, int[][]gradeList){ //list student's names and grades
for(int i=0;i<6; i++){ //6 repeats = 6 students
System.out.print("\n"+nameList[i]+" =====>\t");
for(int gradesInRow=0; gradesInRow<4; gradesInRow++){ // 4 repeats = 4 grades
System.out.print(gradeList[i][gradesInRow]+" ");
}
}
}
public static void courseAverage(String[]nameList, int[][]gradeList){ // calculate the course average
DecimalFormat x = new DecimalFormat ("##.##");
double courseAvg = 0;
int a = 24; //number of students * 4 grades
for(int i=0;i<6; i++){// 6 students = 6 repeats
for(int gradesInRow=0; gradesInRow<4; gradesInRow++){
courseAvg += (gradeList[i][gradesInRow]); // adding the students grades
}
}
System.out.print("\nCourse average is: " + x.format(courseAvg/a)); //dividing the course average by number of student *4
}
public static void addStudent(String[]nameList, int[][]gradeList){ // add a new student and find their average
try{
Scanner s = new Scanner(new File("NewStudent.txt")); // scan the NewStudent file
while(s.hasNextLine()){
for(int i = 0; i<1; i++){
nameList[i]= s.nextLine();
for(int j=0;j<4;j++){
gradeList[i][j] = Integer.parseInt(s.nextLine());// parses string to int and adds grades to the int array
}
}
}
s.close();
}
catch (FileNotFoundException f)
{
System.out.println("No File Found!");
}
double average = 0;
System.out.println("\nStudent name:\tTest marks: ");
for (int x = 0; x<1; x++)
{
System.out.print(nameList[x] + "=====> ");// prints student name
for (int y = 0; y<4; y++){// 4 grades = 4 repeats
System.out.print(gradeList[x][y] + " "); // prints students grades
}
for(int m=0; m<4; m++){
average += (gradeList[0][m]);
}
System.out.print("\nAverage of the new student: " + (average/4));
}
}
public static void studentAverage(String[]nameList, int[][]gradeList)// adds students grades and divides by 4 to find individual averages
{
double average0 = 0;
double average1 = 0;
double average2 = 0;
double average3 = 0;
double average4 = 0;
double average5 = 0;
//calculate each student average and print it out
System.out.print("\n" + nameList[0]+" =====>\t");
for(int i=-1;i<0; i++){
for(int gradesInRow=0; gradesInRow<4; gradesInRow++){
average0 += (gradeList[0][gradesInRow]);
}
}
System.out.print(average0/4);
System.out.print("\n" + nameList[1]+" =====>\t");
for(int i=0;i<1; i++){
for(int gradesInRow=0; gradesInRow<4; gradesInRow++){
average1 += (gradeList[1][gradesInRow]);
}
}
System.out.print(average1/4);
System.out.print("\n" + nameList[2]+" =====>\t");
for(int i=1;i<2; i++){
for(int gradesInRow=0; gradesInRow<4; gradesInRow++){
average2 += (gradeList[2][gradesInRow]);
}
}
System.out.print(average2/4);
System.out.print("\n" + nameList[3]+" =====>\t");
for(int i=2;i<3; i++){
for(int gradesInRow=0; gradesInRow<4; gradesInRow++){
average3 += (gradeList[3][gradesInRow]);
}
}
System.out.print(average3/4);
System.out.print("\n" + nameList[4]+" =====>\t");
for(int i=3;i<4; i++){
for(int gradesInRow=0; gradesInRow<4; gradesInRow++){
average4 += (gradeList[4][gradesInRow]);
}
}
System.out.print(average4/4);
System.out.print("\n" + nameList[5]+" =====>\t");
for(int i=4;i<5; i++){
for(int gradesInRow=0; gradesInRow<4; gradesInRow++){
average5 += (gradeList[5][gradesInRow]);
}
}
System.out.print(average5/4);
}
}
Mohammad
98
90
95
92
Mahmoud
85
76
48
80
Adam
95
77
65
88
Nicole
85
90
80
75
Mary
85
95
90
96
Stacey
65
75
89
95
Hai Binh
87
92
86
87