Commit e1f3ab4
committed
feat: Add Rectangle class with encapsulation and getter/setter methods
WHAT the code does:
Defines a Rectangle class with private fields length and breadth.
Provides:
- Getters: getLength(), getBreadth().
- Setters: setLength(double l), setBreadth(double b), validating against negative values (defaulting to 0 if invalid).
Implements geometry methods:
- area(): returns length × breadth.
- perimeter(): returns 2 × (length + breadth).
- isSquare(): checks if length and breadth are equal.
Defines GetAndSetMethod with main():
- Creates a Rectangle object.
- Sets values via setters, including validation support.
- Prints area, perimeter, square check, and field values via getters.
WHY this matters:
Demonstrates encapsulation: fields are private and only accessible through controlled getter/setter methods.
Shows how validation logic in setters prevents invalid states.
Illustrates practical object modeling by combining attributes and geometric behaviors.
Reinforces the use of methods (instead of direct field access) to preserve data integrity.
HOW it works:
setLength(10.5) sets length to 10.5, setBreadth(5.5) sets breadth to 5.5.
area() → 10.5 × 5.5 = 57.75.
perimeter() → 2 × (10.5 + 5.5) = 32.0.
isSquare() returns false since length ≠ breadth.
getLength() and getBreadth() retrieve stored values for display.
Tips and gotchas:
Comparing doubles with == in isSquare() may cause issues with floating-point precision; consider using a tolerance (e.g., Math.abs(l-b) < 1e-9).
Validation resets invalid inputs to 0; throwing exceptions may be clearer in production code.
Encapsulation could be extended with constructors for initializing rectangles directly.
Overriding toString() could simplify object state printing.
Use-cases:
Educational example of encapsulation and getter/setter usage in Java.
Modeling rectangles in geometric or GUI-related applications.
Foundation for extending into more advanced shape hierarchies (e.g., abstract Shape class).
Short key: class-rectangle encapsulation getters-setters geometry.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent e30ec1f commit e1f3ab4
File tree
1 file changed
+12
-24
lines changed- Section11ObjectOrientedProgramming/src
1 file changed
+12
-24
lines changedLines changed: 12 additions & 24 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
| 1 | + | |
4 | 2 | | |
5 | 3 | | |
6 | 4 | | |
7 | | - | |
8 | | - | |
| 5 | + | |
9 | 6 | | |
10 | 7 | | |
11 | 8 | | |
12 | | - | |
13 | | - | |
| 9 | + | |
14 | 10 | | |
15 | 11 | | |
16 | 12 | | |
17 | | - | |
18 | | - | |
| 13 | + | |
19 | 14 | | |
20 | 15 | | |
21 | 16 | | |
22 | 17 | | |
23 | 18 | | |
24 | 19 | | |
25 | | - | |
26 | | - | |
| 20 | + | |
27 | 21 | | |
28 | 22 | | |
29 | 23 | | |
30 | 24 | | |
31 | 25 | | |
32 | 26 | | |
33 | | - | |
34 | | - | |
| 27 | + | |
35 | 28 | | |
36 | 29 | | |
37 | 30 | | |
38 | 31 | | |
39 | | - | |
40 | | - | |
| 32 | + | |
41 | 33 | | |
42 | 34 | | |
43 | 35 | | |
44 | | - | |
45 | | - | |
| 36 | + | |
46 | 37 | | |
47 | 38 | | |
48 | 39 | | |
49 | 40 | | |
50 | 41 | | |
51 | 42 | | |
52 | 43 | | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
| 44 | + | |
| 45 | + | |
58 | 46 | | |
59 | | - | |
| 47 | + | |
60 | 48 | | |
61 | 49 | | |
62 | 50 | | |
| |||
66 | 54 | | |
67 | 55 | | |
68 | 56 | | |
69 | | - | |
| 57 | + | |
0 commit comments