Commit 1ff2454
committed
feat: Add Person class with epitaph method for lifespan calculation
WHAT the code does:
- Defines a `Person` class with attributes:
- `Name`
- `YearBorn`
- `YearDeceased`
- Constructor initializes these attributes.
- `epitaph()` method calculates lifespan as `YearDeceased - YearBorn` and prints a formatted message.
- `main()` creates a `Person` object (`Mary`, 1932–1999) and calls `epitaph()`.
WHY this matters:
- Demonstrates **basic class design** with attributes, constructor, and instance methods.
- Shows how to compute derived information (lifespan) from stored fields.
- Useful educational example for encapsulation and object instantiation in Java.
HOW it works:
1. Constructor assigns values when `new Person()` is called.
2. `epitaph()` subtracts `YearBorn` from `YearDeceased`.
3. Prints → `"Mary lived to an age of 67"`.
Tips & gotchas:
- Assumes `YearDeceased > YearBorn`; negative or zero values would be illogical.
- Does not handle still-living persons (e.g., `YearDeceased = 0` or `null`).
- Encapsulation could be improved with getters/setters.
- Use consistent variable naming → prefer lowercase fields (`yearBorn`, `yearDeceased`) for Java convention.
Use-cases:
- Biographical applications.
- Educational examples for **constructors and methods**.
- Foundation for genealogy/family-tree programs.
- Demonstration of computing lifespan dynamically.
Short key: class-person-epitaph-lifespan.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent f79f358 commit 1ff2454
1 file changed
+19
-21
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
7 | 7 | | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
14 | 13 | | |
15 | 14 | | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
20 | 19 | | |
21 | 20 | | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
26 | 24 | | |
27 | | - | |
| 25 | + | |
0 commit comments