Skip to content

Commit e30ec1f

Browse files
committed
feat: Add Circle2 with encapsulation, validation, and geometry methods
WHAT the code does: Defines Circle2 with private fields radius and color. Provides: - Constructor: initializes radius = 1.0 and color = "red". - Getters: getRadius(), getColor() for controlled access. - Setters: setRadius(double r) and setColor(String c) with validation logic. Implements behavior methods: - area(): computes circle area (πr²). - circumference(): computes circle circumference (2πr). Defines DataHidding1 with main(): - Creates a Circle2 object. - Prints initial values using getters. - Updates fields with valid inputs via setters. - Demonstrates calculations for area and circumference. - Attempts invalid inputs (negative radius, empty color) to show validation in action. WHY this matters: Demonstrates data hiding, a key OOP principle: - Private fields prevent direct external modification. - Public methods enforce controlled access with validation. Shows how validation logic preserves object integrity (e.g., no negative radius or empty color). Illustrates combining encapsulation with object behavior methods for real-world modeling. HOW it works: Constructor sets default values radius = 1.0, color = red. Valid setter calls update values successfully (radius = 5.0, color = blue). Invalid setter calls reset values to defaults with console messages. area() and circumference() compute circle properties based on the current radius. main() prints results before and after updates. Tips and gotchas: Validation currently resets to defaults silently after printing messages; throwing exceptions could be clearer in robust applications. Comparing strings with null/empty check works here, but additional validation (e.g., restricting to known colors) could be added. Overriding toString() would simplify printing circle details. Fields could be made final with constructors for immutable design, if mutability isn’t needed. Use-cases: Educational example of encapsulation and data hiding in Java. Practical model for geometric objects with validation logic. Foundation for systems requiring controlled object state (e.g., graphics, simulations, GUIs). Short key: class-circle2 encapsulation validation geometry. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 495e93b commit e30ec1f

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed
Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,86 @@
1+
/**
2+
* This class demonstrates data hiding in Java
3+
* Data hiding is achieved by:
4+
* 1. Making instance variables private
5+
* 2. Providing public getter and setter methods for controlled access
6+
*/
7+
class Circle2 {
8+
// Private instance variables - hidden from outside access
9+
private double radius;
10+
private String color;
11+
12+
// Public constructor
13+
public Circle2() {
14+
radius = 1.0;
15+
color = "red";
16+
}
17+
18+
// Getter method for radius
19+
public double getRadius() {
20+
return radius;
21+
}
22+
23+
// Setter method for radius with validation
24+
public void setRadius(double r) {
25+
if (r > 0) {
26+
radius = r;
27+
} else {
28+
System.out.println("Radius must be positive. Setting to default value 1.0");
29+
radius = 1.0;
30+
}
31+
}
32+
33+
// Getter method for color
34+
public String getColor() {
35+
return color;
36+
}
37+
38+
// Setter method for color
39+
public void setColor(String c) {
40+
if (c != null && !c.isEmpty()) {
41+
color = c;
42+
} else {
43+
System.out.println("Color cannot be empty. Setting to default red");
44+
color = "red";
45+
}
46+
}
47+
48+
// Method to calculate area
49+
public double area() {
50+
return Math.PI * radius * radius;
51+
}
52+
53+
// Method to calculate circumference
54+
public double circumference() {
55+
return 2 * Math.PI * radius;
56+
}
57+
}
158

259
public class DataHidding1 {
360
public static void main(String[] args) {
4-
61+
// Create a Circle object
62+
Circle2 c1 = new Circle2();
63+
64+
// Access data using public methods, not directly
65+
System.out.println("Initial radius: " + c1.getRadius());
66+
System.out.println("Initial color: " + c1.getColor());
67+
68+
// Set new values using setter methods
69+
c1.setRadius(5.0);
70+
c1.setColor("blue");
71+
72+
// Access the updated values
73+
System.out.println("Updated radius: " + c1.getRadius());
74+
System.out.println("Updated color: " + c1.getColor());
75+
System.out.println("Area: " + c1.area());
76+
System.out.println("Circumference: " + c1.circumference());
77+
78+
// Attempt to set invalid values
79+
c1.setRadius(-2.5);
80+
c1.setColor("");
81+
82+
// Check that validation worked
83+
System.out.println("Radius after invalid input: " + c1.getRadius());
84+
System.out.println("Color after invalid input: " + c1.getColor());
585
}
686
}

0 commit comments

Comments
 (0)