Skip to content

Commit c8c8a8e

Browse files
committed
feat: Add ConstructorDemo1 with overloaded constructors for rectangles and squares
WHAT the code does: Defines ConstructorDemo1 with private fields length and breadth. Implements three constructors: - Default constructor: sets length and breadth to 1. - Parameterized constructor (double l, double b): initializes with given length and breadth. - Square constructor (double s): sets both length and breadth to the same value. Provides display() method to print length and breadth. Defines Constructor with main(): - Demonstrates creation of objects using all three constructors. - Calls display() to show initialized values. WHY this matters: Demonstrates constructor overloading in Java for flexible object initialization. Encapsulates rectangle and square representation in a single class. Illustrates default, parameterized, and convenience constructors. Shows how constructors eliminate the need for repetitive setter calls after object creation. HOW it works: c1 = new ConstructorDemo1(); → length = 1, breadth = 1. c2 = new ConstructorDemo1(5.0, 10.0); → length = 5, breadth = 10. c3 = new ConstructorDemo1(7.0); → length = breadth = 7. Each object calls display(), printing initialized values. Tips and gotchas: Field values are private, enforcing encapsulation; display() provides controlled access. Constructors with similar signatures can create ambiguity; here, double vs (double,double) resolves clearly. Adding validation inside constructors (e.g., rejecting negative dimensions) improves robustness. Overriding toString() could replace display() for more idiomatic printing. Use-cases: Educational demonstration of constructor overloading in Java. Practical design for representing rectangles and squares in geometry problems. Foundation for extending into area, perimeter, or volume calculations. Short key: class-constructordemo1 constructor-overloading rectangle-square. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent e474d62 commit c8c8a8e

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed
Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,38 @@
1-
class ConstructorDemo1
2-
{
1+
class ConstructorDemo1 {
32
private double length;
43
private double breadth;
54

6-
public ConstructorDemo1()
7-
{
5+
public ConstructorDemo1() {
86
length=1;
97
breadth=1;
108
}
11-
public ConstructorDemo1(double l, double b)
12-
{
9+
public ConstructorDemo1(double l, double b) {
1310
length=l;
1411
breadth=b;
1512
}
16-
public ConstructorDemo1(double s)
17-
{
13+
public ConstructorDemo1(double s) {
1814
length=breadth=s;
1915
}
16+
2017
// Method to display the values
21-
public void display()
22-
{
18+
public void display() {
2319
System.out.println("Length: " + length + ", Breadth: " + breadth);
2420
}
2521
}
2622

27-
public class Constructor
28-
{
29-
public static void main(String[] args)
30-
{
23+
public class Constructor {
24+
public static void main(String[] args) {
3125
ConstructorDemo1 c = new ConstructorDemo1();
3226

3327
// Calling different constructors
28+
ConstructorDemo1 c1 = new ConstructorDemo1(); // Default constructor.
29+
30+
ConstructorDemo1 c2 = new ConstructorDemo1(5.0, 10.0); // Parameterized constructor.
3431

35-
ConstructorDemo1 c1 = new ConstructorDemo1(); // Default constructor
36-
ConstructorDemo1 c2 = new ConstructorDemo1(5.0, 10.0); // Parameterized constructor
37-
ConstructorDemo1 c3 = new ConstructorDemo1(7.0); // Square constructor
32+
ConstructorDemo1 c3 = new ConstructorDemo1(7.0); // Square constructor.
3833

39-
// Displaying the values
4034
c1.display();
4135
c2.display();
4236
c3.display();
4337
}
44-
}
38+
}

0 commit comments

Comments
 (0)