Skip to content

Commit 495e93b

Browse files
committed
feat: Add DemoDataHidding with encapsulated fields and validation for rectangle properties
WHAT the code does: Defines DemoDataHidding with private fields length and breadth. Provides public getters and setters: - getLength(), getBreadth() return field values. - setLength(double l), setBreadth(double b) assign values, defaulting to 0 if negative. Implements methods: - area(): computes length × breadth. - perimeter(): computes 2 × (length + breadth). - isSquare(): returns true if length == breadth. Defines DataHidding with main(): - Attempts to create a Rectangle object, set dimensions, and print computed values. WHY this matters: Demonstrates data hiding by using private fields with controlled access through getters and setters. Validates inputs to prevent invalid states (e.g., negative dimensions). Models real-world rectangle properties with simple methods for area, perimeter, and square check. Highlights encapsulation as a core OOP principle. HOW it works: When positive values are set via setters, the fields are updated normally. If negative values are provided, the setters assign 0 instead. area() and perimeter() compute rectangle geometry. isSquare() compares length and breadth for equality. Tips and gotchas: The DataHidding class references Rectangle, which does not exist; it should reference DemoDataHidding instead. Comparing doubles with == in isSquare() may be unsafe due to floating-point precision issues; use a tolerance (e.g., Math.abs(a-b) < 1e-9). Encapsulation could be further strengthened by using constructors for required fields instead of relying only on setters. Overriding toString() would make printing object details cleaner. Use-cases: Educational example of encapsulation and data hiding in Java. Demonstrates geometry modeling with validation. Foundation for building robust shapes and geometry classes in larger applications. Short key: class-demodatahidding encapsulation rectangle-validation. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 104f008 commit 495e93b

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed
Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,48 @@
1-
class DemoDataHidding
2-
{
1+
class DemoDataHidding {
32
private double length;
43
private double breadth;
54

6-
public double getLength()
7-
{
5+
public double getLength() {
86
return length;
97
}
108

11-
public double getBreadth()
12-
{
9+
public double getBreadth() {
1310
return breadth;
1411
}
1512

16-
public void setLength(double l)
17-
{
13+
public void setLength(double l) {
1814
if(l>=0)
1915
length=l;
2016
else
2117
length=0;
2218
}
2319

24-
public void setBreadth(double b)
25-
{
20+
public void setBreadth(double b) {
2621
if(b>=0)
2722
breadth=b;
2823
else
2924
breadth=0;
3025
}
3126

32-
public double area()
33-
{
27+
public double area() {
3428
return getLength()*getBreadth();
3529
}
3630

37-
public double perimeter()
38-
{
31+
public double perimeter() {
3932
return 2*(length+breadth);
4033
}
4134

42-
public boolean isSquare()
43-
{
35+
public boolean isSquare() {
4436
if(length==breadth)
4537
return true;
4638
else
4739
return false;
4840
}
4941
}
50-
51-
52-
public class DataHidding
53-
{
54-
42+
public class DataHidding {
5543
public static void main(String[] args) {
5644
Rectangle r=new Rectangle();
57-
r.setLength(10.5);//check with negative values.
45+
r.setLength(10.5); //check with negative values.
5846
r.setBreadth(5.5);
5947

6048
System.out.println("Area "+r.area());
@@ -64,4 +52,4 @@ public static void main(String[] args) {
6452
System.out.println("Length "+r.getLength());
6553
System.out.println("Breadth "+r.getBreadth());
6654
}
67-
}
55+
}

0 commit comments

Comments
 (0)