Skip to content

Commit 3cc3c4c

Browse files
committed
add array version of gradebook
1 parent e15edc6 commit 3cc3c4c

File tree

2 files changed

+53
-1
lines changed

2 files changed

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

src/org/launchcode/java/demos/collections/Gradebook.java renamed to src/org/launchcode/java/demos/collections/ArrayListGradebook.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.ArrayList;
44
import java.util.Scanner;
55

6-
public class Gradebook {
6+
public class ArrayListGradebook {
77

88
public static void main(String[] args) {
99

0 commit comments

Comments
 (0)