2D Utilities as a Helper Class (Refactor)
Refactor repeated 2D logic into a reusable helper. Expose static utilities for widths, aligned printing, averages, maxima and transpose. Use the helper from small demos.
2D Utilities as a Helper Class (Refactor) is a free Java Academy lesson on CoddyKit. This is lesson 5 of 6. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, and your progress syncs across the web and the CoddyKit app. The Java Academy course includes 6 lessons in total.
Why Refactor
Why refactor into a helper? Avoid copy-paste, keep one source of truth, and make later lessons simpler. We will create a GradeUtil helper with static methods for widths, aligned printing, averages, maxima and transpose.
Helper Shell + Print
Create a static GradeUtil with widths and printAligned. Then, from main, call GradeUtil.printAligned(m) to print any jagged matrix with aligned columns.
public class Main {
// Static helper with reusable utilities for 2D int matrices
static class GradeUtil {
// Compute per-column widths (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) {
// Demo: build a small jagged matrix and print it via the helper
int[][] m = {
{1, 23, 456},
{7, 8},
{90, 12, 3, 4}
};
System.out.println("Aligned print via GradeUtil:");
GradeUtil.printAligned(m);
}
}
Averages Helper
Add rowAverage and colAverage to the helper and call them from main. Column averages must guard per-row lengths for jagged safety.
public class Main {
static class GradeUtil {
static double rowAverage(int[][] g, int r) {
int sum = 0;
for (int c = 0; c < g[r].length; c = c + 1) sum = sum + g[r][c];
return (double) sum / g[r].length;
}
static double colAverage(int[][] g, int c) {
int sum = 0;
int count = 0;
for (int r = 0; r < g.length; r = r + 1) {
if (c < g[r].length) { sum = sum + g[r][c]; count = count + 1; }
}
return (double) sum / count;
}
}
public static void main(String[] args) {
int[][] g = {
{78, 85, 90, 88, 92},
{95, 91, 89, 93, 87},
{60, 72, 70, 68, 75},
{88, 84, 79, 85, 90}
};
// Use helper to compute row averages
for (int r = 0; r < g.length; r = r + 1) {
double avg = GradeUtil.rowAverage(g, r);
System.out.println("student " + r + " avg = " + String.format("%.2f", avg));
}
// Use helper to compute column averages
for (int c = 0; c < g[0].length; c = c + 1) {
double avg = GradeUtil.colAverage(g, c);
System.out.println("assignment " + c + " avg = " + String.format("%.2f", avg));
}
}
}
Transpose Helpers
Implement transpose for rectangular and jagged matrices. The jagged version creates rows for each column index and pads missing entries with zero.
public class Main {
static class GradeUtil {
// Transpose rectangular matrix (R x C) -> (C x R)
static int[][] transposeRect(int[][] m) {
int rows = m.length;
int cols = m[0].length;
int[][] t = new int[cols][rows];
for (int r = 0; r < rows; r = r + 1) {
for (int c = 0; c < cols; c = c + 1) t[c][r] = m[r][c];
}
return t;
}
// Jagged-safe transpose (pads with 0)
static int[][] transposeJagged(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[][] t = new int[maxCols][];
for (int c = 0; c < maxCols; c = c + 1) {
t[c] = new int[m.length];
for (int r = 0; r < m.length; r = r + 1) t[c][r] = (c < m[r].length) ? m[r][c] : 0;
}
return t;
}
}
public static void main(String[] args) {
int[][] rect = {
{1, 2, 3},
{4, 5, 6}
};
int[][] t = GradeUtil.transposeRect(rect);
System.out.println("t[0][1] should be 2: " + t[0][1]);
int[][] jag = {
{9, 12, 7},
{5},
{8, 10}
};
int[][] tj = GradeUtil.transposeJagged(jag);
System.out.println("tj[1][0] should be 12: " + tj[1][0]);
}
}
Column Max Helper
Expose colMaxima to compute the maximum per column, handling jagged rows safely. Use seen flag to avoid bogus minima when a column is empty.
public class Main {
static class GradeUtil {
// Column maxima for a jagged matrix
static int[] colMaxima(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[] max = new int[maxCols];
for (int c = 0; c < maxCols; c = c + 1) {
int best = Integer.MIN_VALUE;
boolean seen = false;
for (int r = 0; r < m.length; r = r + 1) {
if (c < m[r].length) {
if (!seen || m[r][c] > best) { best = m[r][c]; seen = true; }
}
}
max[c] = seen ? best : 0;
}
return max;
}
}
public static void main(String[] args) {
int[][] jag = {
{9, 12, 7},
{5},
{8, 10}
};
int[] cm = GradeUtil.colMaxima(jag);
System.out.print("col maxima: ");
for (int i = 0; i < cm.length; i = i + 1) System.out.print(cm[i] + (i + 1 < cm.length ? " " : ""));
System.out.println();
}
}
All Utilities Together
Combine utilities into one helper and drive them from a single main: print, averages, transpose and column maxima. This is your reusable toolkit for later lessons.
public class Main {
static class GradeUtil {
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;
}
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);
}
}
static double rowAverage(int[][] g, int r) {
int sum = 0; for (int c = 0; c < g[r].length; c = c + 1) sum = sum + g[r][c];
return (double) sum / g[r].length;
}
static double colAverage(int[][] g, int c) {
int sum = 0; int count = 0;
for (int r = 0; r < g.length; r = r + 1) { if (c < g[r].length) { sum = sum + g[r][c]; count = count + 1; } }
return (double) sum / count;
}
static int[][] transposeRect(int[][] m) {
int rows = m.length; int cols = m[0].length; int[][] t = new int[cols][rows];
for (int r = 0; r < rows; r = r + 1) for (int c = 0; c < cols; c = c + 1) t[c][r] = m[r][c];
return t;
}
static int[][] transposeJagged(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[][] t = new int[maxCols][];
for (int c = 0; c < maxCols; c = c + 1) { t[c] = new int[m.length]; for (int r = 0; r < m.length; r = r + 1) t[c][r] = (c < m[r].length) ? m[r][c] : 0; }
return t;
}
static int[] colMaxima(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[] max = new int[maxCols];
for (int c = 0; c < maxCols; c = c + 1) {
int best = Integer.MIN_VALUE; boolean seen = false;
for (int r = 0; r < m.length; r = r + 1) if (c < m[r].length) { if (!seen || m[r][c] > best) { best = m[r][c]; seen = true; } }
max[c] = seen ? best : 0;
}
return max;
}
}
public static void main(String[] args) {
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("Original gradebook:");
GradeUtil.printAligned(g);
System.out.println("Row averages:");
for (int r = 0; r < g.length; r = r + 1) System.out.println(r + ": " + String.format("%.2f", GradeUtil.rowAverage(g, r)));
System.out.println("Column averages:");
for (int c = 0; c < g[0].length; c = c + 1) System.out.println(c + ": " + String.format("%.2f", GradeUtil.colAverage(g, c)));
System.out.println("Transpose (rect):");
GradeUtil.printAligned(GradeUtil.transposeRect(g));
System.out.println("Column maxima:");
int[] cm = GradeUtil.colMaxima(g);
for (int i = 0; i < cm.length; i = i + 1) System.out.print(cm[i] + (i + 1 < cm.length ? " " : ""));
System.out.println();
}
}
Static Call Check
Quick check: How do you call a static helper that prints a matrix?
Recap & Next
Recap: You refactored common 2D operations into a GradeUtil helper and used it from multiple demos. This keeps code small, readable and consistent across lessons.
Learn Java with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 104
- Lessons
- 374
Frequently Asked Questions
Is the “2D Utilities as a Helper Class (Refactor)” lesson free?
Yes — the full text of “2D Utilities as a Helper Class (Refactor)” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO. The Java Academy course includes 6 lessons in total.
What will I learn in “2D Utilities as a Helper Class (Refactor)”?
Refactor repeated 2D logic into a reusable helper. Expose static utilities for widths, aligned printing, averages, maxima and transpose. Use the helper from small demos. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 5 of 6.
How long does the “2D Utilities as a Helper Class (Refactor)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- 2D Array Basics & Nested Iteration
- Jagged Arrays & Table Formatting
- Row/Column Maxima, Transpose & Formatting
- 2D Mini-Project: Gradebook
- 2D Utilities as a Helper Class (Refactor)
- 2D Data Cleaning & Validation