-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCourseAverage.java
executable file
·34 lines (31 loc) · 1.05 KB
/
CourseAverage.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
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class CourseAverage {
public static void main(String[] args) {
int[] exams = new int[3];
double examAvg, hwAvg, finalExam, courseAvg;
Scanner gradeFile = null;
try {
gradeFile = new Scanner(new FileInputStream("grades.txt"));
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
System.exit(0);
}
for (int i = 0; i < 3; ++i) {
exams[i] = gradeFile.nextInt();
}
examAvg = calcAverage(exams);
hwAvg = gradeFile.nextDouble();
finalExam = gradeFile.nextDouble();
courseAvg = .2 * hwAvg + .6 * examAvg + .2 * finalExam;
System.out.printf("Your course average is %.1f%n", courseAvg);
}
private static double calcAverage(int[] numbers) {
double sum = 0;
for (int number: numbers) {
sum += number;
}
return (double) sum / numbers.length;
}
}