-
Notifications
You must be signed in to change notification settings - Fork 0
/
GradeBook.java
105 lines (90 loc) · 2.9 KB
/
GradeBook.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Write a java app for the following output. the second line
* in the output "CS101 Introduction to Java Programming!" is
* passed from the constructor. take two dimension array to
* print the student numbers and their respective grades. Write
* functions for calculating average marks, displaying the
* lowest and highest grades. Use inheritance if necessary.
*/
// Grade Book Class.
public class GradeBook
{
// Title of the grade book.
private final String title;
// array for holding students grades.
private int[][] studentsGrades;
// array for holding average of grades.
private final double[] average = new double[10];
// parametrized constructor.
public GradeBook(String title)
{
this.title = title;
}
// method for getting grades input.
private void getGrades()
{
/* here I am hardcoded the array. if you want to get input from user you may use loop like this:
// Scanner sc = new Scanner(System.in);
// for (int i = 0; i < 10; i++)
// {
// for (int j = 0; j < 3; j++)
// {
// studentsGrades[i][j] = sc.nextInt();
// }
} */
studentsGrades = new int[][]
{
{87, 96, 70},
{68, 87, 90},
{94, 100, 90},
{100, 81, 82},
{83, 65, 85},
{78, 87, 65},
{85, 75, 83},
{91, 94, 100},
{76, 72, 84},
{87, 93, 73}
};
}
// method for calculating the average of mark of student.
private void calculateAverage()
{
for (int i = 0; i < studentsGrades.length; i++)
{
double avg = 0.0;
for (int j = 0; j < 3; j++)
{
avg += studentsGrades[i][j];
}
avg /= 3;
average[i] = avg;
}
}
// Printing the grades on console.
public void printGrades()
{
System.out.println("Welcome to the grade book for");
System.out.println(title);
System.out.println("\nThe grades are : ");
System.out.printf("%20s %10s %10s %10s \n", "Test1", "Test2", "Test3", "Average");
for (int i = 0; i < studentsGrades.length; i++)
{
System.out.printf("Student %02d", (i + 1));
for (int j = 0; j < 3; j++)
{
System.out.printf("%10s", studentsGrades[i][j]);
}
// precise up-to 2 decimal point
System.out.printf("%12.2f \n", average[i]);
}
}
public static void main(String[] args)
{
// Creating the grade book object.
GradeBook gradeBook = new GradeBook("CS101 Introduction to Java Programming!");
// calling required methods.
gradeBook.getGrades();
gradeBook.calculateAverage();
gradeBook.printGrades();
}
}