-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabExam2Solution.java
More file actions
61 lines (41 loc) · 1.4 KB
/
LabExam2Solution.java
File metadata and controls
61 lines (41 loc) · 1.4 KB
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
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class LabExam2Solution {
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of Students: ");
int numstud = sc.nextInt(); sc.nextLine();
String students[] = new String[numstud];
double grades[] = new double[numstud];
for(int i = 0; i<numstud; i++) {
System.out.println("Enter name of student " + (i+1) + ": ");
students[i] = sc.nextLine();
System.out.println("Enter grade of " + students[i] + " :");
grades[i] = sc.nextDouble(); sc.nextLine();
System.out.println();
}
writeToFile(students,grades,countPassed(grades));
}
public static int countPassed(double[] grades) {
int passed = 0;
for(int i=0;i<grades.length;i++) {
if(grades[i]>=75) {
passed++;
}
}
return passed;
}
public static void writeToFile(String[] names, double[] grades, int passed) throws IOException {
FileWriter fw = new FileWriter("GradesReport.txt");
fw.write("Student Grades: \n");
for(int i=0 ; i<names.length; i++) {
fw.write(names[i] + " - " + grades[i] + "\n");
}
fw.write("\n Passed: " + passed);
fw.write("\n Failed: " + (grades.length-passed));
fw.close();
System.out.println("Grades are saved - GradesReport.txt");
}
}