Skip to content

Commit e1f3ab4

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

1 file changed

+12
-24
lines changed
Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,50 @@
1-
//Write this code again and again and solve it again.
2-
class Rectangle
3-
{
1+
class Rectangle {
42
private double length;
53
private double breadth;
64

7-
public double getLength()
8-
{
5+
public double getLength() {
96
return length;
107
}
118

12-
public double getBreadth()
13-
{
9+
public double getBreadth() {
1410
return breadth;
1511
}
1612

17-
public void setLength(double l)
18-
{
13+
public void setLength(double l) {
1914
if(l>=0)
2015
length=l;
2116
else
2217
length=0;
2318
}
2419

25-
public void setBreadth(double b)
26-
{
20+
public void setBreadth(double b) {
2721
if(b>=0)
2822
breadth=b;
2923
else
3024
breadth=0;
3125
}
3226

33-
public double area()
34-
{
27+
public double area() {
3528
//return length*breadth;
3629
return getLength()*getBreadth();
3730
}
3831

39-
public double perimeter()
40-
{
32+
public double perimeter() {
4133
return 2*(length+breadth);
4234
}
4335

44-
public boolean isSquare()
45-
{
36+
public boolean isSquare() {
4637
if(length==breadth)
4738
return true;
4839
else
4940
return false;
5041
}
5142
}
5243

53-
public class GetAndSetMethod
54-
{
55-
56-
public static void main(String[] args)
57-
{
44+
public class GetAndSetMethod {
45+
public static void main(String[] args) {
5846
Rectangle r=new Rectangle();
59-
r.setLength(10.5);//check with negative values.
47+
r.setLength(10.5); //check with negative values.
6048
r.setBreadth(5.5);
6149

6250
System.out.println("Area "+r.area());
@@ -66,4 +54,4 @@ public static void main(String[] args)
6654
System.out.println("Length "+r.getLength());
6755
System.out.println("Breadth "+r.getBreadth());
6856
}
69-
}
57+
}

0 commit comments

Comments
 (0)