Skip to content

Commit e15edc6

Browse files
committed
add gradebook class
1 parent 11fc33d commit e15edc6

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.launchcode.java.demos.collections;
2+
3+
import java.util.ArrayList;
4+
import java.util.Scanner;
5+
6+
public class Gradebook {
7+
8+
public static void main(String[] args) {
9+
10+
ArrayList<String> students = new ArrayList<>();
11+
ArrayList<Double> grades = new ArrayList<>();
12+
Scanner in = new Scanner(System.in);
13+
String newStudent;
14+
15+
System.out.println("Enter your students (or ENTER to finish):");
16+
17+
// Get student names
18+
do {
19+
newStudent = in.nextLine();
20+
21+
if (!newStudent.equals("")) {
22+
students.add(newStudent);
23+
}
24+
25+
} while(!newStudent.equals(""));
26+
27+
// Get student grades
28+
for (String student : students) {
29+
System.out.print("Grade for " + student + ": ");
30+
Double grade = in.nextDouble();
31+
grades.add(grade);
32+
}
33+
34+
// Print class roster
35+
System.out.println("\nClass roster:");
36+
double sum = 0.0;
37+
38+
for (int i = 0; i < students.size(); i++) {
39+
System.out.println(students.get(i) + " (" + grades.get(i) + ")");
40+
sum += grades.get(i);
41+
}
42+
43+
double avg = sum / students.size();
44+
System.out.println("Average grade: " + avg);
45+
}
46+
}

0 commit comments

Comments
 (0)