Math Geometry: Coordinates
In this Math lesson, we will study Coordinates through small executable cases. First we will make the normal result clear; then inspect data flow, ownership, failure behavior, and the caller contract before adding framework or infrastructure complexity.
Professional code is readable under change. A future developer should be able to identify which input is trusted, who may update state, where I/O happens, and how an expected error reaches a caller without reconstructing the whole application.
What You Will Build
10 focused Math examples for Coordinates. Each has a visible result, an explicit contract, and a next change to test so the lesson becomes reusable knowledge rather than passive reading.
Example 1: Compute a safe average
Let’s isolate one Math Geometry rule together. Run it, trace the inputs and output, then change one boundary value before reusing the pattern. Small observable programs turn syntax into a reliable engineering habit.
Keep contracts explicit: validate data before use, make ownership and mutation visible, and return a named result or failure a caller can test. Static types and a successful build help, but runtime boundaries still need intentional checks.
Math
static double average(int[] values) {
if (values.length == 0) throw new IllegalArgumentException("empty");
long sum = 0;
for (int value : values) sum += value;
return (double) sum / values.length;
}
System.out.println(average(new int[] {2, 4, 6}));Expected Result
4.0
Try next: make the nearest empty, invalid, missing, or boundary case observable instead of leaving behavior to an unchecked assumption.
Example 2: Solve a linear equation
Let’s isolate one Math Geometry rule together. Run it, trace the inputs and output, then change one boundary value before reusing the pattern. Small observable programs turn syntax into a reliable engineering habit.
Keep contracts explicit: validate data before use, make ownership and mutation visible, and return a named result or failure a caller can test. Static types and a successful build help, but runtime boundaries still need intentional checks.
Math
static double solve(double a, double b) {
if (a == 0) throw new IllegalArgumentException("not linear");
return -b / a;
}
System.out.println(solve(2, -8));Expected Result
4.0
Try next: make the nearest empty, invalid, missing, or boundary case observable instead of leaving behavior to an unchecked assumption.
Example 3: Measure Euclidean distance
Let’s isolate one Math Geometry rule together. Run it, trace the inputs and output, then change one boundary value before reusing the pattern. Small observable programs turn syntax into a reliable engineering habit.
Keep contracts explicit: validate data before use, make ownership and mutation visible, and return a named result or failure a caller can test. Static types and a successful build help, but runtime boundaries still need intentional checks.
Math
static double distance(int x1, int y1, int x2, int y2) {
return Math.hypot(x2 - x1, y2 - y1);
}
System.out.println(distance(0, 0, 3, 4));Expected Result
5.0
Try next: make the nearest empty, invalid, missing, or boundary case observable instead of leaving behavior to an unchecked assumption.
Example 4: Count combinations
Let’s isolate one Math Geometry rule together. Run it, trace the inputs and output, then change one boundary value before reusing the pattern. Small observable programs turn syntax into a reliable engineering habit.
Keep contracts explicit: validate data before use, make ownership and mutation visible, and return a named result or failure a caller can test. Static types and a successful build help, but runtime boundaries still need intentional checks.
Math
static long choose(int n, int k) {
k = Math.min(k, n - k);
long result = 1;
for (int i = 1; i <= k; i++) result = result * (n - k + i) / i;
return result;
}
System.out.println(choose(5, 2));Expected Result
10
Try next: make the nearest empty, invalid, missing, or boundary case observable instead of leaving behavior to an unchecked assumption.
Example 5: Enumerate a probability space
Let’s isolate one Math Geometry rule together. Run it, trace the inputs and output, then change one boundary value before reusing the pattern. Small observable programs turn syntax into a reliable engineering habit.
Keep contracts explicit: validate data before use, make ownership and mutation visible, and return a named result or failure a caller can test. Static types and a successful build help, but runtime boundaries still need intentional checks.
Math
int favorable = 0;
int total = 0;
for (int die1 = 1; die1 <= 6; die1++) {
for (int die2 = 1; die2 <= 6; die2++) {
total++;
if (die1 + die2 == 7) favorable++;
}
}
System.out.println(favorable + "/" + total);Expected Result
6/36
Try next: make the nearest empty, invalid, missing, or boundary case observable instead of leaving behavior to an unchecked assumption.
Example 6: Find a greatest common divisor
Let’s isolate one Math Geometry rule together. Run it, trace the inputs and output, then change one boundary value before reusing the pattern. Small observable programs turn syntax into a reliable engineering habit.
Keep contracts explicit: validate data before use, make ownership and mutation visible, and return a named result or failure a caller can test. Static types and a successful build help, but runtime boundaries still need intentional checks.
Math
static int gcd(int a, int b) {
while (b != 0) {
int remainder = a % b;
a = b;
b = remainder;
}
return Math.abs(a);
}
System.out.println(gcd(84, 30));Expected Result
6
Try next: make the nearest empty, invalid, missing, or boundary case observable instead of leaving behavior to an unchecked assumption.
Example 7: Use modular exponentiation
Let’s isolate one Math Geometry rule together. Run it, trace the inputs and output, then change one boundary value before reusing the pattern. Small observable programs turn syntax into a reliable engineering habit.
Keep contracts explicit: validate data before use, make ownership and mutation visible, and return a named result or failure a caller can test. Static types and a successful build help, but runtime boundaries still need intentional checks.
Math
static long powerMod(long base, long exponent, long mod) {
long result = 1;
while (exponent > 0) {
if ((exponent & 1) == 1) result = result * base % mod;
base = base * base % mod;
exponent >>= 1;
}
return result;
}
System.out.println(powerMod(2, 10, 1000));Expected Result
24
Try next: make the nearest empty, invalid, missing, or boundary case observable instead of leaving behavior to an unchecked assumption.
Example 8: Generate a Fibonacci sequence
Let’s isolate one Math Geometry rule together. Run it, trace the inputs and output, then change one boundary value before reusing the pattern. Small observable programs turn syntax into a reliable engineering habit.
Keep contracts explicit: validate data before use, make ownership and mutation visible, and return a named result or failure a caller can test. Static types and a successful build help, but runtime boundaries still need intentional checks.
Math
int previous = 0;
int current = 1;
for (int i = 0; i < 7; i++) {
System.out.print(previous + " ");
int next = previous + current;
previous = current;
current = next;
}Expected Result
0 1 1 2 3 5 8
Try next: make the nearest empty, invalid, missing, or boundary case observable instead of leaving behavior to an unchecked assumption.
Example 9: Approximate a derivative
Let’s isolate one Math Geometry rule together. Run it, trace the inputs and output, then change one boundary value before reusing the pattern. Small observable programs turn syntax into a reliable engineering habit.
Keep contracts explicit: validate data before use, make ownership and mutation visible, and return a named result or failure a caller can test. Static types and a successful build help, but runtime boundaries still need intentional checks.
Math
static double derivative(java.util.function.DoubleUnaryOperator f, double x) {
double h = 1e-6;
return (f.applyAsDouble(x + h) - f.applyAsDouble(x - h)) / (2 * h);
}
System.out.printf("%.1f%n", derivative(v -> v * v, 3));Expected Result
6.0
Try next: make the nearest empty, invalid, missing, or boundary case observable instead of leaving behavior to an unchecked assumption.
Example 10: Traverse a graph breadth first
Let’s isolate one Math Geometry rule together. Run it, trace the inputs and output, then change one boundary value before reusing the pattern. Small observable programs turn syntax into a reliable engineering habit.
Keep contracts explicit: validate data before use, make ownership and mutation visible, and return a named result or failure a caller can test. Static types and a successful build help, but runtime boundaries still need intentional checks.
Math
var graph = java.util.Map.of(
1, java.util.List.of(2, 3),
2, java.util.List.of(4),
3, java.util.List.of()
);
var queue = new java.util.ArrayDeque<Integer>();
var seen = new java.util.HashSet<Integer>();
queue.add(1);
seen.add(1);
while (!queue.isEmpty()) {
int node = queue.remove();
System.out.print(node + " ");
for (int next : graph.getOrDefault(node, java.util.List.of())) {
if (seen.add(next)) queue.add(next);
}
}Expected Result
1 2 3 4
Try next: make the nearest empty, invalid, missing, or boundary case observable instead of leaving behavior to an unchecked assumption.
Design Review and Worked Trace
Review Math Geometry: Coordinates as a full trace: input, validation, calculation or side effect, output, and cleanup. State the normal result, then state the boundary behavior. If either is hard to explain, split the work into a smaller function with a narrower contract.
Keep secret values and privileged decisions outside display code. Authentication establishes an identity; authorization decides what that identity may do. Check both close to protected operations, and use intentional failure responses rather than leaking internal details or crashing generically.
- Run the normal input and record the exact result.
- Test an empty, malformed, or boundary input deliberately.
- Keep state, ownership, and side effects visible to the caller.
- Return a result or named failure that can be tested.
- Never log credentials, tokens, or sensitive input.
This discipline makes Math code easier to review, safer to change, and more useful in production than a collection of untested syntax fragments.
Common Mistakes
- Skipping input validation because a local example happened to work.
- Hiding mutation, ownership, or failure behavior inside a convenient shortcut.
- Using a client-side display rule as the only protection for sensitive work.
- Copying code without changing an input to prove its contract.
