Files

2.6 KiB

matrix

Companion code for the ankurm.com article Matrix Programs in Java: Addition, Multiplication, Transpose, Spiral and Rotation.

The five classic lab programs on int[][], each cross-checked with algebraic laws on seeded random matrices. Every figure and console line in the article is quoted from a file in output/, which the JUnit tests write while asserting the same numbers. There is no docs/ folder: the explanations live in the article itself.

Versions

Component Version Verified against
JDK Temurin 25.0.5+8 (LTS) java -version
JUnit 6.1.3 junit-bom maven-metadata.xml on Maven Central (latest GA)
Maven 3.9.x mvn -version

Quickstart

cd matrix
export JAVA_HOME=/path/to/jdk-25 PATH="$JAVA_HOME/bin:$PATH"
./scripts/run-all.sh      # runs the tests and regenerates output/*.txt

The first Maven run must be online so the plugins land in the local cache.

Source layout

matrix/
├── pom.xml
├── scripts/run-all.sh
├── src/main/java/com/ankurm/interviewlab/matrix/
│   ├── MatrixBasics.java   add, subtract, multiply (i-j-k and i-k-j), multiplyExact, identity, copyOf, format
│   ├── Transpose.java      transpose (any shape), transposeInPlace (square), isSymmetric
│   ├── Spiral.java         spiralOrder, spiralMatrix
│   ├── Rotation.java       clockwise, counter-clockwise, 180, in-place (transpose + reverse, layer swaps)
│   ├── DryRuns.java        instrumented copies that record every step
│   ├── Complexity.java     operation counters (counted, not timed)
│   └── Pitfalls.java       six plausible-looking wrong versions
├── src/test/java/.../      SolutionsTest, DryRunTest, ComplexityTest, RandomAgreementTest, PitfallsTest
└── output/*.txt            captured real output

Captured output

File What it shows
01-addition-and-multiplication.txt sum, product, shape-mismatch and jagged-row errors
02-multiply-dry-run.txt the four steps that build one cell of a product
03-transpose.txt rectangular transpose, in-place square transpose, symmetric check
04-spiral-trace.txt, 04a-spiral-results.txt boundary trace of a 3x4 spiral, edge shapes, spiralMatrix(4)
05-rotation.txt clockwise, counter-clockwise, 180, both in-place methods, a rectangular rotation
06-layer-rotation-trace.txt each four-cell move of the layer rotation on 3x3
07-random-agreement.txt nine laws, 2,000 seeded trials each
08-operation-counts.txt multiplications, swaps, moves and visits
09-pitfalls.txt six classic mistakes reproduced