Commit ae1cc9e
committed
feat: Add RectangleTest class with area, perimeter, and square check methods
WHAT the code does:
Defines RectangleTest with public fields:
- lenght (typo, should be length) and bradth (typo, should be breadth).
Implements:
- Area(): computes area as length × breadth.
- perimeter(): computes 2 × (length + breadth).
- isSquare(): checks if length equals breadth.
Defines Rectangle1 with main():
- Creates a RectangleTest object.
- Assigns values to length and breadth.
- Prints area, perimeter, and whether the rectangle is a square.
WHY this matters:
Demonstrates a simple class for geometric modeling.
Shows how object fields can be used to store state and methods to encapsulate behavior.
Illustrates the basic idea of combining attributes and behavior in OOP.
HOW it works:
RectangleTest r = new RectangleTest();
r.length = 10.5, r.breadth = 5.5.
Area = 10.5 × 5.5 = 57.75.
Perimeter = 2 × (10.5 + 5.5) = 32.0.
isSquare() returns false since length ≠ breadth.
Tips and gotchas:
Field names contain typos (lenght → length, bradth → breadth).
Fields are public, which breaks encapsulation; better design would use private fields with getters/setters.
Comparing doubles with == in isSquare() can be unsafe due to floating-point precision; consider using a tolerance.
Method names should follow Java naming conventions (area() instead of Area()).
Use-cases:
Educational example of simple classes and methods in Java.
Introductory demonstration of modeling real-world objects (rectangle).
Foundation for later refactoring into encapsulated, validated, and reusable shape classes.
Short key: class-rectangletest geometry area-perimeter square-check.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent f5a6ca4 commit ae1cc9e
1 file changed
+10
-16
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
| 1 | + | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
6 | | - | |
7 | | - | |
| 5 | + | |
8 | 6 | | |
9 | 7 | | |
10 | 8 | | |
11 | | - | |
12 | | - | |
| 9 | + | |
13 | 10 | | |
14 | 11 | | |
15 | 12 | | |
16 | | - | |
17 | | - | |
| 13 | + | |
18 | 14 | | |
19 | 15 | | |
20 | 16 | | |
21 | 17 | | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
| 18 | + | |
| 19 | + | |
26 | 20 | | |
27 | 21 | | |
28 | 22 | | |
| |||
36 | 30 | | |
37 | 31 | | |
38 | 32 | | |
39 | | - | |
40 | | - | |
| 33 | + | |
| 34 | + | |
41 | 35 | | |
42 | 36 | | |
43 | 37 | | |
44 | 38 | | |
45 | 39 | | |
46 | | - | |
| 40 | + | |
| 41 | + | |
47 | 42 | | |
48 | 43 | | |
49 | 44 | | |
| |||
56 | 51 | | |
57 | 52 | | |
58 | 53 | | |
59 | | - | |
0 commit comments