-
Notifications
You must be signed in to change notification settings - Fork 0
/
GradeAverage1.java
154 lines (122 loc) · 6.98 KB
/
GradeAverage1.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Analysis:
Input: 10 pairs of grades and units
Processes:
Read the grades and units
Compute the sum of the products of grades and units
Compute the total of the units
Compute the average : (sum of products of grades and units)/total_units
Show the average
Give an appropriate message
Output:
Average, Message
Algorithm:
1. Let grade represent a grade for a subject
2. Let units represent number of units for a subject
3. Let totalGrade = 0 where totalGrade represents the sum of the products of grades and
corresponding units
4. Let totalUnits = 0 where totalUnits represents the total number of units
5. Let average represent the average grade of the student
6. Read the first grade
7. Read the number of units for the first grade
8. Add the product of first grade and the corresponding units to totalGrade
9. Add the units for the first grade to totalUnits
10. Repeat steps 6, 7, 8 and 9 for the 2nd to the 10th grade
11. Compute the average where average = totalGrades/totalUnits
12. Display the average
13. If (average >= 85) print "Congratulations! You belong to the dean's list";
14. If (average < 85) print "Sorry! You did not make it to the dean's list. Do better
next term. "
*/
package exercises.prelim;
import java.lang.*;
import java.util.Scanner;
public class GradeAverage1 {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in); // Create Scanner Object
int grade; // Declare variables and initialize totalGrade and totalUnits to 0
int units;
int totalGrade = 0;
int totalUnits = 0;
double average;
// Prompt the user for total grade and total number of units for 1st subject
System.out.print("Enter the grade of the student for the 1st subject: ");
grade = Integer.parseInt(kbd.nextLine());
System.out.print("Enter the number of units for the 1st subject: ");
units = Integer.parseInt(kbd.nextLine());
totalGrade = totalGrade + grade * units; // Updates the value of totalGrade and totalUnits
totalUnits += units;
// Prompt the user for total grade and total number of units for 2nd subject
System.out.print("Enter the grade of the student for the 2nd subject: ");
grade = Integer.parseInt(kbd.nextLine());
System.out.print("Enter the number of units for the 2nd subject: ");
units = Integer.parseInt(kbd.nextLine());
totalGrade = totalGrade + grade * units; // Updates the value of totalGrade and totalUnits
totalUnits += units;
// Prompt the user for total grade and total number of units for 3rd subject
System.out.print("Enter the grade of the student for the 3rd subject: ");
grade = Integer.parseInt(kbd.nextLine());
System.out.print("Enter the number of units for the 3rd subject: ");
units = Integer.parseInt(kbd.nextLine());
totalGrade = totalGrade + grade * units; // Updates the value of totalGrade and totalUnits
totalUnits += units;
// Prompt the user for total grade and total number of units for 4th subject
System.out.print("Enter the grade of the student for the 4th subject: ");
grade = Integer.parseInt(kbd.nextLine());
System.out.print("Enter the number of units for the 4th subject: ");
units = Integer.parseInt(kbd.nextLine());
totalGrade = totalGrade + grade * units; // Updates the value of totalGrade and totalUnits
totalUnits += units;
// Prompt the user for total grade and total number of units for 5th subject
System.out.print("Enter the grade of the student for the 5th subject: ");
grade = Integer.parseInt(kbd.nextLine());
System.out.print("Enter the number of units for the 5th subject: ");
units = Integer.parseInt(kbd.nextLine());
totalGrade = totalGrade + grade * units; // Updates the value of totalGrade and totalUnits
totalUnits += units;
// Prompt the user for total grade and total number of units for 6th subject
System.out.print("Enter the grade of the student for the 6th subject: ");
grade = Integer.parseInt(kbd.nextLine());
System.out.print("Enter the number of units for the 6th subject: ");
units = Integer.parseInt(kbd.nextLine());
totalGrade = totalGrade + grade * units; // Updates the value of totalGrade and totalUnits
totalUnits += units;
// Prompt the user for total grade and total number of units for 7th subject
System.out.print("Enter the grade of the student for the 7th subject: ");
grade = Integer.parseInt(kbd.nextLine());
System.out.print("Enter the number of units for the 7th subject: ");
units = Integer.parseInt(kbd.nextLine());
totalGrade = totalGrade + grade * units; // Updates the value of totalGrade and totalUnits
totalUnits += units;
// Prompt the user for total grade and total number of units for 8th subject
System.out.print("Enter the grade of the student for the 8th subject: ");
grade = Integer.parseInt(kbd.nextLine());
System.out.print("Enter the number of units for the 8th subject: ");
units = Integer.parseInt(kbd.nextLine());
totalGrade = totalGrade + grade * units; // Updates the value of totalGrade and totalUnits
totalUnits += units;
// Prompt the user for total grade and total number of units for 9th subject
System.out.print("Enter the grade of the student for the 9th subject: ");
grade = Integer.parseInt(kbd.nextLine());
System.out.print("Enter the number of units for the 9th subject: ");
units = Integer.parseInt(kbd.nextLine());
totalGrade += grade * units; // Updates the value of totalGrade and totalUnits
totalUnits += units;
// Prompt the user for total grade and total number of units for 10th subject
System.out.print("Enter the grade of the student for the 10th subject: ");
grade = Integer.parseInt(kbd.nextLine());
System.out.print("Enter the number of units for the 10th subject: ");
units = Integer.parseInt(kbd.nextLine());
totalGrade = totalGrade + grade * units; // Updates the value of totalGrade and totalUnits
totalUnits += units;
average = totalGrade / totalUnits; // Computes the average
System.out.print("Your Average Grade = " + average + "."); // Display the Average
// Determining if the student belong to the dean's list
if (average >= 85){
System.out.print( " Congratulations! You belong to the dean's list.");
}
else { //If (average < 85)
System.out.print(" Sorry! You did not make it to the dean's list. Do better next term.");
}
}
} // end