forked from angiejones/java-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrades.java
68 lines (53 loc) · 1.58 KB
/
Grades.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
package chapter7;
import java.util.Scanner;
/*
* Create a program that allows a user to enter any
* number of grades and provides them with their
* average score, as well as the highest and lowest scores.
*/
public class Grades {
private static int grades[];
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args){
System.out.println("How many grades would you like to enter?");
grades = new int[scanner.nextInt()];
getGrades();
System.out.println("Average: " + String.format("%.2f", calculateAverage()));
System.out.println("Highest: " + getHighest());
System.out.println("Lowest: " + getLowest());
}
public static void getGrades(){
for(int i=0; i<grades.length; i++){
System.out.println("Enter grade #" + (i+1));
grades[i] = scanner.nextInt();
}
}
public static int calculateSum(){
int sum = 0;
for(int grade : grades){
sum = sum + grade;
}
return sum;
}
public static double calculateAverage(){
return calculateSum()/grades.length;
}
public static int getHighest(){
int highest = grades[0];
for(int grade: grades){
if(grade > highest){
highest = grade;
}
}
return highest;
}
public static int getLowest(){
int lowest = grades[0];
for(int grade: grades){
if(grade < lowest){
lowest = grade;
}
}
return lowest;
}
}