Skip to content

Commit 6989bfe

Browse files
committed
Add Java structs and classes module
1 parent 27e82f6 commit 6989bfe

14 files changed

Lines changed: 330 additions & 15 deletions

File tree

CONCEPT_INDEX.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Use this page to browse the same concept across every active language track.
3636

3737
| Concept | C++ | C# | Go | Java | Python | TypeScript |
3838
| --- | --- | --- | --- | --- | --- | --- |
39-
| structs-and-classes | [C++](languages/cpp/03-advanced/structs-and-classes/README.md) | [C#](languages/csharp/03-advanced/structs-and-classes/README.md) | [Go](languages/go/03-advanced/structs-and-classes/README.md) | Planned | [Python](languages/python/03-advanced/structs-and-classes/README.md) | [TypeScript](languages/typescript/03-advanced/structs-and-classes/README.md) |
39+
| structs-and-classes | [C++](languages/cpp/03-advanced/structs-and-classes/README.md) | [C#](languages/csharp/03-advanced/structs-and-classes/README.md) | [Go](languages/go/03-advanced/structs-and-classes/README.md) | [Java](languages/java/03-advanced/structs-and-classes/README.md) | [Python](languages/python/03-advanced/structs-and-classes/README.md) | [TypeScript](languages/typescript/03-advanced/structs-and-classes/README.md) |
4040
| constructors-and-invariants | [C++](languages/cpp/03-advanced/constructors-and-invariants/README.md) | [C#](languages/csharp/03-advanced/constructors-and-invariants/README.md) | [Go](languages/go/03-advanced/constructors-and-invariants/README.md) | Planned | [Python](languages/python/03-advanced/constructors-and-invariants/README.md) | [TypeScript](languages/typescript/03-advanced/constructors-and-invariants/README.md) |
4141
| copy-and-move-semantics | [C++](languages/cpp/03-advanced/copy-and-move-semantics/README.md) | [C#](languages/csharp/03-advanced/copy-and-move-semantics/README.md) | [Go](languages/go/03-advanced/copy-and-move-semantics/README.md) | Planned | [Python](languages/python/03-advanced/copy-and-move-semantics/README.md) | [TypeScript](languages/typescript/03-advanced/copy-and-move-semantics/README.md) |
4242
| inheritance-and-polymorphism | [C++](languages/cpp/03-advanced/inheritance-and-polymorphism/README.md) | [C#](languages/csharp/03-advanced/inheritance-and-polymorphism/README.md) | [Go](languages/go/03-advanced/inheritance-and-polymorphism/README.md) | Planned | [Python](languages/python/03-advanced/inheritance-and-polymorphism/README.md) | [TypeScript](languages/typescript/03-advanced/inheritance-and-polymorphism/README.md) |

LANGUAGE_PARITY_MATRIX.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This matrix tracks module and checkpoint parity across C++, C#, Go, Java, Python
44

55
- Canonical order is defined by the C++ track.
66
- C++, C#, Go, Python, and TypeScript now reach module and checkpoint parity through `04-expert`.
7-
- Java currently tracks `01-foundations` and `02-core` as a Java 21 MVP.
7+
- Java currently tracks `01-foundations`, `02-core`, and the first `03-advanced` module as a Java 21 MVP.
88
- Status labels:
99
- `Done`: module implemented with example, exercises, and README.
1010
- `Planned`: module not implemented yet, already queued in order.
@@ -47,15 +47,15 @@ Current parity progress in non-C++ tracks:
4747

4848
- C#: `5/5` modules complete in `03-advanced`, `5/5` in `04-expert`
4949
- Go: `5/5` modules complete in `03-advanced`, `5/5` in `04-expert`
50-
- Java: `0/5` modules complete in `03-advanced`, `0/5` in `04-expert`
50+
- Java: `1/5` modules complete in `03-advanced`, `0/5` in `04-expert`
5151
- Python: `5/5` modules complete in `03-advanced`, `5/5` in `04-expert`
5252
- TypeScript: `5/5` modules complete in `03-advanced`, `5/5` in `04-expert`
5353

5454
### Advanced (`03-advanced`) - Current Expansion Queue
5555

5656
| Order | Module | C++ | C# | Go | Java | Python | TypeScript |
5757
| --- | --- | --- | --- | --- | --- | --- | --- |
58-
| 1 | structs-and-classes | Done | Done | Done | Planned | Done | Done |
58+
| 1 | structs-and-classes | Done | Done | Done | Done | Done | Done |
5959
| 2 | constructors-and-invariants | Done | Done | Done | Planned | Done | Done |
6060
| 3 | copy-and-move-semantics | Done | Done | Done | Planned | Done | Done |
6161
| 4 | inheritance-and-polymorphism | Done | Done | Done | Planned | Done | Done |

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ If you only want to learn one track, follow that track README first because lang
5656
| C++ | 00-setup, 01-foundations, 02-core, 03-advanced, 04-expert | Foundations, Core, Advanced, Expert, projects, assessments | Most complete and primary track |
5757
| C# | 01-foundations, 02-core, 03-advanced, 04-expert | 8/8 foundations modules, 6/6 core modules, 5/5 advanced modules, 5/5 expert modules, 4/4 projects, 4/4 assessments | Module and checkpoint parity complete through expert |
5858
| Go | 01-foundations, 02-core, 03-advanced, 04-expert | 8/8 foundations modules, 6/6 core modules, 5/5 advanced modules, 5/5 expert modules, 4/4 projects, 4/4 assessments | Module and checkpoint parity complete through expert |
59-
| Java | 01-foundations, 02-core | 8/8 foundations modules, 6/6 core modules, 2/4 projects, 2/4 assessments | Java 21 MVP through core; advanced, expert, and later checkpoints planned |
59+
| Java | 01-foundations, 02-core, 03-advanced partial | 8/8 foundations modules, 6/6 core modules, 1/5 advanced modules, 2/4 projects, 2/4 assessments | Java 21 MVP through core plus first advanced module; remaining advanced, expert, and later checkpoints planned |
6060
| Python | 01-foundations, 02-core, 03-advanced, 04-expert | 8/8 foundations modules, 6/6 core modules, 5/5 advanced modules, 5/5 expert modules, 4/4 projects, 4/4 assessments | Module and checkpoint parity complete through expert |
6161
| TypeScript | 01-foundations, 02-core, 03-advanced, 04-expert | 8/8 foundations modules, 6/6 core modules, 5/5 advanced modules, 5/5 expert modules, 4/4 projects, 4/4 assessments | Module and checkpoint parity complete through expert |
6262

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Java 03 Advanced
2+
3+
This level starts the advanced Java track with object modeling. It is currently a partial expansion.
4+
5+
## Learning Metadata
6+
7+
- Difficulty: Intermediate.
8+
- Estimated Time: 45-60 minutes for the current module.
9+
- Prerequisites: Completion of `01-foundations` and `02-core`.
10+
- Study Strategy: Focus on data plus behavior first, then compare Java classes with records, structs, and classes in other tracks.
11+
12+
## Module Order
13+
14+
1. [structs-and-classes](./structs-and-classes/README.md)
15+
16+
## Level Outcomes
17+
18+
- Model related data and behavior with Java records and classes.
19+
- Protect object state through constructors and methods.
20+
- Compare Java object modeling with C++, C#, Go, Python, and TypeScript.
21+
22+
## Done When
23+
24+
- [ ] I ran `structs-and-classes/example/Main.java`.
25+
- [ ] I completed both `structs-and-classes` exercises.
26+
- [ ] I can explain when a Java record is enough and when a class is better.
27+
- [ ] I can keep state updates behind methods instead of editing fields directly.
28+
29+
## Study Tip
30+
31+
This level is intentionally partial. Finish `structs-and-classes` before expanding into constructors, copy/reference behavior, inheritance, and generics.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Structs and Classes (Java)
2+
3+
This module introduces object modeling with records and classes.
4+
5+
## Learning Metadata
6+
7+
- Difficulty: Intermediate.
8+
- Estimated Time: 35-50 minutes.
9+
- Prerequisites: `01-foundations/functions`, `01-foundations/scope-and-lifetime-basics`, and `02-core/input-validation`.
10+
- Cross-Language Lens: Compare Java records and classes with structs, dataclasses, interfaces, and class-style patterns in the other tracks.
11+
12+
## Quick Run
13+
14+
~~~bash
15+
javac -d build/java languages/java/03-advanced/structs-and-classes/example/Main.java
16+
java -cp build/java Main
17+
~~~
18+
19+
## Topics Covered
20+
21+
- Lightweight value modeling with `record`.
22+
- Stateful behavior using classes and private fields.
23+
- Constructor cleanup to preserve valid object state.
24+
- Method-based updates for controlled mutations.
25+
26+
## Common Pitfalls
27+
28+
- Using public mutable fields when methods should protect object state.
29+
- Letting invalid constructor inputs propagate through the object lifecycle.
30+
- Choosing a full class when a record would communicate immutable data more clearly.
31+
32+
## Exercise Focus
33+
34+
- exercises/Exercise01.java: model a rectangle using a class with methods.
35+
- exercises/Exercise02.java: build an encapsulated `Counter` class with commands.
36+
37+
### Exercise Specs
38+
39+
1. exercises/Exercise01.java
40+
- Input: width and height.
41+
- Output: area and perimeter.
42+
- Edge cases: non-positive dimensions should stop with a message; decimal values should work.
43+
44+
2. exercises/Exercise02.java
45+
- Input: sequence of commands (`inc`, `dec`, `reset`, `stop`).
46+
- Output: counter value updates and final value.
47+
- Edge cases: unknown commands; immediate `stop`.
48+
49+
## Checkpoint
50+
51+
- [ ] I can explain when `record` is enough and when a class with methods is better.
52+
- [ ] I can enforce simple invariants in constructors.
53+
- [ ] I can keep state updates inside well-named methods.
54+
- [ ] I completed exercises/Exercise01.java.
55+
- [ ] I completed exercises/Exercise02.java.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Module focus: Modeling related data and behavior with structured types.
2+
// Why it matters: practicing structs and classes patterns makes exercises and checkpoints easier to reason about.
3+
4+
import java.util.List;
5+
import java.util.Locale;
6+
7+
public class Main {
8+
record Coordinate(int x, int y) {
9+
int manhattanDistanceFromOrigin() {
10+
// Records are a good fit for small immutable values with derived behavior.
11+
return Math.abs(x) + Math.abs(y);
12+
}
13+
}
14+
15+
static class Wallet {
16+
private final String owner;
17+
private double balance;
18+
19+
Wallet(String owner, double initialBalance) {
20+
// Constructors are the first chance to normalize or reject invalid state.
21+
String cleanOwner = owner == null ? "" : owner.trim();
22+
this.owner = cleanOwner.isEmpty() ? "Unknown" : cleanOwner;
23+
this.balance = Math.max(0.0, initialBalance);
24+
}
25+
26+
boolean deposit(double amount) {
27+
// Invalid deposits are rejected instead of silently changing state.
28+
if (amount <= 0.0) {
29+
return false;
30+
}
31+
balance += amount;
32+
return true;
33+
}
34+
35+
boolean withdraw(double amount) {
36+
// Methods protect the balance invariant by rejecting impossible updates.
37+
if (amount <= 0.0 || amount > balance) {
38+
return false;
39+
}
40+
balance -= amount;
41+
return true;
42+
}
43+
44+
String owner() {
45+
// Accessor methods expose read-only views of private fields.
46+
return owner;
47+
}
48+
49+
double balance() {
50+
return balance;
51+
}
52+
}
53+
54+
public static void main(String[] args) {
55+
Locale.setDefault(Locale.US);
56+
// Prepare sample inputs that exercise the record path.
57+
List<Coordinate> route = List.of(new Coordinate(2, 3), new Coordinate(-1, 4), new Coordinate(5, -2));
58+
59+
// Report values so learners can verify the structs and classes outcome.
60+
System.out.println("Coordinates (record example):");
61+
for (Coordinate point : route) {
62+
System.out.println(
63+
"Point (%d, %d), Manhattan distance = %d"
64+
.formatted(point.x(), point.y(), point.manhattanDistanceFromOrigin()));
65+
}
66+
67+
Wallet wallet = new Wallet("Maya", 120.0);
68+
// Run controlled mutations through methods rather than editing fields directly.
69+
wallet.deposit(35.0);
70+
wallet.withdraw(40.0);
71+
72+
System.out.println();
73+
System.out.println("Wallet (class example):");
74+
System.out.println("Owner: " + wallet.owner());
75+
System.out.printf("Balance: %.2f%n", wallet.balance());
76+
}
77+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.Locale;
2+
import java.util.Scanner;
3+
4+
public class Exercise01 {
5+
static class Rectangle {
6+
private final double width;
7+
private final double height;
8+
9+
Rectangle(double width, double height) {
10+
if (width <= 0.0 || height <= 0.0) {
11+
throw new IllegalArgumentException("Dimensions must be positive.");
12+
}
13+
this.width = width;
14+
this.height = height;
15+
}
16+
17+
double area() {
18+
return width * height;
19+
}
20+
21+
double perimeter() {
22+
return 2.0 * (width + height);
23+
}
24+
}
25+
26+
public static void main(String[] args) {
27+
Locale.setDefault(Locale.US);
28+
Scanner scanner = new Scanner(System.in);
29+
double width = scanner.nextDouble();
30+
double height = scanner.nextDouble();
31+
32+
try {
33+
Rectangle rectangle = new Rectangle(width, height);
34+
System.out.printf("Area: %.2f%n", rectangle.area());
35+
System.out.printf("Perimeter: %.2f%n", rectangle.perimeter());
36+
} catch (IllegalArgumentException error) {
37+
System.out.println(error.getMessage());
38+
}
39+
}
40+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.Scanner;
2+
3+
public class Exercise02 {
4+
static class Counter {
5+
private int value;
6+
7+
void increment() {
8+
value++;
9+
}
10+
11+
void decrement() {
12+
value--;
13+
}
14+
15+
void reset() {
16+
value = 0;
17+
}
18+
19+
int value() {
20+
return value;
21+
}
22+
}
23+
24+
public static void main(String[] args) {
25+
Scanner scanner = new Scanner(System.in);
26+
Counter counter = new Counter();
27+
28+
while (scanner.hasNext()) {
29+
String command = scanner.next().trim().toLowerCase();
30+
if (command.equals("stop")) {
31+
break;
32+
}
33+
34+
switch (command) {
35+
case "inc" -> counter.increment();
36+
case "dec" -> counter.decrement();
37+
case "reset" -> counter.reset();
38+
default -> {
39+
System.out.println("Unknown command: " + command);
40+
continue;
41+
}
42+
}
43+
44+
System.out.println("Value: " + counter.value());
45+
}
46+
47+
System.out.println("Final value: " + counter.value());
48+
}
49+
}

languages/java/CHECKLIST.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,17 @@
4141
- [ ] assessments/01-foundations
4242
- [ ] assessments/02-core
4343

44+
## 03-advanced
45+
46+
- [ ] structs-and-classes example
47+
- [ ] structs-and-classes exercises
48+
4449
## Planned
4550

46-
- [ ] 03-advanced modules
51+
- [ ] constructors-and-invariants module
52+
- [ ] copy-and-move-semantics module
53+
- [ ] inheritance-and-polymorphism module
54+
- [ ] templates-basics module
4755
- [ ] 04-expert modules
4856
- [ ] projects/03-advanced
4957
- [ ] projects/04-expert

languages/java/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
## Scope and Status
44

5-
This track currently covers `01-foundations` and `02-core` as a Java 21 MVP, including projects and assessments for both implemented levels. Advanced, expert, and later checkpoints are planned for future iterations.
5+
This track currently covers `01-foundations` and `02-core` as a Java 21 MVP, including projects and assessments for both implemented levels. The `03-advanced` track has started with `structs-and-classes`; expert and later checkpoints are planned for future iterations.
66

77
- 8/8 foundations modules implemented.
88
- 6/6 core modules implemented.
9-
- 0/5 advanced modules implemented.
9+
- 1/5 advanced modules implemented (`structs-and-classes`).
1010
- 0/5 expert modules implemented.
1111
- 2/4 projects implemented (`01-foundations`, `02-core`).
1212
- 2/4 assessments implemented (`01-foundations`, `02-core`).
@@ -49,6 +49,8 @@ java -cp build/java Main
4949
- [sorting-and-searching](./02-core/sorting-and-searching/README.md)
5050
- [maps-and-frequency-counting](./02-core/maps-and-frequency-counting/README.md)
5151
- [error-handling-and-defensive-programming](./02-core/error-handling-and-defensive-programming/README.md)
52+
- [03-advanced](./03-advanced/README.md)
53+
- [structs-and-classes](./03-advanced/structs-and-classes/README.md)
5254
- Projects
5355
- [01-foundations](./projects/01-foundations/README.md)
5456
- [02-core](./projects/02-core/README.md)

0 commit comments

Comments
 (0)