Skip to content

Commit ae1cc9e

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

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed
Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
1-
class RectangleTest
2-
{
1+
class RectangleTest {
32
public double lenght;
43
public double bradth;
54

6-
public double Area()
7-
{
5+
public double Area() {
86
return lenght * bradth;
97
}
108

11-
public double perimeter()
12-
{
9+
public double perimeter() {
1310
return 2*(lenght+bradth);
1411
}
1512

16-
public boolean isSquare()
17-
{
13+
public boolean isSquare() {
1814
return lenght == bradth;
1915
}
2016
}
2117

22-
class Rectangle1
23-
{
24-
public static void main(String[] args)
25-
{
18+
class Rectangle1 {
19+
public static void main(String[] args) {
2620
RectangleTest r = new RectangleTest();
2721
r.lenght=10.5;
2822
r.bradth=5.5;
@@ -36,14 +30,15 @@ public static void main(String[] args)
3630
Encapsulation is one of the fundamental principles of Object-Oriented Programming (OOP).
3731
It refers to hiding the internal details of an object and only exposing the necessary parts through well-defined methods.
3832
39-
In simple terms, encapsulation is the process of wrapping data (variables) and code (methods) together into a single unit (class) while restricting direct access to the data.
40-
33+
In simple terms, encapsulation is the process of wrapping data (variables) and code (methods) together into a single
34+
unit (class) while restricting direct access to the data.
4135
4236
Encapsulation in Java (OOP Concept)
4337
Encapsulation is one of the fundamental principles of Object-Oriented Programming (OOP).
4438
It refers to hiding the internal details of an object and only exposing the necessary parts through well-defined methods.
4539
46-
In simple terms, encapsulation is the process of wrapping data (variables) and code (methods) together into a single unit (class) while restricting direct access to the data.
40+
In simple terms, encapsulation is the process of wrapping data (variables) and code (methods) together into a single
41+
unit (class) while restricting direct access to the data.
4742
4843
Key Features of Encapsulation:
4944
@@ -56,4 +51,3 @@ The variables (fields) of a class are kept private to prevent direct access from
5651
Enhances Security:
5752
Prevents unauthorized access to data by restricting direct modification.
5853
*/
59-

0 commit comments

Comments
 (0)