Skip to content

Commit 1ff2454

Browse files
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

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed
Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
1-
public class Person
2-
{
3-
//attributes of every Person object
4-
private int yearBorn;
5-
private int yearDeceased;
6-
private String name;
1+
public class Person {
2+
//attributes of every Person object.
3+
// attributes of every Person object
4+
private int YearBorn;
5+
private int YearDeceased;
6+
private String Name;
77

8-
//constructor//
9-
public Person(String named, int born, int died)
10-
{
11-
name = named;
12-
yearBorn = born;
13-
yearDeceased = died;
8+
// constructor
9+
public Person(String Name, int YearBorn, int YearDeceased) {
10+
this.Name = Name;
11+
this.YearBorn = YearBorn;
12+
this.YearDeceased = YearDeceased;
1413
}
1514

16-
//instance (object) method//
17-
public void epitaph()
18-
{
19-
/* Print <name> lived to an age of <age> */
15+
// instance method
16+
public void epitaph() {
17+
int age = YearDeceased - YearBorn;
18+
System.out.println(Name + " lived to an age of " + age);
2019
}
2120

22-
public static void main(String[] args)
23-
{
24-
Person grandma = new Person("Mary", 1932, 1999);
25-
grandma.epitaph(); //apply method to object (yes, in this case, grandma is considered an object)
21+
public static void main(String[] args) {
22+
Person person = new Person("Mary", 1932, 1999);
23+
person.epitaph();
2624
}
27-
}
25+
}

0 commit comments

Comments
 (0)