/* Java program: Grade Book by PTL.
Version 007 - 01 November, 2018.
This Java program will allow an authorized user to enter student names and test
scores. It will calculate and display 4 test scores, min, max, average, and test grades, final grade,
and also number of A, B, C... for each student. It also calculates min,max, test average and
number of grades (A, B, C,..) for each test.
DESIGN:
Use 4 arrays : names[] to store the student names
table[][] to store 4 test scores, min max avarage for each student
grades[][] to store the grades of 4 tests for students
count[][] to store numbers of students graddes and test grades
Define the class Table with constructor Table() to initialize some variables.
This class also defines many methods to calculate min, max, averages, and numbers of A, B, C,...
Note: The program is applied for 3 students and 4 tests but it can be expanded for many test and students
by changing the indice of the arrays and the print-out messages.
*/
package gradebookmk2ple;
import java.util.Scanner;
/* Table class provides all methods to calculate Min, Max, Averages, Grades, and
numbers of the Grades for each student and also for each test
*/
class Table
{
int i, j, k;
int idx1 = 6, idx2 = 7;
int cntA, cntB, cntC, cntD, cntF;
double sum, classAve, testAve;
double min, max;
double scoresNum, studNum;
Table() // Use constructor to initialize some variables
{
cntA = 0;
cntB = 0;
cntC = 0;
cntD = 0;
cntF = 0;
scoresNum = 4;
studNum = 3;
}
char grades[][] = new char[3][5]; // Store the student grades
String names[] = new String[3]; // Store student names
int count[][] = new int[5][9]; // Store number of grades (A, B, C, D, or F) of students and the tests
double table[][] = new double[idx1][idx2]; // Array stores test scores, min, max, ave.
double studTbl[][] = new double[3][5]; // Array stores sorted numerical ordered scores for student medians
double tstTbl[][] = new double[4][4]; // Array stores sorted numerical ordered scores for test medians
Scanner scan = new Scanner(System.in); // Input data from console, keyborad.
void enterName() // Input student names
{
for (i = 0; i < 3; i++)
{
System.out.println("ENTER STUDENT NAME " + (i+1));
names[i] = scan.next();
}
}
void compuScores() // Input test scores and calculate the average for each student
{
sum = 0;
for (i = 0; i < (idx1-3); i++)
{
System.out.println("ENTER SCORES FOR STUDENT " + (i+1));
for (j = 0; j < (idx2-3); j++)
{
table[i][j] = scan.nextDouble();
sum = sum + table[i][j]; // Get sum of scores for each student
}
table[i][6] = sum / scoresNum; // Store Average of each student
sum = 0; // Reset sum for next student
//System.out.println(" Sum = " + sum + " Average = " + table[i][6]);
}
}
void compuStudentMinMax() // Calculate min (low), max (high) scores for each student
{
for (i = 0; i < (idx1-3); i++)
{
min = max = table[i][0]; // Init min, max
for (j = 0; j < (idx2-3); j++)
{
if (table[i][j] < min)
min = table[i][j];
if (table[i][j] > max)
max = table[i][j];
}
table[i][4] = min;
table[i][5] = max;
//System.out.println("min = " + table[i][3] + " max = " + table[i][4]);
}
}
void compuTestMinMax() // Calcualate min (low), max (high) of each test
{
for (j = 0; j < (idx2-3); j++)
{
min = max = table[0][j]; // Init min, max
for (i = 0; i < (idx1-3); i++)
{
if (table[i][j] < min)
min = table[i][j];
if (table[i][j] > max)
max = table[i][j];
}
table[3][j] = min;
table[4][j] = max;
//System.out.println("min = " + table[i][3] + " max = " + table[i][4]);
}
}
void compuGrade() // Calculate the grades of students
{
for (i = 0; i < (idx1-3); i++)
{ // Final grade for each student
if (table[i][6] > 89.5) grades[i][4] = 'A';
else if ((table[i][6] >= 79.5) & (table[i][6] < 89.5)) grades[i][4] = 'B';
else if ((table[i][6] >= 69.5) & (table[i][6] < 79.5)) grades[i][4] = 'C';
else if ((table[i][6] >= 59.5) & (table[i][6] < 69.5)) grades[i][4] = 'D';
else grades[i][4] = 'F';
}
for (i = 0; i < (idx1-3); i++)
{ // Test grades and humber of grades (A, B,..) for each student
for (j = 0; j < (idx2-3); j++)
{
if (table[i][j] > 89.5)
{
grades[i][j] = 'A';
cntA++;
count[i][0] = cntA;
}
else if ((table[i][j] >= 79.5) & (table[i][j] < 89.5))
{
grades[i][j] = 'B';
cntB++;
count[i][1] = cntB;
}
else if ((table[i][j] >= 69.5) & (table[i][j] < 79.5))
{
grades[i][j] = 'C';
cntC++;
count[i][2] = cntC;
}
else if ((table[i][j] >= 59.5) & (table[i][j] < 69.5))
{
grades[i][j] = 'D';
cntD++;
count[i][3] = cntD;
}
else
{
grades[i][j] = 'F';
cntF++;
count[i][4] = cntF;
}
}
cntA = cntB = cntC = cntD = cntF = 0;
}
for (j = 0; j < (idx2-3); j++)
{ // Number of grades (A, B,..) on each test
for (i = 0; i < (idx1-3); i++)
{ // Note: Can use the Switch staement here if time allows.
// if (grades[i][j] == 'A') // Use if else ladder
// {
// cntA++;
// count[0][j+5] = cntA;
// }
// else if (grades[i][j] == 'B')
// {
// cntB++;
// count[1][j+5] = cntB;
// }
// else if (grades[i][j] == 'C')
// {
// cntC++;
// count[2][j+5] = cntC;
// }
// else if (grades[i][j] == 'D')
// {
// cntD++;
// count[3][j+5] = cntD;
// }
// else
// {
// cntF++;
// count[4][j+5] = cntF;
// }
switch (grades[i][j]) // Use Switch statement
{
case 'A':
cntA++;
count[0][j+5] = cntA;
break;
case 'B':
cntB++;
count[1][j+5] = cntB;
break;
case 'C':
cntC++;
count[2][j+5] = cntC;
break;
case 'D':
cntD++;
count[3][j+5] = cntD;
break;
case 'F':
cntF++;
count[4][j+5] = cntF;
break;
default:
break;
}
}
cntA = cntB = cntC = cntD = cntF = 0;
}
}
double compuTestAverage() // Calculate Average of each test
{
for (j = 0; j < (idx2-3); j++)
{
testAve = 0;
for (i = 0; i < (idx1-3); i++)
testAve = testAve + table[i][j];
testAve = testAve / studNum;
table[5][j] = testAve;
testAve = 0;
}
return testAve;
}
double compuClassAverage() // Calcukate the average of the entire class
{
classAve = 0;
for (i = 0; i < (idx1-2); i++)
{
classAve = classAve + table[i][6];
}
classAve = classAve / studNum;
return classAve;
}
void compuStudentMedian()
{
double temp;
// Copy all scores from table[3][4] to studTbl[3][4]
for (i = 0; i < (idx1-3); i++)
{
for (j = 0; j < (idx2-3); j++)
{
studTbl[i][j] = table[i][j];
}
}
// Ascending buble sorting
for (i = 0; i < (idx1-3); i++)
{
for (j = 0; j < (idx2-3); j++)
{
for (k = (j+1); k < (idx2-3); k++)
{
if (studTbl[i][j] > studTbl[i][k])
{
temp = studTbl[i][j];
studTbl[i][j] = studTbl[i][k];
studTbl[i][k] = temp;
}
}
}
studTbl[i][4] = (studTbl[i][1] + studTbl[i][2]) / 2;
//System.out.println("studTbl[i][1]=" + studTbl[i][1] + " studTbl[i][2]=" + studTbl[i][2]);
}
}
void compuTestMedian()
{
double temp;
// Copy all scores from table[3][4] to tstTbl[3][4]
for (i = 0; i < (idx1-3); i++)
{
for (j = 0; j < (idx2-3); j++)
{
tstTbl[i][j] = table[i][j];
}
}
// Ascending buble sorting for test medians
for (j = 0; j < (idx2-3); j++)
{
for (i = 0; i < (idx1-3); i++)
{
for (k = (i+1); k < (idx1-3); k++)
{
if (tstTbl[i][j] > tstTbl[k][j])
{
temp = tstTbl[i][j];
tstTbl[i][j] = tstTbl[k][j];
tstTbl[k][j] = temp;
}
}
}
tstTbl[3][j] = tstTbl[1][j]; // test median values.
//System.out.println("studTbl[i][1]=" + studTbl[i][1] + " studTbl[i][2]=" + studTbl[i][2]);
}
}
}
public class Gradebookmk2ple
{
public static void main(String[] args)
{ /* The main method just calls class methods to display student names, test scores and obtain students'
min, max, averages, test grades, numbers of grades (A, B, C, D, or F).
It also calculates min, max, test average and number of grades for each test.*/
int i, j;
double classAverage;
Table student = new Table();
student.enterName();
student.compuScores();
student.compuStudentMinMax();
student.compuTestMinMax();
student.compuGrade();
student.compuTestAverage();
student.compuStudentMedian();
student.compuTestMedian();
classAverage = student.compuClassAverage();
System.out.println("\t");
System.out.println("TABLE OF TEST RESULTS");
// System.out.println("Students " + "Test1\t" + "Grade1\t" + "Test2\t" + "Grade2\t" + "Test3\t" + "Grade3\t" + "Test4\t" + "Grade4\t" + "Min\t" + "Max\t" + "Final Averages\t" + "Final Grades\t" + "Number of A\t" + "Number of B\t" +"Number of C\t" +"Number of D\t" +"Number of F\t");
//System.out.println("Students " + "Test1 " + "Grade1\t" + "Test2\t" + "Grade2\t" + "Test3\t" + "Grade3\t" + "Test4\t" + "Grade4\t" + "Final Grades");
System.out.println("Students " + "Test1 " + "Test2\t" + "Test3\t" + "Test4\t" + "Average\t" + "Median\t" + "High\t" + "Low\t" + "A\t" + "B\t" + "C\t" + "D\t" + "F\t");
for (i = 0; i < (student.idx1-3); i++)
{
//System.out.print(student.names[i] + "\t" + student.table[i][0] + "\t" + student.grades[i][0]+ "\t" + student.table[i][1] + "\t" + student.grades[i][1]+ "\t" + student.table[i][2] + "\t" + student.grades[i][2]+ "\t" + student.table[i][3] + "\t" + student.grades[i][3]+ "\t" + student.table[i][4] + "\t" + student.table[i][5] + "\t" + student.table[i][6] + "\t\t" + student.grades[i][4]);
//System.out.println("\t\t" + student.count[i][0] + "\t\t" + student.count[i][1] + "\t\t" + student.count[i][2] + "\t\t" + student.count[i][3] + "\t\t" + student.count[i][4]);
//System.out.println(student.names[i] + "\t " + student.table[i][0] + "\t " + student.grades[i][0]+ "\t" + student.table[i][1] + "\t" + student.grades[i][1]+ "\t" + student.table[i][2] + "\t" + student.grades[i][2]+ "\t" + student.table[i][3] + "\t" + student.grades[i][3]+ "\t" + student.grades[i][4]);
//System.out.println("\t " + "Min = " + student.table[i][4] + ", Max = " + student.table[i][5] + ", Final Average = " + student.table[i][6]);
//System.out.println("\t " + "Number of A = " + student.count[i][0] + ", Number of B = " + student.count[i][1] + ", Number of C = " + student.count[i][2] + ", Number of D = " + student.count[i][3] + ", Number of F = " + student.count[i][4]);
System.out.println(student.names[i] + "\t " + student.table[i][0] + "\t " + student.table[i][1] + "\t" + student.table[i][2] + "\t" + student.table[i][3] + "\t" + student.table[i][6]+ "\t" + student.studTbl[i][4] + "\t" + student.table[i][5] + "\t" + student.table[i][4] + "\t" + student.count[i][0] + "\t" + student.count[i][1] + "\t" + student.count[i][2]+ "\t" + student.count[i][3]+ "\t" + student.count[i][4]);
}
//System.out.println(" ");
//System.out.println("studTbl[0][0]=" + student.studTbl[0][0] + " studTbl[0][1]=" + student.studTbl[0][1]);
//System.out.println("studTbl[0][2]=" + student.studTbl[0][2] + " studTbl[0][3]=" + student.studTbl[0][3]);
//System.out.println("table[0][1]=" + student.table[0][1] + " table[0][2]=" + student.table[0][2]);
//System.out.println("median studTbl[0][4]=" + student.studTbl[0][4]);
//System.out.println();
//for (i=0; i<3; i++) // List content of studTbl[3][4]
//{
// for (j=0; j<4; j++)
// {
// System.out.print(student.studTbl[i][j] + ",");
// }
// System.out.println();
//}
System.out.println();
System.out.print("Average\t");
for (j = 0; j < (student.idx2-3); j++)
{
//System.out.println(" Test" + (j+1) + " Min = " + student.table[3][j] + ", Max = " + student.table[4][j] + ", Test Average = " + student.table[5][j]);
//System.out.println("\t" + "Number of A = " + student.count[0][j+5] + ", Number of B = " + student.count[1][j+5] + ", Number of C = " + student.count[2][j+5] + ", Number of D = " + student.count[3][j+5] + ", Number of F = " + student.count[4][j+5]);
System.out.print(" " + student.table[5][j] + "\t");
}
System.out.println(" ");
System.out.print("Median\t");
for (j = 0; j < (student.idx2-3); j++)
{
System.out.print(" " + student.tstTbl[3][j] + "\t");
}
System.out.println(" ");
System.out.print("High\t");
for (j = 0; j < (student.idx2-3); j++)
{
System.out.print(" " + student.table[4][j] + "\t");
}
System.out.println(" ");
System.out.print("Low\t");
for (j = 0; j < (student.idx2-3); j++)
{
System.out.print(" " + student.table[3][j] + "\t");
}
System.out.println(" ");
System.out.print("A's\t ");
for (j = 5; j < (student.idx2+2); j++)
System.out.print( + student.count[0][j] + "\t ");
System.out.println();
System.out.print("B's\t ");
for (j = 5; j < (student.idx2+2); j++)
System.out.print(student.count[1][j] + "\t ");
System.out.println();
System.out.print("C's\t ");
for (j = 5; j < (student.idx2+2); j++)
System.out.print(student.count[2][j] + "\t ");
System.out.println();
System.out.print("D's\t ");
for (j = 5; j < (student.idx2+2); j++)
System.out.print(student.count[3][j] + "\t ");
System.out.println();
System.out.print("F's\t ");
for (j = 5; j < (student.idx2+2); j++)
System.out.print(student.count[4][j] + "\t ");
System.out.println();
System.out.println(" Class Average = " + classAverage);
}
}
No comments:
Post a Comment