2D Mini-Project: Gradebook
Mini-project: build a small gradebook using a 2D int matrix. Print aligned tables, compute per-student and per-assignment averages, find best performers, curve scores, and output a summary report.
Project Overview
Project goal: Represent a gradebook as a 2D int matrix where each row is a student and each column is a homework/quiz/exam. You will print an aligned table, compute per-student and per-assignment averages, find the top student/assignment, curve scores, and generate a summary report.
Follow the scenes and run each code sample in order. No input is required.
Build & Print
Step 1: Create a small gradebook matrix and print it with aligned columns. We use a helper to compute column widths and a printer that pads each value.
public class Main {
// Join helper for one-dimensional arrays (space-separated)
static String join(int[] a) {
String s = "";
for (int i = 0; i < a.length; i = i + 1) {
s = s + a[i] + (i + 1 < a.length ? " " : "");
}
return s;
}
// Compute column widths for aligned printing (jagged-aware)
static int[] widths(int[][] m) {
int maxCols = 0;
for (int r = 0; r < m.length; r = r + 1) {
if (m[r].length > maxCols) maxCols = m[r].length;
}
int[] w = new int[maxCols];
for (int r = 0; r < m.length; r = r + 1) {
for (int c = 0; c < m[r].length; c = c + 1) {
int len = Integer.toString(m[r][c]).length();
if (len > w[c]) w[c] = len;
}
}
for (int c = 0; c < w.length; c = c + 1) if (w[c] < 1) w[c] = 1;
return w;
}
// Print matrix with aligned columns
static void printAligned(int[][] m) {
int[] w = widths(m);
for (int r = 0; r < m.length; r = r + 1) {
String line = "";
for (int c = 0; c < m[r].length; c = c + 1) {
String s = Integer.toString(m[r][c]);
while (s.length() < w[c]) s = " " + s;
line = line + s + (c + 1 < m[r].length ? " " : "");
}
System.out.println(line);
}
}
public static void main(String[] args) {
// Create a 4x5 gradebook (4 students, 5 assignments)
int[][] g = {
{78, 85, 90, 88, 92},
{95, 91, 89, 93, 87},
{60, 72, 70, 68, 75},
{88, 84, 79, 85, 90}
};
System.out.println("Gradebook (rows: students, cols: assignments):");
printAligned(g);
}
}