-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask1.java
More file actions
75 lines (65 loc) · 2.32 KB
/
Copy pathTask1.java
File metadata and controls
75 lines (65 loc) · 2.32 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.util.ArrayList;
import java.util.Scanner;
public class Task1 {
public static void main(String[] args) {
ArrayList<Double> grades = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
String input;
System.out.println("Enter student grades. Type 'done' to finish:");
while (true) {
System.out.print("Enter grade: ");
input = scanner.nextLine();
if (input.equalsIgnoreCase("done")) {
break;
}
try {
double grade = Double.parseDouble(input);
if (grade < 0 || grade > 100) {
System.out.println("Please enter a grade between 0 and 100.");
} else {
grades.add(grade);
}
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a numeric value.");
}
}
if (grades.isEmpty()) {
System.out.println("No grades entered.");
} else {
double average = calculateAverage(grades);
double highest = findHighest(grades);
double lowest = findLowest(grades);
System.out.println("\nGrade Summary:");
System.out.println("Average Grade: " + average);
System.out.println("Highest Grade: " + highest);
System.out.println("Lowest Grade: " + lowest);
}
// closing the scanner object to avoid leaks in the code
scanner.close();
}
private static double calculateAverage(ArrayList<Double> grades) {
double sum = 0;
for (double grade : grades) {
sum += grade;
}
return sum / grades.size();
}
private static double findHighest(ArrayList<Double> grades) {
double highest = grades.get(0);
for (double grade : grades) {
if (grade > highest) {
highest = grade;
}
}
return highest;
}
private static double findLowest(ArrayList<Double> grades) {
double lowest = grades.get(0);
for (double grade : grades) {
if (grade < lowest) {
lowest = grade;
}
}
return lowest;
}
}