Commit e30ec1f
committed
feat: Add Circle2 with encapsulation, validation, and geometry methods
WHAT the code does:
Defines Circle2 with private fields radius and color.
Provides:
- Constructor: initializes radius = 1.0 and color = "red".
- Getters: getRadius(), getColor() for controlled access.
- Setters: setRadius(double r) and setColor(String c) with validation logic.
Implements behavior methods:
- area(): computes circle area (πr²).
- circumference(): computes circle circumference (2πr).
Defines DataHidding1 with main():
- Creates a Circle2 object.
- Prints initial values using getters.
- Updates fields with valid inputs via setters.
- Demonstrates calculations for area and circumference.
- Attempts invalid inputs (negative radius, empty color) to show validation in action.
WHY this matters:
Demonstrates data hiding, a key OOP principle:
- Private fields prevent direct external modification.
- Public methods enforce controlled access with validation.
Shows how validation logic preserves object integrity (e.g., no negative radius or empty color).
Illustrates combining encapsulation with object behavior methods for real-world modeling.
HOW it works:
Constructor sets default values radius = 1.0, color = red.
Valid setter calls update values successfully (radius = 5.0, color = blue).
Invalid setter calls reset values to defaults with console messages.
area() and circumference() compute circle properties based on the current radius.
main() prints results before and after updates.
Tips and gotchas:
Validation currently resets to defaults silently after printing messages; throwing exceptions could be clearer in robust applications.
Comparing strings with null/empty check works here, but additional validation (e.g., restricting to known colors) could be added.
Overriding toString() would simplify printing circle details.
Fields could be made final with constructors for immutable design, if mutability isn’t needed.
Use-cases:
Educational example of encapsulation and data hiding in Java.
Practical model for geometric objects with validation logic.
Foundation for systems requiring controlled object state (e.g., graphics, simulations, GUIs).
Short key: class-circle2 encapsulation validation geometry.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 495e93b commit e30ec1f
1 file changed
+81
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
1 | 58 | | |
2 | 59 | | |
3 | 60 | | |
4 | | - | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
5 | 85 | | |
6 | 86 | | |
0 commit comments